概述
nofollow">matplotlib画饼状图
1. 图中加标注
plt
import numpy as np
x=np.linspace(-1,1,10)
y=x**2
fig=plt.figure(figsize=(8,4))
ax=plt.subplot(111)
plt.plot(x,y)
for i,(_x,_y) in enumerate(zip(x,y)):
plt.text(_x,_y,i,color='red',fontsize=i+10)
plt.text(0.5,0.8,'subplot words',color='blue',ha='center',transform=ax.trans Axes)
plt.figtext(0.1,0.92,'figure words',color='green')
plt.annotate('buttom',xy=(0,0),xytext=(0.2,0.2),arrowprops=dict(facecolor='blue',shrink=0.05))
plt.show()
<p style="text-align:center;">
plt
import numpy as np
dict = {'A': 40,'B': 70,'C': 30,'D': 85}
for i,key in enumerate(dict):#Circulate both index and value(Here is key)
plt.bar(i,dict[key],color='r',width=0.2)
plt.xticks(np.arange(len(dict))+0.1,dict.keys())#Translation
plt.yticks(dict.values())
plt.grid(True)
plt.show()
3. colormap图
<p style="text-align:center;">
'+fn) #fn='/d3/MWRT/R20130805/F06925_EMS60.txt' data=wlab.dlmread(fn) EMS=EMS+list(data[:,1])#地表发射率 LST=LST+list(data[:,2])#温度 TBH=TBH+list(data[:,8])#水平亮温 TBV=TBV+list(data[:,9])#垂直亮温 #----------------------------------------------------------- #生成格点数据,利用griddata插值 grid_x,grid_y = np.mgrid[275:315:1,0.60:0.95:0.01] grid_z = griddata((LST,EMS),TBH,(grid_x,grid_y),method='cubic') #将横纵坐标都映射到(0,1)的范围内 extent=(0,1) #指定colormap cmap = matplotlib.cm.jet #设定每个图的colormap和colorbar所表示范围是一样的,即归一化 norm = matplotlib.colors.Normalize(vmin=160,vmax=300) #显示图形,此处没有使用contourf #>>>ctf=plt.contourf(grid_x,grid_y,grid_z) gci=plt.imshow(grid_z.T,extent=extent,origin='lower',cmap=cmap,norm=norm) #配置一下坐标刻度等 ax=plt.gca() ax.set_xticks(np.linspace(0,9)) ax.set_xticklabels( ('275','280','285','290','295','300','305','310','315')) ax.set_yticks(np.linspace(0,8)) ax.set_yticklabels( ('0.60','0.65','0.70','0.75','0.80','0.85','0.90','0.95')) #显示colorbar cbar = plt.colorbar(gci) cbar.set_label('$T_B(K)$',fontdict=font) cbar.set_ticks(np.linspace(160,300,8)) cbar.set_ticklabels( ('160','180','200','220','240','260','300')) #设置label ax.set_ylabel('Land Surface Emissivity',fontdict=font) ax.set_xlabel('Land Surface Temperature(K)',fontdict=font) #陆地地表温度LST #设置title titleStr='$T_B$ for Freq = '+str(float(fp[1:-1])*0.01)+'GHz' plt.title(titleStr) figname=fp+'.png' plt.savefig(figname) plt.clf()#清除图形
plt.show()
print('ALL -> Finished OK')
print('ALL -> Finished OK')
print('ALL -> Finished OK')
4. 饼状图
40376
Germany 3099080
Russia 2383402
Brazil 2293954
UK 2260803
France 2217900
Italy 1846950
import matplotlib.pyplot as plt
quants: GDP
labels: country name
labels = []
quants = []Read data
for line in file('../data/major_country_gdp'):
info = line.split()
labels.append(info[0])
quants.append(float(info[1]))make a square figure
For China,make the piece explode a bit
def explode(label,target='China'):
if label == target: return 0.1
else: return 0
expl = map(explode,labels)Colors used. Recycle if not enough.
colors = ["pink","coral","yellow","orange"]
Pie Plot
autopct: format of "percent" string;
plt.pie(quants,explode=expl,colors=colors,labels=labels,autopct='%1.1f%%',pctdistance=0.8,shadow=True)
plt.title('Top 10 GDP Countries',bBox={'facecolor':'0.8','pad':5})
plt.show()
labels = []
quants = []
for line in file('../data/major_country_gdp'):
info = line.split()
labels.append(info[0])
quants.append(float(info[1]))
def explode(label,target='China'):
if label == target: return 0.1
else: return 0
expl = map(explode,labels)
colors = ["pink","coral","yellow","orange"]
plt.pie(quants,explode=expl,colors=colors,labels=labels,autopct='%1.1f%%',pctdistance=0.8,shadow=True)
plt.title('Top 10 GDP Countries',bBox={'facecolor':'0.8','pad':5})
plt.show()
labels = []
quants = []
for line in file('../data/major_country_gdp'):
info = line.split()
labels.append(info[0])
quants.append(float(info[1]))
def explode(label,target='China'):
if label == target: return 0.1
else: return 0
expl = map(explode,labels)
colors = ["pink","coral","yellow","orange"]
plt.pie(quants,explode=expl,colors=colors,labels=labels,autopct='%1.1f%%',pctdistance=0.8,shadow=True)
plt.title('Top 10 GDP Countries',bBox={'facecolor':'0.8','pad':5})
plt.show()
总结
以上是编程之家为你收集整理的matplotlib画图全部内容,希望文章能够帮你解决matplotlib画图所遇到的程序开发问题。
如果您也喜欢它,动动您的小指点个赞吧