Matplotlib.pyplot库
程序员文章站
2022-03-19 15:57:16
...
1、pyplot饼状图的绘制 plt.pie()
推荐阅读:
matplotlib 知识点11:绘制饼图(pie 函数精讲)
#plt.pie() 饼图
#import matplotlib.pyplot as plt
#labels='Frogs','Hogs','Dogs','Logs'
#sizes=[15,30,45,10]
#explode=(0,0.2,0,0)
#plt.pie(sizes,explode=explode,labels=labels,autopct='%1.1f%%',shadow=False,startangle=90)
#plt.show()
#变式
import matplotlib.pyplot as plt
labels='Frogs','Hogs','Dogs','Logs'
sizes=[15,30,45,10]
explode=(0,0.2,0,0)
colors = ['r','g','y','b']
#plt.pie(sizes,explode=explode,labels=labels,autopct='%1.1f%%',shadow=False,startangle=90,colors=colors)
#plt.pie(sizes,explode=explode,labels=labels,autopct='%1.1f%%',shadow=True,startangle=90)
#plt.pie(sizes,explode=explode,labels=labels,autopct='%1.1f%%',shadow=False,startangle=90,labeldistance=0.7)
plt.pie(sizes,explode=explode,labels=labels,autopct='%1.1f%%')
plt.legend(loc='upper right')
plt.axis('equal')
plt.show()
2、pyplot直方图的绘制 plt.hist()
recommand
matplotlib可视化篇hist()–直方图
#import numpy as np
#import matplotlib.pyplot as plt
#np.random.seed(0)
#mu,sigma=100,20 #均值和方差
#a=np.random.normal(mu,sigma,size=100)
#plt.hist(a,20,normed=1,histtype='stepfilled',facecolor='b',alpha=0.75)
#plt.title('HISTOGRAM')
#plt.show()
#变式
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(0)
mu,sigma=100,20 #均值和方差
a=np.random.normal(mu,sigma,size=100)
#plt.hist(a,20,normed=1,histtype='bar',facecolor='y',alpha=0.5)
plt.hist(a,20,normed=1,histtype='bar',facecolor='y',align='left')
plt.title('HISTOGRAM')
plt.show()
3、pyplot极坐标图的绘制
面向对象绘制极坐标
emmm,这个列子用的是plt.bar()的参数,产生随机数绘图,重点是理解吧
import numpy as np
import matplotlib.pyplot as plt
N=10
theta=np.linspace(0.0,2*np.pi,N,endpoint=False)
radii=10*np.random.rand(N)
width=np.pi/4*np.random.rand(N)
ax=plt.subplot(111,projection='polar')
bars=plt.bar(theta,radii,width=width,bottom=0.0)
for r,bar in zip(radii,bars):
bar.set_facecolor(plt.cm.viridis(r/10.))
bar.set_alpha(0.5)
plt.show()
radii和width分别对应plt.bar()中的height和width
zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表
4、pyplot散点图的绘制
import numpy as np
import matplotlib.pyplot as plt
fig,ax=plt.subplots()
ax.plot(10*np.random.randn(100),10*np.random.randn(100),'o')
ax.set_title('simple scatter')
plt.show()
上一篇: 案例-旋转木马(CSS3)
下一篇: 学python能找什么工作