餅圖&圓環圖&箱線圖&棉棒圖&誤差棒圖&帶誤差棒的柱狀圖&子圖
- 函數及參數
- 簡單餅圖繪圖
-
- 清單裡的資料是小數時,按照小數所對應分數計算
- 清單裡的資料是整數時,按照百分比計算
- label(s)--标簽(餅圖外側顯示的說明文字)
- explode--與圓心的距離
- startangle--起始繪制角度(預設從x軸正方向逆時針畫起)
- shadow--在餅圖下面畫一個陰影。預設值為False
- labeldistance--label标簽的位置(與半徑成比例,預設值為1.1)
- autopct--控制餅圖内百分比設定
- pctdistance--指定autopct的位置刻度(餅圖百分比與圓心的距離,預設值為0.6)
- radius--餅圖半徑,預設值為1
- counterclock--指定指針方向(預設為True,逆時針)
- frame--繪制帶有表的軸架構
- textprops--設定标簽和比例文字的格式
- legend--圖例
-
- 預設設定
- bbox_to_anchor
- ncol
- borderaxespad--圖例的内邊距
- 圓環圖
- 箱線圖
-
- notch--是否是凹口的形式展現箱線圖,預設非凹口
- sym--指定異常點的形狀,預設為+号顯示
- 棉棒圖
-
- 棉棒的樣式--linefmt
- 棉棒末端的樣式--markerfmt
-
- markerfmt = 's'
- markerfmt = '*'
- 指定基線的樣式--basefmt
- bottom--設定基線位置
- 誤差棒圖
-
- fmt--資料點的标記樣式和資料點标記的連接配接樣式
- ecolor--誤差棒的線條顔色
- 誤差棒的線條粗細
- ms--資料點的大小
- mfc--資料點的顔色
- mec--資料點的邊緣顔色
- capsize--誤差棒邊界橫杠的大小
- capthick--誤差棒邊界橫杠的厚度
- 帶誤差棒的柱狀圖
- 子圖
函數及參數
參考文檔:官網說明文檔
屬性 | 說明 | 類型 |
---|---|---|
x | 資料 | list |
labels | 标簽 | list |
autopct | 資料标簽 | %0.1%% 保留一位小數 |
explode | 突出的部分 | list |
shadow | 是否顯示陰影 | bool |
pctdistance | 資料标簽的距離圓心位置 | 0~1 |
labeldistance | 标簽的比例 | float |
startangle | 開始繪圖的角度 | float |
radius | 半徑長 | 預設是1 |
簡單餅圖繪圖
清單裡的資料是小數時,按照小數所對應分數計算
x = [0.1,0.5,0.2]
plt.pie(x)
清單裡的資料是整數時,按照百分比計算
x = [1,5,2]
plt.pie(x)
label(s)–标簽(餅圖外側顯示的說明文字)
x = [1,5,2]
plt.pie(x,labels=["apple","orange","banana"])
explode–與圓心的距離
x = [1,5,2]
plt.pie(x,labels=["apple","orange","banana"],
explode=[0.1,0,0])
x = [1,5,2]
plt.pie(x,labels=["apple","orange","banana"],
explode=[1.5,0.5,0])
startangle–起始繪制角度(預設從x軸正方向逆時針畫起)
x = [1,5,2]
plt.pie(x,labels=["apple","orange","banana"],
startangle=30)
shadow–在餅圖下面畫一個陰影。預設值為False
x = [1,5,2]
plt.pie(x,labels=["apple","orange","banana"],
shadow=True)
labeldistance–label标簽的位置(與半徑成比例,預設值為1.1)
x = [1,5,2]
plt.pie(x,labels=["apple","orange","banana"],
shadow=True,
labeldistance=0.8)
x = [1,5,2]
plt.pie(x,labels=["apple","orange","banana"],
shadow=True,
labeldistance=0.5)
autopct–控制餅圖内百分比設定
’%1.1f’指小數點前後位數(沒有用空格補齊)
x = [1,5,2]
plt.pie(x,labels=["apple","orange","banana"],
shadow=True,
labeldistance=1.2,
autopct='%1.1f')
x = [1,5,2]
plt.pie(x,labels=["apple","orange","banana"],
shadow=True,
labeldistance=1.2,
autopct='%1.1f%%')
x = [1,5,2]
plt.pie(x,labels=["apple","orange","banana"],
shadow=True,
labeldistance=1.2,
autopct='%.0f%%')
pctdistance–指定autopct的位置刻度(餅圖百分比與圓心的距離,預設值為0.6)
x = [1,5,2]
plt.pie(x,labels=["apple","orange","banana"],
shadow=True,
labeldistance=1.2,
autopct='%.0f%%',
pctdistance=0.2)
x = [1,5,2]
plt.pie(x,labels=["apple","orange","banana"],
shadow=True,
labeldistance=1.2,
autopct='%0.f%%',
pctdistance=1.6)
radius–餅圖半徑,預設值為1
x = [1,5,2]
plt.pie(x,labels=["apple","orange","banana"],
radius=2)
counterclock–指定指針方向(預設為True,逆時針)
x = [1,5,2]
plt.pie(x,labels=["apple","orange","banana"],
counterclock=False)
frame–繪制帶有表的軸架構
x = [1,5,2]
plt.pie(x,labels=["apple","orange","banana"],
frame=True)
x = [1,5,2]
plt.pie(x,labels=["apple","orange","banana"],
frame=True)
plt.xlim(-1,1)
plt.ylim(-1,1)
plt.grid()
textprops–設定标簽和比例文字的格式
x = [1,5,2]
plt.pie(x,labels=["apple","orange","banana"],
textprops={'fontsize':20,'color':'blue'})
legend–圖例
預設設定
x = [1,5,2]
plt.pie(x,labels=["apple","orange","banana"],
textprops={'fontsize':20,'color':'blue'})
plt.axis("equal")#将餅圖顯示為正圓
plt.legend()
bbox_to_anchor
x = [1,5,2]
plt.pie(x,labels=["apple","orange","banana"],
textprops={'fontsize':20,'color':'blue'})
plt.axis("equal")#将餅圖顯示為正圓
plt.legend(loc="upper right",#位于右上角
fontsize=15, #圖例字型大小
bbox_to_anchor=(2,0.5),#外邊距:上邊&右邊,預設值[0.5,0.5]
borderaxespad=0.3,#圖例的内邊距
#ncol=2
)#ncol=2分兩列
當bbox_to_anchor=(1,1.05):
當bbox_to_anchor=(2,0.5):
ncol
x = [1,5,2]
plt.pie(x,labels=["apple","orange","banana"],
textprops={'fontsize':20,'color':'blue'})
plt.axis("equal")#将餅圖顯示為正圓
plt.legend(loc="upper right",#位于右上角
fontsize=15,
bbox_to_anchor=(1.1,1.05),#外邊距:上邊&右邊,預設值[0.5,0.5]
borderaxespad=0.3,#圖例的内邊距
ncol=2)#ncol=2分兩列
borderaxespad–圖例的内邊距
當borderaxespad=0.1:
當borderaxespad=4:
圓環圖
x = [1,5,2]
plt.pie(x,labels=["apple","orange","banana"],
textprops={'fontsize':20,'color':'blue'})
plt.pie([1],colors='w',radius=0.7)
箱線圖
由五個數值點組成:最小值、下四分位數、中位數、上四分位數、最大值。
下四分位數、中位數、上四分位數組成一個“帶有間隔的盒子”。
上四分位數到最大值之間建立一條延伸線,這個延伸線成為“胡須”。
參數 | 說明 |
---|---|
x | 指定要繪制箱線圖的資料 |
notch | 是否是凹口的形式展現箱線圖,預設非凹口 |
sym | 指定異常點的形狀,預設為+号顯示 |
vert | 是否需要将箱線圖垂直擺放,預設垂直擺放 |
whis | 指定上下須與上下四分位的距離,預設為1.5倍的四 分位差 |
positions | 指定箱線圖的位置,預設為[0,1,2…] |
widths | 指定箱線圖的寬度,預設為0.5 |
patch_ artist: | 是否填充箱體的顔色 |
meanline | 是否用線的形式表示均值,預設用點來表示 |
showmeans | 是否顯示均值,預設不顯示 |
showcaps | 是否顯示箱線圖頂端和末端的兩條線,預設顯示 |
showbox | 是否顯示箱線圖的箱體,預設顯示 |
showfliers | 是否顯示異常值,預設顯示 |
boxprops | 設定箱體的屬性,如邊框色,填充色等 |
labels | 為箱線圖添加标簽,類似于圖例的作用 |
filerprops | 設定異常值的屬性,如異常點的形狀、大小、填充色等 |
medianprops | 設定中位數的屬性,如線的類型、粗細等 |
meanprops | 設定均值的屬性,如點的大小、顔色等 |
capprops | 設定箱線圖頂端和末端線條的屬性,如顔色、粗細等 |
whiskerprops | 設定須的屬性,如顔色、粗細、線的類型等 |
data = [np.random.normal(0,std,100) for std in range(1,4)]
plt.boxplot(data)
notch–是否是凹口的形式展現箱線圖,預設非凹口
data = [np.random.normal(0,std,100) for std in range(1,4)]
plt.boxplot(data,notch=True)
sym–指定異常點的形狀,預設為+号顯示
data = [np.random.normal(0,std,100) for std in range(1,4)]
plt.boxplot(data,notch=True,
sym = '*',#異常點的形狀
)
plt.boxplot(data)
棉棒圖
x = np.linspace(0.5,2*np.pi,20)
y = np.random.randn(20)
plt.stem(x,y)
棉棒的樣式–linefmt
#棉棒圖
x = np.linspace(0.5,2*np.pi,20)
y = np.random.randn(20)
plt.stem(x,y,
linefmt = '-.'#棉棒的樣式
)
棉棒末端的樣式–markerfmt
#棉棒圖
x = np.linspace(0.5,2*np.pi,20)
y = np.random.randn(20)
plt.stem(x,y,
linefmt = '-.',#棉棒的樣式
markerfmt = 's',#棉棒末端的樣式
)
markerfmt = ‘s’
markerfmt = ‘*’
指定基線的樣式–basefmt
#棉棒圖
x = np.linspace(0.5,2*np.pi,20)
y = np.random.randn(20)
plt.stem(x,y,
linefmt = '-.',#棉棒的樣式
markerfmt = '*',#棉棒末端的樣式
basefmt = '--',#指定基線的樣式
)
bottom–設定基線位置
x = np.linspace(0.5,2*np.pi,20)
y = np.random.randn(20)
plt.stem(x,y,
linefmt = '-.',#棉棒的樣式
markerfmt = '*',#棉棒末端的樣式
basefmt = '--',#指定基線的樣式
bottom=1
)
誤差棒圖
x = np.linspace(-4,4,10)
y = np.sin(x)
plt.errorbar(x,y,yerr = 0.2,xerr = 0.2)
fmt–資料點的标記樣式和資料點标記的連接配接樣式
x = np.linspace(-4,4,10)
y = np.sin(x)
plt.errorbar(x,y,yerr = 0.2,xerr = 0.2,fmt='ro:')
ecolor–誤差棒的線條顔色
x = np.linspace(-4,4,10)
y = np.sin(x)
plt.errorbar(x,y,yerr = 0.2,xerr = 0.2,fmt='ro:',
ecolor='g')
誤差棒的線條粗細
x = np.linspace(-4,4,10)
y = np.sin(x)
plt.errorbar(x,y,yerr = 0.2,xerr = 0.2,fmt='ro:',
ecolor='g',
elinewidth = 5)
ms–資料點的大小
x = np.linspace(-4,4,10)
y = np.sin(x)
plt.errorbar(x,y,yerr = 0.2,xerr = 0.2,fmt='ro:',
ecolor='g',
elinewidth = 5,
ms = 10)
mfc–資料點的顔色
x = np.linspace(-4,4,10)
y = np.sin(x)
plt.errorbar(x,y,yerr = 0.2,xerr = 0.2,fmt='ro:',
ecolor='g',
elinewidth = 5,
ms=10,
mfc='orange')
mec–資料點的邊緣顔色
x = np.linspace(-4,4,10)
y = np.sin(x)
plt.errorbar(x,y,yerr = 0.2,xerr = 0.2,fmt='ro:',
ecolor='g',
elinewidth = 5,
ms=10,
mfc='orange',
mec='black')
capsize–誤差棒邊界橫杠的大小
x = np.linspace(-4,4,10)
y = np.sin(x)
plt.errorbar(x,y,yerr = 0.2,xerr = 0.2,fmt='ro:',
ecolor='g',
elinewidth = 5,
ms=10,
mfc='orange',
mec='black',
capsize=10)
capthick–誤差棒邊界橫杠的厚度
x = np.linspace(-4,4,10)
y = np.sin(x)
plt.errorbar(x,y,yerr = 0.2,xerr = 0.2,fmt='ro:',
ecolor='g',
elinewidth = 5,
ms=10,
mfc='orange',
mec='black',
capsize=10,
capthick=5)
帶誤差棒的柱狀圖
x = np.arange(5)
y = [66,88,55,99,123]
err = [5,9,6,10,7]
err_attri = dict(elinewidt=2,ecolor="black",capsize=3)
plt.bar(x,y,
color = 'b',
width = 0.6,
align = 'center',
yerr = err,
error_kw = err_attri)
子圖
plt.figure()
plt.subplot(2,2,1)
plt.plot([0,1],[0,1])
plt.subplot(2,2,2)
plt.plot([0,1],[1,3])
plt.subplot(223)
plt.plot([0,1],[0,4])
plt.subplot(224)
plt.plot([0,1],[0,8])
plt.figure()
plt.subplot(2,1,1)
plt.plot([0,1],[0,1])
plt.subplot(2,3,4)
plt.plot([0,1],[0,2])
plt.subplot(235)
plt.plot([0,1],[0,3])
plt.subplot(236)
plt.plot([0,1],[0,4])
ax1 = plt.subplot2grid((3,3),(0,0))
ax2 = plt.subplot2grid((3,3),(1,0))
ax3 = plt.subplot2grid((3,3),(0,2),rowspan=3)
ax4 = plt.subplot2grid((3,3),(2,0),colspan=2)
ax5 = plt.subplot2grid((3,3),(0,1),rowspan=2)
import numpy as np
ax1 = plt.subplot2grid((3,3),(0,0))
ax2 = plt.subplot2grid((3,3),(1,0))
ax3 = plt.subplot2grid((3,3),(0,2),rowspan=3)
ax4 = plt.subplot2grid((3,3),(2,0),colspan=2)
ax5 = plt.subplot2grid((3,3),(0,1),rowspan=2)
x = np.linspace(0,10,100)
y = np.exp(x)
ax1.plot(x,y)