日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

python使用matplotlib繪制柱狀圖教程

系統 1948 0

Matplotlib的概念這里就不多介紹了,關于 繪圖庫Matplotlib的安裝方法: 點擊這里

小編之前也和大家分享過python使用matplotlib實現的折線圖和制餅圖效果,感興趣的朋友們也可以點擊查看,下面來看看python使用matplotlib繪制柱狀圖的方法吧,具體如下:

1. 基本的柱狀圖

            
import matplotlib.pyplot as plt

data = [5, 20, 15, 25, 10]

plt.bar(range(len(data)), data)
plt.show()
          

python使用matplotlib繪制柱狀圖教程_第1張圖片

plt.bar函數簽名為:

            
bar(left, height, width=0.8, bottom=None, **kwargs)
          

事實上,left,height,width,bottom這四個參數確定了柱體的位置和大小。默認情況下,left為柱體的居中位置(可以通過align參數來改變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()
          

python使用matplotlib繪制柱狀圖教程_第2張圖片

2. 設置柱體樣式

(1)顏色

通過facecolor(或fc)關鍵字參數可以設置柱體顏色,例如:

            
import matplotlib.pyplot as plt

data = [5, 20, 15, 25, 10]

plt.bar(range(len(data)), data, fc='g')
plt.show()
          

python使用matplotlib繪制柱狀圖教程_第3張圖片

通過color關鍵字參數 可以一次性設置多個顏色,例如:

            
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()
          

python使用matplotlib繪制柱狀圖教程_第4張圖片

(2)描邊

相關的關鍵字參數為:

  • 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()
          

python使用matplotlib繪制柱狀圖教程_第5張圖片

(3)填充

hatch關鍵字可用來設置填充樣式,可取值為:/, \, |, -, +, 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. 設置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()
          

python使用matplotlib繪制柱狀圖教程_第7張圖片

4. 堆疊柱狀圖

通過bottom參數,可以繪制堆疊柱狀圖。例如:

            
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()
          

python使用matplotlib繪制柱狀圖教程_第8張圖片

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()
          

python使用matplotlib繪制柱狀圖教程_第9張圖片

6. 條形圖

使用barh方法繪制條形圖。例如:

            
import matplotlib.pyplot as plt

data = [5, 20, 15, 25, 10]

plt.barh(range(len(data)), data)
plt.show()
          

python使用matplotlib繪制柱狀圖教程_第10張圖片

plt.barh方法的簽名為:

            
barh(bottom, width, height=0.8, left=None, **kwargs)
          

可以看到與plt.bar方法類似。因此堆積條形圖和并列條形圖的畫法與前面類似,不做贅述。

7. 正負條形圖

            
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()
          

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家學習或者使用python能帶來一定的幫助,如果有疑問大家可以留言交流。


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 文成县| 望江县| 泌阳县| 德江县| 安平县| 兴海县| 鄂尔多斯市| 西盟| 余庆县| 大冶市| 文成县| 长沙县| 天峨县| 望都县| 常熟市| 萨迦县| 崇信县| 尚志市| 汕头市| 河间市| 晋中市| 隆昌县| 中阳县| 五台县| 基隆市| 会昌县| 邻水| 犍为县| 阿拉善左旗| 烟台市| 东台市| 班玛县| 大竹县| 泰顺县| 沙洋县| 山东| 大竹县| 庄河市| 五家渠市| 博野县| 安仁县|