Gointerface接口声明实现及作用详解
331
2022-08-26
Python进阶—Matplotlib(Python爬虫:入门+进阶)
Matplotlib再来一次 文章目录一、基础用法二、figure图像三、设置坐标轴四、legend图例五、标注六、散点图七、直方图八、等高线图九、3D图十、subplot十一、动态图 配合 机器学习食用更佳。
一、基础用法
画直线
import matplotlib.pyplot as pltimport numpy as np%matplotlib inline
x = np.linspace(-1,1,100)#从-1到1生成100个点y = 2*x + 1plt.plot(x,y)plt.show()
二、figure图像
不同图像在不同的figure中改变图像大小plt.plot(x,y2,color=‘blue’,linewidth=5.0,linestyle=’-’)
x = np.linspace(-1,1,100)y1 = 2*x+1y2 = x ** 2plt.figure()plt.plot(x,y1)plt.figure(figsize=(8,5)) # 改变图像大小plt.plot(x,y2)plt.show()
x = np.linspace(-1,1,100)y1 = 2 *x + 1y2 = x ** 2plt.plot(x,y1,color='red',linewidth=1.0,linestyle='--')plt.plot(x,y2,color='blue',linewidth=5.0,linestyle='-')
[
三、设置坐标轴
xlim、ylim限制范围xlabel、ylabel描述xticks、yticks 修改坐标范围或者类型画坐标图
x = np.linspace(-3,3,100)y1 = 2 *x + 1y2 = x ** 2# xy范围plt.xlim((-1,2))plt.ylim((-2,3))# xy描述plt.xlabel('I AM X')plt.ylabel('I AM Y')plt.plot(x,y1,color='red',linewidth=1.0,linestyle='--')plt.plot(x,y2,color='blue',linewidth=5.0,linestyle='-')
[
new_ticks = np.linspace(-2,2,11)print(new_ticks)
[-2. -1.6 -1.2 -0.8 -0.4 0. 0.4 0.8 1.2 1.6 2. ]
x = np.linspace(-3,3,100)y1 = 2 *x + 1y2 = x ** 2# xy范围plt.xlim((-1,2))plt.ylim((-2,3))# xy描述plt.xlabel('I AM X')plt.ylabel('I AM Y')plt.xticks(new_ticks)plt.yticks([-2,-1,0,1,2,3],['level0','level1','level2','level3','level4','level5'])plt.plot(x,y1,color='red',linewidth=1.0,linestyle='--')plt.plot(x,y2,color='blue',linewidth=5.0,linestyle='-')
[
x = np.linspace(-3,3,100)y1 = 2*x + 1y2 = x**2#xy范围plt.xlim((-1,2))plt.ylim((-2,3))#xy描述plt.xlabel('I AM X')plt.ylabel('I AM Y')plt.plot(x,y1,color='red',linewidth=1.0,linestyle='--')plt.plot(x,y2,color='blue',linewidth=5.0,linestyle='-')new_ticks = np.linspace(-2,2,11)print(new_ticks)plt.xticks(new_ticks)plt.yticks([-1,0,1,2,3], ['level1','level2','level3','level4','level5'])# 得到当前的坐标ax = plt.gca()ax.spines['right'].set_color('red') # 把右边颜色变成红色ax.spines['top'].set_color('none') # 把上边去掉# 把x轴的刻度设置为‘bottom’# 把y轴的刻度设置为‘left’ax.xaxis.set_ticks_position('bottom')ax.yaxis.set_ticks_position('left')# 设置bottom对应到0点# 设置left对应到0点ax.spines['bottom'].set_position(('data',0))ax.spines['left'].set_position(('data',0))plt.show()
[-2. -1.6 -1.2 -0.8 -0.4 0. 0.4 0.8 1.2 1.6 2. ]
四、legend图例
legend l1, = plt.plot(x,y1,color=‘red’,linewidth=1.0,linestyle=’–’) l2, = plt.plot(x,y2,color=‘blue’,linewidth=5.0,linestyle=’-’)
plt.legend(handles=[l1,l2],labels=[‘test1’,‘test2’],loc=‘best’) # loc是图例位置 loc:best是自动选择最佳位置
x = np.linspace(-3,3,100)y1 = 2*x + 1y2 = x**2#xy范围plt.xlim((-1,2))plt.ylim((-2,3))#xy描述plt.xlabel('I AM X')plt.ylabel('I AM Y')l1, = plt.plot(x,y1,color='red',linewidth=1.0,linestyle='--')l2, = plt.plot(x,y2,color='blue',linewidth=5.0,linestyle='-')plt.legend(handles=[l1,l2],labels=['test1','test2'],loc='best') # loc是图例位置 loc:best是自动选择最佳位置new_ticks = np.linspace(-2,2,11)print(new_ticks)plt.xticks(new_ticks)plt.yticks([-1,0,1,2,3], ['level1','level2','level3','level4','level5'])plt.show()
[-2. -1.6 -1.2 -0.8 -0.4 0. 0.4 0.8 1.2 1.6 2. ]
五、标注
x = np.linspace(-1,1,100)y1 = 2*x + 1plt.plot(x,y1,color='red',linewidth=1.0,linestyle='-')#gca get current axisax = plt.gca()#把右边和上边的边框去掉ax.spines['right'].set_color('none')ax.spines['top'].set_color('none')#把x轴的刻度设置为‘bottom’#把y轴的刻度设置为‘left’ax.xaxis.set_ticks_position('bottom')ax.yaxis.set_ticks_position('left')#设置bottom对应到0点#设置left对应到0点ax.spines['bottom'].set_position(('data',0))ax.spines['left'].set_position(('data',0))x0 = 0.5y0 = 2*x0 + 1#画点plt.scatter(x0,y0,s=50,color='b')#画虚线plt.plot([x0,x0],[y0,0],'k--',lw=2)plt.annotate(r'$2x+1=%s$' % y0,xy=(x0,y0),xytext=(+30,-30),textcoords='offset points',fontsize=16, arrowprops=dict(arrowstyle='->',connectionstyle='arc3,rad=.2'))plt.text(-1,2,r'$this\ is\ the\ text$',fontdict={'size':'16','color':'r'})plt.show()
六、散点图
plt.scatter(x,y,s=50,c=‘b’,alpha=0.5) # alpha透明度
plt.scatter(np.arange(5),np.arange(5))plt.show()
x = np.random.normal(0,1,500)y = np.random.normal(0,1,500)plt.scatter(x,y,s=50,c='b',alpha=0.5) # alpha透明度plt.xlim((-2,2))plt.ylim((-2,2))plt.xticks(())plt.yticks(())plt.show()
七、直方图
plt.bar(x,y,facecolor=’#9999ff’,edgecolor=‘white’) # edgecolor 边框颜色
x = np.arange(10)y = 2**x + 10plt.bar(x,y)plt.show()
x = np.arange(10)y = 2**x + 10plt.bar(x,-y)plt.show()
x = np.arange(10)y = 2**x + 10plt.bar(x,y,facecolor='#9999ff',edgecolor='white') # edgecolor 边框颜色plt.show()
x = np.arange(10)y = 2**x + 10plt.bar(x,y,facecolor='#9999ff',edgecolor='white')for x,y in zip(x,y): plt.text(x,y,'%.2f' % y,ha='center',va='bottom')plt.show()
八、等高线图
X,Y = np.meshgrid(x,y) plt.contourf(X,Y,f(X,Y),8,alpha=0.75,cmap=plt.cm.hot)
C = plt.contour(X,Y,f(X,Y),8,colors=‘black’,linewidths=.5) plt.clabel(C,inline=True,fontsize=10)
def f(x, y): return (1-x/2+x**5+y**3)*np.exp(-x**2-y**2) x = np.linspace(-3,3,100)y = np.linspace(-3,3,100)X,Y = np.meshgrid(x,y)plt.contourf(X,Y,f(X,Y),8,alpha=0.75,cmap=plt.cm.hot)plt.xticks(())plt.yticks(())plt.show()
def f(x, y): return (1-x/2+x**5+y**3)*np.exp(-x**2-y**2) x = np.linspace(-3,3,100)y = np.linspace(-3,3,100)X,Y = np.meshgrid(x,y)plt.contourf(X,Y,f(X,Y),8,alpha=0.75,cmap=plt.cm.hot)C = plt.contour(X,Y,f(X,Y),8,colors='black',linewidths=.5)plt.clabel(C,inline=True,fontsize=10)plt.xticks(())plt.yticks(())plt.show()
九、3D图
ax.plot_surface(X,Y,Z,rstride=1,cstride=1,cmap=plt.get_cmap(‘rainbow’)) ax.contourf(X,Y,Z,zdir=‘z’,offset=-2,cmap=‘rainbow’) ax.set_zlim(-2,2)
import matplotlib.pyplot as pltimport numpy as npfrom mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()ax = Axes3D(fig)x = np.arange(-4,4,0.25)y = np.arange(-4,4,0.25)X,Y = np.meshgrid(x,y)R = np.sqrt(X**2 + Y**2)Z = np.sin(R)ax.plot_surface(X,Y,Z,rstride=1,cstride=1,cmap=plt.get_cmap('rainbow'))plt.show()
fig = plt.figure()ax = Axes3D(fig)x = np.arange(-4,4,0.25)y = np.arange(-4,4,0.25)X,Y = np.meshgrid(x,y)R = np.sqrt(X**2 + Y**2)Z = np.sin(R)ax.plot_surface(X,Y,Z,rstride=1,cstride=1,cmap=plt.get_cmap('rainbow'))ax.contourf(X,Y,Z,zdir='z',offset=-2,cmap='rainbow')ax.set_zlim(-2,2)plt.show()
十、subplot
plt.figure()plt.subplot(2,2,1)plt.plot([0,1],[0,1])plt.subplot(2,2,2)plt.plot([0,1],[0,1])plt.subplot(223)plt.plot([0,1],[0,1])plt.subplot(224)plt.plot([0,1],[0,1])plt.show()
plt.figure()plt.subplot(2,1,1)plt.plot([0,1],[0,1])plt.subplot(2,3,4)plt.plot([0,1],[0,1])plt.subplot(235)plt.plot([0,1],[0,1])plt.subplot(236)plt.plot([0,1],[0,1])plt.show()
十一、动态图
import matplotlib.pyplot as pltimport numpy as npfrom matplotlib import animation
fig,ax = plt.subplots()x = np.arange(0,2*np.pi,0.01)line, = ax.plot(x,np.sin(x))def animate(i): line.set_ydata(np.sin(x+i/10)) return line,def init(): line.set_ydata(np.sin(x)) return line,ani = animation.FuncAnimation(fig=fig,func=animate,init_func=init,interval=20)plt.show()
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~