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)大家可以留言交流。
相關(guān)文章
python使用OpenCV實(shí)現(xiàn)多目標(biāo)跟蹤
這篇文章主要介紹了python使用OpenCV實(shí)現(xiàn)多目標(biāo)跟蹤,如何在OpenCV中使用MultiTracker類實(shí)現(xiàn)多目標(biāo)跟蹤API。在深入了解詳細(xì)信息之前,請(qǐng)查看下面列出的關(guān)于目標(biāo)跟蹤的帖子,以了解在OpenCV中實(shí)現(xiàn)的單個(gè)目標(biāo)跟蹤器的基礎(chǔ)知識(shí),需要的朋友可以參考一下2022-04-04
matlab調(diào)用python的各種方法舉例子詳解
為了發(fā)揮matlab的繪圖優(yōu)勢(shì)+原先python寫好的功能組合方式,下面這篇文章主要給大家介紹了關(guān)于matlab調(diào)用python的各種方法,需要的朋友可以參考下2023-09-09
Python 使用 environs 庫(kù)定義環(huán)境變量的方法
這篇文章主要介紹了Python 使用 environs 庫(kù)來(lái)更好地定義環(huán)境變量,本節(jié)我們以 Python 項(xiàng)目為例,說(shuō)說(shuō)環(huán)境變量的設(shè)置。通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02
Python編程實(shí)現(xiàn)tail-n查看日志文件的方法
這篇文章主要介紹了Python編程實(shí)現(xiàn)tail-n查看日志文件的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
python網(wǎng)絡(luò)編程之?dāng)?shù)據(jù)傳輸U(kuò)DP實(shí)例分析
這篇文章主要介紹了python網(wǎng)絡(luò)編程之?dāng)?shù)據(jù)傳輸U(kuò)DP實(shí)現(xiàn)方法,實(shí)例分析了Python基于UDP協(xié)議的數(shù)據(jù)傳輸實(shí)現(xiàn)方法,需要的朋友可以參考下2015-05-05
Python任務(wù)調(diào)度模塊APScheduler使用
這篇文章主要介紹了Python任務(wù)調(diào)度模塊APScheduler使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04

