pyplot饼图的绘制
import matplotlib.pyplot as plt
labels='Frogs','Hogs','Dogs','Logs'
sizes=[15,30,45,10]
explode=(0,0.1,0,0) #第二个突出显示
plt.pie(sizes,explode=explode,labels=labels,autopct='%1.1f%%',shadow=False,startangle=90)
plt.show()
加入如下语句可使图像变为正圆
plt.axis=('equal')
pyplot直方图的绘制
import matplotlib.pyplot as plt
import numpy as np
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)
#第二个参数bin,直方图的个数
plt.title('Histogram')
plt.show()
plt.hist参数:
arr: 需要计算直方图的一维数组
bins: 直方图的柱数,可选项,默认为10
normed: 是否将得到的直方图向量归一化。默认为0
facecolor: 直方图颜色
edgecolor: 直方图边框颜色
alpha: 透明度
histtype: 直方图类型,‘bar’, ‘barstacked’, ‘step’, ‘stepfilled’
pyplot极坐标图的绘制
import matplotlib.pyplot as plt
import numpy as np
N=20
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=ax.bar(theta,radii,width=width,bottom=0.0)
for r,bar in zip(radii,bars):
bar.set_facecolor(plt.cm.virdis(r/10.))
bar.set_alpha(0.5)
plt.show()
面向对象绘制极坐标
import matplotlib.pyplot as plt
import numpy as np
N=10
theta=np.linspace(0.0,2*np.pi,N,endpoint=False)
radii=10*np.random.rand(N)
width=np.pi/2*np.random.rand(N)
ax=plt.subplot(111,projection='polar')
bars=ax.bar(theta,radii,width=width,bottom=0.0)
for r,bar in zip(radii,bars):
bar.set_facecolor(plt.cm.virdis(r/10.))
bar.set_alpha(0.5)
plt.show()
pyplot散点图绘制
import matplotlib.pyplot as plt
import numpy as np
fig,ax=plt.subplots()
ax.plot(10*np.random.randn(100),10*np.random.randn(100),'o')
ax.set_title('Simple Scatter')
plt.show()