App下載

Python----數(shù)據(jù)可視化之matplotlib餅圖、柱狀圖

猿友 2020-12-30 14:02:17 瀏覽數(shù) (3181)
反饋

matplotlib 庫除了可以繪制曲線圖, 還可以繪制統(tǒng)計(jì)圖形,這篇文章介紹餅圖、柱狀圖等幾種常用統(tǒng)計(jì)圖形的繪制方法。 推薦好課:Python 自動(dòng)化辦公、用Python自動(dòng)辦公做職場(chǎng)高手

1、餅圖

使用 pie() 方法繪制餅圖: 

import matplotlib.pyplot as plt


print('\n-----歡迎來到w3cschool.cn')  

plt.rc('font',family='Arial',size='9')

plt.rc('axes',unicode_minus='False')

labels = ['Strawberry', 'Apple', 'Banana', 'Pear', 'Orange']

sizes = [39, 20, 55, 30,25] # 每個(gè)元素的值,會(huì)自動(dòng)根據(jù)該值計(jì)算百分比

explode = [0.1, 0.2, 0, 0, 0]  # 每個(gè)元素的膨脹距離,這里指定了第0和第1個(gè)

 

fig, ax = plt.subplots()

ax.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%', shadow=True, startangle=0) 

# autopct 精度 startangle 第1個(gè)元素的起始角位置,其他元素逆時(shí)針方向組織,shadow 是否使用陰影

ax.axis('scaled')  #設(shè)置餅圖的樣式,設(shè)置為equals顯示的會(huì)是圓形    

fig.savefig('matplot-basic-pie.jpg')

plt.show()

效果如圖所示:

111


ax.pie() 方法參數(shù):

  • sizes:各元素的絕對(duì)數(shù)值大小,相對(duì)百分比會(huì)根據(jù)這些值計(jì)算;
  • explode:各個(gè)部分向外彈出的值;
  • autopct:百分比的顯示精度;
  • shadow:是否顯示陰影;
  • startangle:起始元素的位置,就是表示labels[0]的起始角位置,剩下的元素會(huì)逆時(shí)針方向組織。

2、柱狀圖

使用bar()方法繪制柱狀圖:

import matplotlib.pyplot as plt

import numpy as np


print('\n-----歡迎來到w3cschool.cn')  

plt.rc('font',family='Arial',size='9')

plt.rc('axes',unicode_minus='False')

    

fig, ax = plt.subplots()

fruit = ('Banana', 'Strawberry', 'Watermelon', 'Apple', 'Papaya', 'Tomatoes')

weight = (100,135,50,83,92,66)

ax.bar(fruit, weight, align='center',width=0.7)

ax.set_ylabel('Weight')#設(shè)置x軸標(biāo)簽

ax.set_title('Histogram')

fig.savefig('matplot-bar.jpg')

plt.show()

效果如圖所示:


下載

bar()參數(shù):

  • 第1個(gè)固定參數(shù):x坐標(biāo)點(diǎn)名稱
  • 第2個(gè)固定參數(shù):y坐標(biāo)值
  • align:對(duì)齊方式;
  • width:柱寬度;

3、水平柱狀圖

使用barh()方法繪制水平柱狀圖:

import matplotlib.pyplot as plt

import numpy as np


print('\n-----歡迎來到w3cschool.cn')   

plt.rc('font',family='Arial',size='9')

plt.rc('axes',unicode_minus='False')

    

fig, ax = plt.subplots()

fruit = ('Banana', 'Strawberry', 'Watermelon', 'Apple', 'Papaya', 'Tomatoes')

y_pos = np.arange(len(fruit))

weight = (100,135,50,83,92,66)

ax.barh(y_pos, weight, align='center',height=0.7)

ax.set_yticks(y_pos)#設(shè)置y軸坐標(biāo)

ax.set_yticklabels(fruit)#設(shè)置y軸標(biāo)簽

ax.invert_yaxis()  # 設(shè)置標(biāo)簽從上到下,更符合閱讀習(xí)慣

ax.set_xlabel('weight')#設(shè)置x軸標(biāo)簽

ax.set_title('Horizontal bar chart')

fig.savefig('matplot-hor-bar.jpg')

plt.show()

效果如圖所示:

下載 (1)

barh() 方法參數(shù):

  • 第1個(gè)固定參數(shù):y軸坐標(biāo);
  • 第2個(gè)固定參數(shù):寬度值,實(shí)際上對(duì)應(yīng)的是x軸的長(zhǎng)度;
  • align:對(duì)齊方法,可選center和edge,表示柱圖的位置和對(duì)應(yīng)y軸坐標(biāo)的關(guān)系;
  • height:柱圖y方向的高度

ax.invert_yaxis()表示將y坐標(biāo)反轉(zhuǎn),這樣更符合閱讀習(xí)慣,第0個(gè)元素在最上方顯示。

4、分組柱狀圖

分組柱狀圖就是柱狀圖的組合形式,實(shí)際是2個(gè)柱狀圖合并在一起顯示:

import matplotlib.pyplot as plt

import numpy as np

 

print('\n-----歡迎來到w3cschool.cn')  

plt.rc('font',family='Arial',size='9')

plt.rc('axes',unicode_minus='False')

fruit = ('Banana', 'Strawberry', 'Watermelon', 'Apple', 'Papaya', 'Tomatoes')

weight = (100,135,50,83,92,66)

count = (20,15,30,53,22,36)

x = np.arange(len(fruit))

    

fig, ax = plt.subplots()

width = 0.4

ax.bar(x-width/2, weight,  width=width,label='weight')

ax.bar(x+width/2, count,  width=width,label='number')   

 

ax.set_title('Grouping histogram')

ax.set_ylabel('Weight / number')#設(shè)置y軸標(biāo)簽

ax.set_xticks(x)          #設(shè)置x軸坐標(biāo)值

ax.set_xticklabels(fruit) #設(shè)置x軸坐標(biāo)標(biāo)簽

fig.savefig('matplot-bar-group.jpg')

ax.legend()               #顯示圖例

plt.show()

效果如圖所示:


下載 (2)

bar()入?yún)⒌牡?個(gè)參數(shù)不是直接使用分類形式的tuple,而是根據(jù)分類的數(shù)量重新定義的numpy數(shù)組,這樣2個(gè)柱形圖就可以根據(jù)該數(shù)值加減寬度的一半實(shí)現(xiàn)并排放置。

用法類似pyplot.plot(),多了個(gè)參數(shù)where表示劃線階梯在該點(diǎn)的前中后哪個(gè)位置,可以是’pre’,’mid’,’post’等三種類型,默認(rèn)’pre’。


0 人點(diǎn)贊