python制作各种条形图
生活随笔
收集整理的這篇文章主要介紹了
python制作各种条形图
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
??豎向條形圖
???源代碼如下:
import matplotlib.pyplot as plt# 這兩行代碼解決 plt 中文顯示的問(wèn)題 plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = Falsewaters = ('綠色', '藍(lán)色', '紅色', '白色', '黑色') buy_number = [6, 7, 6, 1, 2]#通過(guò)color可調(diào)節(jié)圖的顏色,可設(shè)定顏色列表,這樣條形圖每條顏色可變 colorss = ['g','b','r','w','k'] #通過(guò)width變量可調(diào)整條形的寬度 width = 0.4 # the width of the bars #通過(guò)label可對(duì)條形進(jìn)行標(biāo)簽,必須與plt.legend()連用,給所有條形設(shè)置標(biāo)簽需用列表 plt.bar(waters, buy_number, width, color=colorss,label='hello') plt.legend() plt.title('顏色統(tǒng)計(jì)')plt.show()???效果圖如下:
??橫向條形圖:
???橫向條形圖就是將豎向條形圖的bar函數(shù)改為barh函數(shù)
???源代碼如下:
???效果圖如下:
??并列條形圖:
???源代碼如下:
import matplotlib.pyplot as plt import numpy as np# 這兩行代碼解決 plt 中文顯示的問(wèn)題 plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = False width = 0.4 # the width of the bars#設(shè)置x軸標(biāo)注 waters = ('綠色', '藍(lán)色', '紅色', '白色', '黑色') #設(shè)置條形縱坐標(biāo) buy_number = [6, 7, 6, 1, 2] buy_number1 = [1, 2, 3, 4, 5]#設(shè)置條形橫坐標(biāo) index_male = np.arange(len(waters)) # 男生條形圖的橫坐標(biāo) index_female = index_male + width # 女生條形圖的橫坐標(biāo)#通過(guò)color可調(diào)節(jié)圖的顏色,可設(shè)定顏色列表,這樣條形圖每條顏色可變 colorss = ['g','b','r','w','k'] #通過(guò)width變量可調(diào)整條形的寬度 #通過(guò)label可對(duì)條形進(jìn)行標(biāo)簽,必須與plt.legend()連用,給所有條形設(shè)置標(biāo)簽需用列表 #在并行條形圖中,有幾個(gè)并行就使用幾次bar,需要特別標(biāo)出height和width變量,第一個(gè)參數(shù)為橫坐標(biāo) plt.bar(index_male, height=buy_number, width=width, color=colorss,label='hello') plt.bar(index_female,height=buy_number1,width=width)#可調(diào)節(jié)坐標(biāo)刻度,顯示更加美觀 plt.xticks(index_male + width/2, waters) plt.legend() plt.title('顏色統(tǒng)計(jì)')plt.show()???效果圖如下:
??重疊條形圖:
??對(duì)于重疊條形圖,可通過(guò)設(shè)置相同x值,不同y值表現(xiàn)。使用不同顏色顯示,即使用兩個(gè)bar函數(shù)。源代碼如下:
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
總結(jié)
以上是生活随笔為你收集整理的python制作各种条形图的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 【精辟】socket阻塞与非阻塞,同步与
- 下一篇: 使用caffe自带模型训练cifar10