python中如何利用matplotlib畫(huà)多個(gè)并列的柱狀圖
首先如果柱狀圖中有中文,比如X軸和Y軸標(biāo)簽需要寫中文,解決中文無(wú)法識(shí)別和亂碼的情況,加下面這行代碼就可以解決了:
plt.rcParams['font.sans-serif'] = ['SimHei'] # 解決中文亂碼
以下總共展示了三種畫(huà)不同需求的柱狀圖:
畫(huà)多組兩個(gè)并列的柱狀圖:
import matplotlib import matplotlib.pyplot as plt import numpy as np plt.rcParams['font.sans-serif'] = ['SimHei'] labels = ['G1', 'G2', 'G3', 'G4', 'G5'] men_means = [20, 34, 30, 35, 27] women_means = [25, 32, 34, 20, 25] x = np.arange(len(labels)) # the label locations width = 0.35 # the width of the bars fig, ax = plt.subplots() rects1 = ax.bar(x - width/2, men_means, width, label='Men') rects2 = ax.bar(x + width/2, women_means, width, label='Women') # Add some text for labels, title and custom x-axis tick labels, etc. ax.set_ylabel('Scores') ax.set_title('Scores by group and gender') ax.set_xticks(x) ax.set_xticklabels(labels) ax.legend() def autolabel(rects): """Attach a text label above each bar in *rects*, displaying its height.""" for rect in rects: height = rect.get_height() ax.annotate('{}'.format(height), xy=(rect.get_x() + rect.get_width() / 2, height), xytext=(0, 3), # 3 points vertical offset textcoords="offset points", ha='center', va='bottom') autolabel(rects1) autolabel(rects2) fig.tight_layout() plt.show()
繪制好的柱狀圖如下:
畫(huà)兩組5個(gè)并列的柱狀圖:
import matplotlib import matplotlib.pyplot as plt import numpy as np plt.rcParams['font.sans-serif']=['SimHei'] # 解決中文亂碼 labels = ['第一項(xiàng)', '第二項(xiàng)'] a = [4.0, 3.8] b = [26.9, 48.1] c = [55.6, 63.0] d = [59.3, 81.5] e = [89, 90] x = np.arange(len(labels)) # 標(biāo)簽位置 width = 0.1 # 柱狀圖的寬度,可以根據(jù)自己的需求和審美來(lái)改 fig, ax = plt.subplots() rects1 = ax.bar(x - width*2, a, width, label='a') rects2 = ax.bar(x - width+0.01, b, width, label='b') rects3 = ax.bar(x + 0.02, c, width, label='c') rects4 = ax.bar(x + width+ 0.03, d, width, label='d') rects5 = ax.bar(x + width*2 + 0.04, e, width, label='e') # 為y軸、標(biāo)題和x軸等添加一些文本。 ax.set_ylabel('Y軸', fontsize=16) ax.set_xlabel('X軸', fontsize=16) ax.set_title('這里是標(biāo)題') ax.set_xticks(x) ax.set_xticklabels(labels) ax.legend() def autolabel(rects): """在*rects*中的每個(gè)柱狀條上方附加一個(gè)文本標(biāo)簽,顯示其高度""" for rect in rects: height = rect.get_height() ax.annotate('{}'.format(height), xy=(rect.get_x() + rect.get_width() / 2, height), xytext=(0, 3), # 3點(diǎn)垂直偏移 textcoords="offset points", ha='center', va='bottom') autolabel(rects1) autolabel(rects2) autolabel(rects3) autolabel(rects4) autolabel(rects5) fig.tight_layout() plt.show()
繪制好的柱狀圖如下:
3. 要將柱狀圖的樣式畫(huà)成適合論文中使用的黑白并且?guī)Щy的樣式:
import matplotlib.pyplot as plt import numpy as np plt.rcParams['font.sans-serif'] = ['SimHei'] # 解決中文亂碼 labels = ['第一項(xiàng)', '第二項(xiàng)'] a = [50, 80] b = [37, 69] c = [78, 60] d = [66, 86] e = [80, 95] # marks = ["o", "X", "+", "*", "O"] x = np.arange(len(labels)) # 標(biāo)簽位置 width = 0.1 # 柱狀圖的寬度 fig, ax = plt.subplots() rects1 = ax.bar(x - width * 2, a, width, label='a', hatch="...", color='w', edgecolor="k") rects2 = ax.bar(x - width + 0.01, b, width, label='b', hatch="oo", color='w', edgecolor="k") rects3 = ax.bar(x + 0.02, c, width, label='c', hatch="++", color='w', edgecolor="k") rects4 = ax.bar(x + width + 0.03, d, width, label='d', hatch="XX", color='w', edgecolor="k") rects5 = ax.bar(x + width * 2 + 0.04, e, width, label='e', hatch="**", color='w', edgecolor="k") # 為y軸、標(biāo)題和x軸等添加一些文本。 ax.set_ylabel('Y', fontsize=16) ax.set_xlabel('X', fontsize=16) ax.set_title('標(biāo)題') ax.set_xticks(x) ax.set_xticklabels(labels) ax.legend()
以上
總結(jié)
到此這篇關(guān)于python中如何利用matplotlib畫(huà)多個(gè)并列柱狀圖的文章就介紹到這了,更多相關(guān)python matplotlib畫(huà)并列柱狀圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python+request+unittest實(shí)現(xiàn)接口測(cè)試框架集成實(shí)例
這篇文章主要介紹了Python+request+unittest實(shí)現(xiàn)接口測(cè)試框架集成實(shí)例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03python實(shí)現(xiàn)將range()函數(shù)生成的數(shù)字存儲(chǔ)在一個(gè)列表中
這篇文章主要介紹了python實(shí)現(xiàn)將range()函數(shù)生成的數(shù)字存儲(chǔ)在一個(gè)列表中,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04python selenium禁止加載某些請(qǐng)求的實(shí)現(xiàn)
本文主要介紹了python selenium禁止加載某些請(qǐng)求的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01python 爬蟲(chóng) 實(shí)現(xiàn)增量去重和定時(shí)爬取實(shí)例
今天小編就為大家分享一篇python 爬蟲(chóng) 實(shí)現(xiàn)增量去重和定時(shí)爬取實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02Python的Flask框架中實(shí)現(xiàn)登錄用戶的個(gè)人資料和頭像的教程
這篇文章主要介紹了Python的Flask框架中實(shí)現(xiàn)登錄用戶的個(gè)人資料和頭像的教程,這也是各個(gè)web框架的最基本功能之一,需要的朋友可以參考下2015-04-04python獲取網(wǎng)絡(luò)圖片方法及整理過(guò)程詳解
這篇文章主要介紹了python獲取網(wǎng)絡(luò)圖片方法及整理過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12python3.5實(shí)現(xiàn)socket通訊示例(TCP)
本篇文章主要介紹了python3.5實(shí)現(xiàn)socket通訊示例(TCP),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02