利用matplotlib繪製條形圖

利用matplotlib繪製條形圖

matplotlib是python中一種非常強大的繪圖庫,是數據分析必不可少的一部分,它可以把數據轉化成統計圖形的方式展示,這次是利用matplotlib來繪製條形圖,matplotlib還可以繪製很多統計圖形,比如:散點圖、折線圖、曲線圖等等。這些圖形案例都可以上matplotlib官網中去查看,官網上的案例都是代碼,可以直接運行的,這樣就更方便我們學習matplotlib(https://matplotlib.org/gallery/index.html)

首先導入matplotlib庫

from matplotlib import pyplot as plt
from matplotlib import font_manager

這次只需要用到pyplot和font_manager這兩個方法,font_manager使用來設置中文字體,因爲有時候刻度或者標題需要展示中文,所以要設置中文字體

準備數據

# 設置兩個數據,關於2017年各電影票房
movie_name = ['戰狼2','速度與激情8','羞羞的鐵拳','前任3:再見前任','功夫瑜伽','西遊伏妖篇','變形金剛5:最後的騎士','芳華','摔跤吧!爸爸','尋夢環遊記']
movie_box = [53.39,26.49,21.9,19.26,17.53,16.49,15.45,14.11,12.96,12.02]

設置圖形大小和像素

plt.figure(figsize=(20,18),dpi=80)

figsize傳遞的參數是圖形大小,dpi是像素

繪圖

# r = plt.bar(range(len(movie_name)),movie_box,width=0.4,color='MidnightBlue')#barh是代表繪製橫向條形圖,而bar就是代表直的條形圖

這裏要用一個參數來接收plt.bar的數據,用於後面展示座標,也可以不用設參數來接收同樣能夠展示圖形,len(movie_name)是代表x座標軸,movie_box表示y軸。width是代表條形圖的寬度,範圍是0~1,color傳遞的是條形圖的顏色。

設置中文字體

zh_font = font_manager.FontProperties(fname=r'D:\mypathon3\python\font.ttc')

要先下載中文字體的ttc文件

繪製X軸刻度

plt.xticks(range(len(movie_name)),movie_name,fontproperties=zh_font)

因爲原先x軸的刻度是len(movie_name),所以這裏繪製x軸刻度是要遍歷len(movie_name)然後用movie_name電影名一一對應上。因爲電影名是中文,所以要傳遞中文字體的參數,用fontproperties傳參。

繪製y軸刻度也是一樣,plt.yticks()

繪製網格線

plt.grid(alpha=0.5,color='plum')

alpha傳遞的是網格線的透明度,color傳遞的是網格線的顏色。

保存圖片

plt.savefig(r'I:\crack\img\movie3.png',bbox_inches = 'tight')

傳遞bbox_inches參數是爲了保存完整的圖片,因爲我發現這個圖如果不傳遞bbox_inches無法展現全部內容。

條形圖的座標

for rects in r:
    height = rects.get_height()
    plt.text(rects.get_x() + rects.get_width()/2,1.03*height,'%s'%float(height),fontsize = 18,ha='center')

plt.bar().text(x, y, s, fontdict=fontdict, withdash=withdash, **kwargs,fontsize = 18,ha=‘center’)
先遍歷圖形信息,然後獲得條形圖座標的高,x 和y的參數是表示座標位置,s傳遞的參數是要在座標上展示的數據,就是條形圖y軸的座標。而fontsize是代表字體的大小,ha代表在橫向位置上的位置。

最後展示圖形

plt.show()

繪製多條條形圖

首先準備數據

movie_name = ['戰狼2','功夫瑜伽','前任3:再見前任','變形金剛5:最後的騎士']
day_14 = [1234,2901,5743,2187]
day_15 = [342,2467,4938,2314]
day_16 = [2222,432,567,457]

四部電影在三天票房的對比,因爲多條條形圖和一條條形圖有些代碼是一樣的,這裏就不做過多的解釋了。

準備X軸

bar_width = 0.3
x_14 = list(range(len(movie_name)))
x_15 = [i + bar_width for i in x_14]
x_16 = [i + bar_width*2 for i in x_14]

這裏設定每條條形圖的寬度都是0.3,因爲三條條形圖要貼在一起所以要遍歷x軸。

繪圖

rect_1 = plt.bar(x_14,day_14,width=bar_width,label = '14日票房',color = 'Magenta')
rect_2 = plt.bar(x_15,day_15,width=bar_width,label = '15日票房',color = 'Indigo')
rect_3 = plt.bar(x_16,day_16,width=bar_width,label = '16日票房')

這裏的繪圖多了一個label,這個變量是用來製作圖例。

繪製X軸刻度

plt.xticks(x_15,movie_name,fontproperties=zh_font)

這裏因爲三條條形圖x軸的刻度都是一樣的,所以只需要傳遞一個參數,因爲要在中間展示,所以就放在第二條的位置上。

繪製圖例

plt.legend(prop = zh_font)

這裏的圖例也是用中文展示,所以也要傳遞中文字體

繪製座標

for rect_1s in rect_1:
    height = rect_1s.get_height()
    plt.text(rect_1s.get_x() + rect_1s.get_width()/2,1.03*height,'%s'%float(height),ha='center')

for rect_2s in rect_2:
    heights = rect_2s.get_height()
    plt.text(rect_2s.get_x() + rect_2s.get_width()/2,heights,int(heights),ha='center')

for rect_3s in rect_3:
    heights = rect_3s.get_height()
    plt.text(rect_3s.get_x() + rect_3s.get_width()/2,heights,int(heights),ha='center')

多條條形圖的源代碼

movie_name = ['戰狼2','功夫瑜伽','前任3:再見前任','變形金剛5:最後的騎士']
day_14 = [1234,2901,5743,2187]
day_15 = [342,2467,4938,2314]
day_16 = [2222,432,567,457]

#設置中文字體
zh_font = font_manager.FontProperties(fname=r'D:\mypathon3\python\font.ttc')

#準備X軸,寬帶爲0.3
bar_width = 0.3
x_14 = list(range(len(movie_name)))
x_15 = [i + bar_width for i in x_14]
x_16 = [i + bar_width*2 for i in x_14]

#繪圖
rect_1 = plt.bar(x_14,day_14,width=bar_width,label = '14日票房',color = 'Magenta')
rect_2 = plt.bar(x_15,day_15,width=bar_width,label = '15日票房',color = 'Indigo')
rect_3 = plt.bar(x_16,day_16,width=bar_width,label = '16日票房')

#X軸的刻度
plt.xticks(x_15,movie_name,fontproperties=zh_font)

#圖例
plt.legend(prop = zh_font)
#座標
for rect_1s in rect_1:
    height = rect_1s.get_height()
    plt.text(rect_1s.get_x() + rect_1s.get_width()/2,1.03*height,'%s'%float(height),ha='center')

for rect_2s in rect_2:
    heights = rect_2s.get_height()
    plt.text(rect_2s.get_x() + rect_2s.get_width()/2,heights,int(heights),ha='center')

for rect_3s in rect_3:
    heights = rect_3s.get_height()
    plt.text(rect_3s.get_x() + rect_3s.get_width()/2,heights,int(heights),ha='center')

plt.show()

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章