plt.scatter(x,y,s=100,c=‘r’,marker=’<’,alpha=‘0.5’)
x=np.random.randn(1000)
y1=np.random.randn((1000))
plt.scatter(x,y1,s=100,c='r',marker='<',alpha='0.5')
plt.show()
plot(x,y,‘格式’)
#linspace为生成等区间的数值
x=np.linspace(-10,10,20)
y=x**2
plt.plot(x,y,color='green', linestyle='dashed', marker='<',
markerfacecolor='blue', markersize=12)
plt.savefig('折线图')
plt.show()
垂直方向:
plt.bar(x,height,color,edgecolor,width=0.8,bottom=None,align=‘center’)
水平方向:
plt.bar(x=0,height,color,edgecolor,width=0.8,bottom,align=‘center’,orientation=‘horizontal’)
示例 1:普通条形图
N=5
y=[20,10,30,25,15]
index = np.arange(N)
p1 = plt.bar(x=index, height=y,width=0.5,bottom=100,color='red')
plt.savefig('条形图')
plt.show()
index = np.arange(4)
sales_BJ=[52,55,63,53]
sales_SH=[44,66,55,41]
bar_width=0.3
plt.bar(index,sales_BJ,bar_width,color='b')
plt.bar(index+bar_width,sales_SH,bar_width,color='r')
plt.savefig('并列条形图')
plt.show()
plt.bar(index,sales_BJ,bar_width,color='b')
plt.bar(index,sales_SH,bar_width,color='r',bottom=sales_BJ)
plt.savefig('层叠条形图')
plt.show()
plt.hist(x,bins=10,color=‘red’,normed=True)
示例 1:标准化直方图,返回 y 轴为x频率
mu = 100 # mean of distribution
sigma = 20 # standard deviation of distribution
x = mu + sigma * np.random.randn(2000)
plt.savefig('直方图')
plt.hist(x, bins=10,color='red',normed=True)
plt.hist(x, bins=50,color='green',normed=False)
plt.savefig('非标准化直方图')
plt.show()
plt.pie(x,y,height,color,edgecolor,width=0.8,bottom=None,align=“center”)
labels = 'A', 'B', 'C', 'D'
fracs = [15, 30, 45, 10]
explode = (0, 0.05, 0, 0)
plt.axes(aspect=1)
plt.pie(fracs, explode=explode, labels=labels, autopct='%.0f%%', shadow=True)
plt.savefig('饼状图')
plt.show()
plt.boxplot(data,sym=‘o’,whis=1.5)
np.random.seed(100)
data = np.random.normal(size=1000, loc=0.0, scale=1.0)
plt.boxplot(data,sym='o',whis=1.5)
plt.savefig('箱型图')
plt.show()
y=np.arrange(1,5)
plt.plot(y,color='0.5')
plt.show()
y=np.arrange(1,5)
plt.plot(y,color='#FF00FF')
plt.show()
y=np.arrange(1,5)
plt.plot(y,color=(0.1,0.2,0.3))
plt.show()
不同点形状默认使用不同颜色
可以将颜色、点型、线型写成一个字符串,如:
fig=plt.figure()
plt.subplot(参数1参数2参数3)
plt.plot(x,x)
x=np.arange(1,100)
plt.subplot(221)
plt.plot(x,x)
plt.subplot(222)
plt.plot(x,-x)
plt.subplot(223)
plt.plot(x,x*x)
plt.subplot(224)
plt.plot(x,np.log(x))
plt.show()
import matplotlib.pyplot as plt
fig1=plt.figure()
ax1=fig1.add_subplot(111)
ax1.plot([1,2,3],[3,2,1])
fig2=plt.figure()
ax2=fig2.add_subplot(111)
ax2.plot([1,2,3],[1,2,3])
plt.show()
y=np.arange(1,5)
plt.plot(y,y*2)
plt.grid(True,color='g',linestyle='-',linewidth='2')
plt.savefig('网格2')
plt.show()
x=np.arange(1,11,1)
y=x*x
plt.plot(x,x*2,label='Normal')
plt.plot(x,x*3,label='Fast')
plt.plot(x,x*4,label='Faster')
plt.legend(loc=2,ncol=3)
plt.show()
plt.plot(x,x*2)
plt.plot(x,x*3)
plt.plot(x,x*4)
plt.legend(['Normal','Fast','Faster'])
plt.savefig('图例1')
plt.show()
x=np.arange(0,10,1)
y=np.random.randn(len(x))
fig=plt.figure()
ax=fig.add_subplot(111)
l,=plt.plot(x,y)
ax.legend(['ax legend'])
line, =ax.plot(x,y,label='Inline label')
line.set_label('label via method')
ax.legend()
plt.show()
语法:
语法:
x=np.arange(2,20,1)
y1=x*x
y2=np.log(x)
plt.plot(x,y1)
plt.twinx()
plt.plot(x,y2,color='c')
plt.savefig('双坐标轴1')
plt.show()
x=np.arange(2,20,1)
y1=x*x
y2=np.log(x)
fig=plt.figure()
ax1=fig.add_subplot(111)
ax1.plot(x,y1)
ax1.set_ylabel('Y1')
ax2=ax1.twinx()
ax2.plot(x,y2,color='y')
ax2.set_ylabel('Y2')
plt.savefig('双坐标轴2')
plt.show()
x=np.arange(-10,11,1)
y=x*x
plt.plot(x,y)
plt.annotate('this is the bottom',xy=(0,1),xytext=(0,20),
arrowprops=dict(facecolor='g',frac=0.2,headwidth=20,width=5))
plt.savefig('添加注释')
plt.show()
x=np.arange(-10,11,1)
y=x*x
plt.plot(x,y)
plt.text(0,40,'function:y=x*x',
family='fantasy',size=15,
color='k',style='italic',weight=1000,
bbox=dict(facecolor='r',alpha=0.2))
plt.savefig('添加文字')
plt.show()
fig=plt.figure()
ax=fig.add_subplot(111)
ax.set_xlim([1,7])
ax.set_ylim([1,5])
ax.text(2,4,r'$ \alpha_i \beta_j \pi \lambda \omega$',size=15)
ax.text(5,4,r'$ \sin(0)=\cos(\frac{\pi}{2})$',size=15)
ax.text(2,2,r'$ \lim_{x\rightarrow y}\frac{1}{x^3}$',size=15)
ax.text(5,2,r'$ \sqrt[4]{x}=\sqrt{y}$',size=15)
plt.savefig('Tex公式')
plt.show()
x=np.linspace(0,5*np.pi,1000)
y1=np.sin(x)
y2=np.sin(2*x)
plt.fill(x,y1,'r',alpha=0.2)
plt.fill(x,y2,'b',alpha=0.2)
plt.savefig('区域填充')
plt.show()
x=np.linspace(0,5*np.pi,1000)
y1=np.sin(x)
y2=np.sin(2*x)
fig=plt.figure()
ax=plt.gca()
ax.plot(x,y1,x,y2,color='black')
ax.fill_between(x,y1,y2,facecolor='blue',alpha=0.3)
plt.savefig('区域填充1')
plt.show()
内置美化函数:plt.style.use('ggplot)
style中可用参数有:
‘seaborn-dark’,
‘seaborn-darkgrid’,
‘seaborn-ticks’,
‘fivethirtyeight’,
‘seaborn-whitegrid’,
‘classic’,
‘_classic_test’,
‘fast’,
‘seaborn-talk’,
‘seaborn-dark-palette’,
‘seaborn-bright’,
‘seaborn-pastel’,
‘grayscale’,
‘seaborn-notebook’,
‘ggplot’,
‘seaborn-colorblind’,
‘seaborn-muted’,
‘seaborn’,
‘Solarize_Light2’,
‘seaborn-paper’,
‘bmh’,
‘tableau-colorblind10’,
‘seaborn-white’,
‘dark_background’,
‘seaborn-poster’,
‘seaborn-deep’
plt.style.use('ggplot')
fig, axes=plt.subplots(ncols=2,nrows=2)
ax1,ax2,ax3,ax4=axes.ravel()
x,y=np.random.normal(size=(2,100))
ax1.plot(x,y,'o')
s=1
x=np.arange(0,10)
y1=np.arange(0,10)
y2=np.arange(s,10+s)
y3=np.arange(2*s,10+2*s)
y4=np.arange(3*s,10+3*s)
y5=np.arange(4*s,10+4*s)
ax2.plot(x,y1,'-')
ax2.plot(x,y2,'-')
ax2.plot(x,y3,'-')
ax2.plot(x,y4,'-')
ax2.plot(x,y5,'-')
x=np.arange(5)
y1,y2,y3=np.random.randint(1,25,size=(3,5))
width=0.25
ax3.bar(x,y1,width)
ax3.bar(x+width,y2,width)
ax3.bar(x+2*width,y3,width)
xy=np.random.normal(size=2)
ax4.add_patch(plt.Circle(xy,radius=0.3))
ax4.axis('equal')
plt.savefig('美化')
plt.show()