python使用matplotlib繪制柱狀圖教程
Matplotlib的概念這里就不多介紹了,關(guān)于繪圖庫(kù)Matplotlib的安裝方法:點(diǎn)擊這里
小編之前也和大家分享過(guò)python使用matplotlib實(shí)現(xiàn)的折線圖和制餅圖效果,感興趣的朋友們也可以點(diǎn)擊查看,下面來(lái)看看python使用matplotlib繪制柱狀圖的方法吧,具體如下:
1. 基本的柱狀圖
import matplotlib.pyplot as plt data = [5, 20, 15, 25, 10] plt.bar(range(len(data)), data) plt.show()
plt.bar函數(shù)簽名為:
bar(left, height, width=0.8, bottom=None, **kwargs)
事實(shí)上,left,height,width,bottom這四個(gè)參數(shù)確定了柱體的位置和大小。默認(rèn)情況下,left為柱體的居中位置(可以通過(guò)align參數(shù)來(lái)改變left值的含義),即:
(left - width / 2, bottom)
為左下角位置(left + width / 2, bottom + height)
為右上角位置
例如:
import matplotlib.pyplot as plt data = [5, 20, 15, 25, 10] plt.bar([0.3, 1.7, 4, 6, 7], data, width=0.6, bottom=[10, 0, 5, 0, 5]) plt.show()
2. 設(shè)置柱體樣式
(1)顏色
通過(guò)facecolor(或fc)關(guān)鍵字參數(shù)可以設(shè)置柱體顏色,例如:
import matplotlib.pyplot as plt data = [5, 20, 15, 25, 10] plt.bar(range(len(data)), data, fc='g') plt.show()
通過(guò)color關(guān)鍵字參數(shù) 可以一次性設(shè)置多個(gè)顏色,例如:
import matplotlib.pyplot as plt data = [5, 20, 15, 25, 10] plt.bar(range(len(data)), data, color='rgb') # or `color=['r', 'g', 'b']` plt.show()
(2)描邊
相關(guān)的關(guān)鍵字參數(shù)為:
- edgecolor 或 ec
- linestyle 或 ls
- linewidth 或 lw
例如:
import matplotlib.pyplot as plt data = [5, 20, 15, 25, 10] plt.bar(range(len(data)), data, ec='r', ls='--', lw=2) plt.show()
(3)填充
hatch關(guān)鍵字可用來(lái)設(shè)置填充樣式,可取值為:/, \, |, -, +, x, o, O, ., *。例如:
import matplotlib.pyplot as plt data = [5, 20, 15, 25, 10] plt.bar(range(len(data)), data, ec='k', lw=1, hatch='o') plt.show()
3. 設(shè)置tick label
import matplotlib.pyplot as plt data = [5, 20, 15, 25, 10] labels = ['Tom', 'Dick', 'Harry', 'Slim', 'Jim'] plt.bar(range(len(data)), data, tick_label=labels) plt.show()
4. 堆疊柱狀圖
通過(guò)bottom參數(shù),可以繪制堆疊柱狀圖。例如:
import numpy as np import matplotlib.pyplot as plt size = 5 x = np.arange(size) a = np.random.random(size) b = np.random.random(size) plt.bar(x, a, label='a') plt.bar(x, b, bottom=a, label='b') plt.legend() plt.show()
5. 并列柱狀圖
繪制并列柱狀圖與堆疊柱狀圖類似,都是繪制多組柱體,只需要控制好每組柱體的位置和大小即可。例如:
import numpy as np import matplotlib.pyplot as plt size = 5 x = np.arange(size) a = np.random.random(size) b = np.random.random(size) c = np.random.random(size) total_width, n = 0.8, 3 width = total_width / n x = x - (total_width - width) / 2 plt.bar(x, a, width=width, label='a') plt.bar(x + width, b, width=width, label='b') plt.bar(x + 2 * width, c, width=width, label='c') plt.legend() plt.show()
6. 條形圖
使用barh方法繪制條形圖。例如:
import matplotlib.pyplot as plt data = [5, 20, 15, 25, 10] plt.barh(range(len(data)), data) plt.show()
plt.barh方法的簽名為:
barh(bottom, width, height=0.8, left=None, **kwargs)
可以看到與plt.bar方法類似。因此堆積條形圖和并列條形圖的畫法與前面類似,不做贅述。
7. 正負(fù)條形圖
import numpy as np import matplotlib.pyplot as plt a = np.array([5, 20, 15, 25, 10]) b = np.array([10, 15, 20, 15, 5]) plt.barh(range(len(a)), a) plt.barh(range(len(b)), -b) plt.show()
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家學(xué)習(xí)或者使用python能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流。
- Python使用matplotlib繪制動(dòng)畫的方法
- python使用matplotlib繪制折線圖教程
- python學(xué)習(xí)之matplotlib繪制散點(diǎn)圖實(shí)例
- Python通過(guò)matplotlib繪制動(dòng)畫簡(jiǎn)單實(shí)例
- Python使用matplotlib繪制多個(gè)圖形單獨(dú)顯示的方法示例
- python使用matplotlib模塊繪制多條折線圖、散點(diǎn)圖
- python使用matplotlib繪制熱圖
- Python用?matplotlib?繪制柱狀圖
- Python?matplotlib?繪制散點(diǎn)圖詳解建議收藏
- python中matplotlib的用法及繪制簡(jiǎn)單圖形詳解
相關(guān)文章
python利用元類和描述器實(shí)現(xiàn)ORM模型的詳細(xì)步驟
Python中的類與數(shù)據(jù)庫(kù)之間的映射,對(duì)數(shù)據(jù)的操作就不用編寫SQL語(yǔ)言了,因?yàn)槎挤庋b好了,比如你想插入一條數(shù)據(jù),你就直接創(chuàng)建一個(gè)對(duì)象即可,下面通過(guò)本文學(xué)習(xí)下python利用元類和描述器實(shí)現(xiàn)ORM模型的詳細(xì)步驟,感興趣的朋友一起看看吧2021-11-11Python之Matlibplot畫圖功能演示過(guò)程
這篇文章主要介紹了Python之Matlibplot畫圖功能演示過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09Python實(shí)現(xiàn)命令行通訊錄實(shí)例教程
這篇文章主要介紹怎樣編寫了一段命令行通訊錄的小程序。下面是編寫的思路以及代碼,歡迎感興趣的同學(xué)交流探討。2016-08-08Python實(shí)現(xiàn)PDF到Word文檔的高效轉(zhuǎn)換
這篇文章主要為大家詳細(xì)介紹了如何使用Python編程語(yǔ)言,結(jié)合庫(kù)和工具,將PDF文件轉(zhuǎn)換為可編輯的Word文檔,使文檔的編輯變得方便高效,需要的可以參考下2024-01-01ActiveMQ:使用Python訪問(wèn)ActiveMQ的方法
今天小編就為大家分享一篇ActiveMQ:使用Python訪問(wèn)ActiveMQ的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01