python如何利用matplotlib繪制并列雙柱狀圖并標(biāo)注數(shù)值
項(xiàng)目場(chǎng)景:
Python項(xiàng)目需要畫兩組數(shù)據(jù)的雙柱狀圖,以下以一周七天兩位小朋友吃糖顆數(shù)為例進(jìn)行演示,用matplotlib庫實(shí)現(xiàn)
代碼:
import matplotlib import matplotlib.pyplot as plt import numpy as np def drawHistogram(): matplotlib.rc("font", family='MicroSoft YaHei') list1 = np.array([5, 2, 1, 0, 8, 0, 6]) # 柱狀圖第一組數(shù)據(jù) list2 = np.array([9, 5, 1, 2, 9, 2, 0]) # 柱狀圖第二組數(shù)據(jù) length = len(list1) x = np.arange(length) # 橫坐標(biāo)范圍 listDate = ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期天"] plt.figure() total_width, n = 0.8, 2 # 柱狀圖總寬度,有幾組數(shù)據(jù) width = total_width / n # 單個(gè)柱狀圖的寬度 x1 = x - width / 2 # 第一組數(shù)據(jù)柱狀圖橫坐標(biāo)起始位置 x2 = x1 + width # 第二組數(shù)據(jù)柱狀圖橫坐標(biāo)起始位置 plt.title("一周每天吃悠哈軟糖顆數(shù)柱狀圖") # 柱狀圖標(biāo)題 # plt.xlabel("星期") # 橫坐標(biāo)label 此處可以不添加 plt.ylabel("吃悠哈軟糖顆數(shù)(個(gè))") # 縱坐標(biāo)label plt.bar(x1, list1, width=width, label="小s吃糖數(shù)") plt.bar(x2, list2, width=width, label="小y吃糖數(shù)") plt.xticks(x, listDate) # 用星期幾替換橫坐標(biāo)x的值 plt.legend() # 給出圖例 plt.show() if __name__ == '__main__': drawHistogram()
效果圖:
擴(kuò)展功能及代碼:
擴(kuò)展功能一
如果橫坐標(biāo)標(biāo)簽比較長(zhǎng)或是文字比較多,以一定角度傾斜展示,上文中代碼這一行:
plt.xticks(x, listDate)
可以改為:
plt.xticks(x, listDate, rotation=30) # rotation為標(biāo)簽旋轉(zhuǎn)角度
橫坐標(biāo)標(biāo)簽旋轉(zhuǎn)30°效果如下:
橫坐標(biāo)標(biāo)簽旋轉(zhuǎn)90°效果如下:
擴(kuò)展功能二
如果希望具體的數(shù)據(jù)值展示在柱狀圖中,可以在代碼 plt.legend()
前加入如下代碼:
for a, b in zip(x1, list1): plt.text(a, b + 0.1, '%.0f' % b, ha='center', va='bottom', fontsize=7) for a, b in zip(x2, list2): plt.text(a, b + 0.1, '%.0f' % b, ha='center', va='bottom', fontsize=7)
加了具體數(shù)值的柱狀圖效果如下:
補(bǔ)充:Python畫圖實(shí)現(xiàn)同一結(jié)點(diǎn)多個(gè)柱狀圖
import numpy as np x = [1,2] #橫坐標(biāo) y = [3,4] #第一個(gè)縱坐標(biāo) y1 = [5,6] #第二個(gè)縱坐標(biāo) x = np.arange(len(x)) #首先用第一個(gè)的長(zhǎng)度作為橫坐標(biāo) width = 0.05 #設(shè)置柱與柱之間的寬度 fig,ax = plt.subplots() ax.bar(x,y,width,alpha = 0.9) ax.bar(x+width,y1,width,alpha = 0.9,color= 'red') ax.set_xticks(x +width/2)#將坐標(biāo)設(shè)置在指定位置 ax.set_xticklabels(x)#將橫坐標(biāo)替換成 plt.show()
后續(xù)有時(shí)間再繼續(xù)補(bǔ)充擴(kuò)展功能哦~
總結(jié)
到此這篇關(guān)于python如何利用matplotlib繪制并列雙柱狀圖并標(biāo)注數(shù)值的文章就介紹到這了,更多相關(guān)python matplotlib繪制并列雙柱狀圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 使用Python的matplotlib庫繪制柱狀圖
- 教你利用python的matplotlib(pyplot)繪制折線圖和柱狀圖
- Python數(shù)據(jù)分析之使用matplotlib繪制折線圖、柱狀圖和柱線混合圖
- Python使用matplotlib給柱狀圖添加數(shù)據(jù)標(biāo)簽bar_label()
- python中如何利用matplotlib畫多個(gè)并列的柱狀圖
- Python用?matplotlib?繪制柱狀圖
- python使用matplotlib畫柱狀圖、散點(diǎn)圖
- python使用matplotlib繪制柱狀圖教程
- Python 如何利用pandas 和 matplotlib繪制柱狀圖
相關(guān)文章
python獲取網(wǎng)頁中所有圖片并篩選指定分辨率的方法
下面小編就為大家分享一篇python獲取網(wǎng)頁中所有圖片并篩選指定分辨率的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-03-03python實(shí)現(xiàn)簡(jiǎn)單文件讀寫函數(shù)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)簡(jiǎn)單文件讀寫函數(shù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-02-02

使用python將請(qǐng)求的requests headers參數(shù)格式化方法

Pandas 對(duì)Dataframe結(jié)構(gòu)排序的實(shí)現(xiàn)方法

python 爬取古詩文存入mysql數(shù)據(jù)庫的方法