python制作各种条形图
生活随笔
收集整理的這篇文章主要介紹了
python制作各种条形图
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
??豎向條形圖
???源代碼如下:
import matplotlib.pyplot as plt# 這兩行代碼解決 plt 中文顯示的問題 plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = Falsewaters = ('綠色', '藍色', '紅色', '白色', '黑色') buy_number = [6, 7, 6, 1, 2]#通過color可調節圖的顏色,可設定顏色列表,這樣條形圖每條顏色可變 colorss = ['g','b','r','w','k'] #通過width變量可調整條形的寬度 width = 0.4 # the width of the bars #通過label可對條形進行標簽,必須與plt.legend()連用,給所有條形設置標簽需用列表 plt.bar(waters, buy_number, width, color=colorss,label='hello') plt.legend() plt.title('顏色統計')plt.show()???效果圖如下:
??橫向條形圖:
???橫向條形圖就是將豎向條形圖的bar函數改為barh函數
???源代碼如下:
???效果圖如下:
??并列條形圖:
???源代碼如下:
import matplotlib.pyplot as plt import numpy as np# 這兩行代碼解決 plt 中文顯示的問題 plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = False width = 0.4 # the width of the bars#設置x軸標注 waters = ('綠色', '藍色', '紅色', '白色', '黑色') #設置條形縱坐標 buy_number = [6, 7, 6, 1, 2] buy_number1 = [1, 2, 3, 4, 5]#設置條形橫坐標 index_male = np.arange(len(waters)) # 男生條形圖的橫坐標 index_female = index_male + width # 女生條形圖的橫坐標#通過color可調節圖的顏色,可設定顏色列表,這樣條形圖每條顏色可變 colorss = ['g','b','r','w','k'] #通過width變量可調整條形的寬度 #通過label可對條形進行標簽,必須與plt.legend()連用,給所有條形設置標簽需用列表 #在并行條形圖中,有幾個并行就使用幾次bar,需要特別標出height和width變量,第一個參數為橫坐標 plt.bar(index_male, height=buy_number, width=width, color=colorss,label='hello') plt.bar(index_female,height=buy_number1,width=width)#可調節坐標刻度,顯示更加美觀 plt.xticks(index_male + width/2, waters) plt.legend() plt.title('顏色統計')plt.show()???效果圖如下:
??重疊條形圖:
??對于重疊條形圖,可通過設置相同x值,不同y值表現。使用不同顏色顯示,即使用兩個bar函數。源代碼如下:
import numpy as np import matplotlib.pyplot as pltplt.subplot(1, 1, 1) fig = plt.figure() plt.figure(figsize=(8, 6)) plt.rcParams['font.sans-serif'] = ['KaiTi'] plt.rcParams['axes.unicode_minus'] = False x = np.array(["a", "b", "c", "d"]) y1 = np.array([8566, 5335, 7610, 6482]) y2 = np.array([4283, 2667, 3655, 3241]) plt.bar(x, y1, width=0.3, label="1") plt.bar(x, y2, width=0.3, label="2") plt.title("xxx", loc="center") plt.grid(False) plt.legend(loc="upper center", ncol=2) plt.show()效果圖如下:
參考博客:https://blog.csdn.net/robert_chen1988/article/details/100047692
總結
以上是生活随笔為你收集整理的python制作各种条形图的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【精辟】socket阻塞与非阻塞,同步与
- 下一篇: python多进程管道通信(精)