%matplotlib inline
import matplotlib.pyplot as plt
1.pie簡單參數:plt.pie(x, explode=None, labels=None……)javascript
屬性 | 說明 | 類型 |
---|---|---|
x | 數據 | list |
labels | 標籤 | list |
autopct | 數據標籤 | %0.1%% 保留一位小數 |
explode | 突出的部分,分離程度 | list |
shadow | 是否顯示陰影 | bool |
pctdistance | 數據標籤的距離圓心位置 | 0~1 |
labeldistance | 標籤的比例 | float |
startangle | 開始繪圖的角度 | float |
radius | 半徑長 | 默認是1 |
m=51212.
f=40742
m_perc=m/(m+f)
f_perc=f/(m+f)
colors=['navy','lightcoral']
labels=["Male","Female"]
plt.figure(figsize=(5,5))#可控制圓的大小
paches,texts,autotexts=plt.pie([m_perc,f_perc],labels=labels,autopct='%1.1f%%',explode=[0,0.05],colors=colors)#畫pie圖
for text in texts+autotexts:
text.set_fontsize(20)#設置字體大小
for text in autotexts:
text.set_color('white')#設置字體顏色
2.設置子圖佈局css
#3行3列的子圖在0行0列的位置
ax1=plt.subplot2grid((3,3),(0,0))
ax2=plt.subplot2grid((3,3),(1,0))
ax3=plt.subplot2grid((3,3),(0,2),rowspan=3)#rowspan:在當前位置佔用了幾行
ax4=plt.subplot2grid((3,3),(2,0),colspan=2)#colspan:在當前位置佔用了幾列
ax5=plt.subplot2grid((3,3),(0,1),rowspan=2)
3.嵌套圖html
import numpy as np
x=np.linspace(0,10,1000)
y2=np.sin(x**2)
y1=x**2
fig,ax1=plt.subplots()#指定外層圖
left,bottom,width,height=[0.22,0.45,0.3,0.35]#中間子圖嵌套的位置
ax2=fig.add_axes([left,bottom,width,height])#加上子圖
ax1.plot(x,y1)
ax2.plot(x,y2)
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
#定義柱狀圖的標籤位置定位函數
def autolabel(rects):
for rect in rects:
height=rect.get_height()
ax1.text(rect.get_x()+rect.get_width()/2,1.02*height,
"{:,}".format(float(height)),
ha='center',va='bottom',fontsize=18)
#導入數據
top10_arrivals_countries=['CANADA','MWXICO','UNITED\nKINGDOM',\
'JAPAN','CHIAN','GERMANY','SOUTH\nKOREA',\
'FRANCE','BRAZIL','AUSTRALIA']
top10_arrivals_values=[16.6232,15.3245,3.1534,2.9954,\
2.6454,1.2545,1.6425,1.4253,\
1.3225,1.1354]
arrivals_countries=['WESTERN\nEUROPE','ASIA','SOUTH\nAMERICA',\
'OCEANIA','CARIBBEAN','MIDDLE\nEAST',\
'CENTEAL\nAMERICA','EASTERN\nEUROPE','AFRICA']
arrivals_percent=[36.9,30.4,13.8,4.4,4.0,3.6,2.9,2.6,1.5]
fig,ax1=plt.subplots(figsize=(20,12))#指定外層圖
labell=ax1.bar(range(10),top10_arrivals_values,color='blue')#柱狀圖1
#寫註釋,柱狀圖
plt.xticks(range(10),top10_arrivals_countries,fontsize=10)
#畫子圖
ax2=inset_axes(ax1,width=6,height=6,loc=5)#添加子圖小圖,loc是具體位置
explode=(0.08,0.08,0.05,0.05,0.08,0.08,0.04,0.04,0.05)#縫隙距離
patchea,texts,autotexts=ax2.pie(arrivals_percent,labels=arrivals_countries,autopct='%1.1f%%',explode=explode)
#設置字體大小,子圖
for text in texts+autotexts:
text.set_fontsize(16)
#設置軸位置spines
for spine in ax1.spines.values():
spine.set_visible(False)#指定下面軸不可見
#柱狀圖label標註數據
autolabel(labell)