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方法類似。因此堆積條形圖和并列條形圖的畫(huà)法與前面類似,不做贅述。
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)大家可以留言交流。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元

