数据可视化——Matplotlib绘图02
程序员文章站
2024-01-14 11:27:16
...
本篇将继续介绍Matplotlib绘图。
常用的图像设置命令:
plt.axhline() #绘制平行于x轴的水平参考线
plt.axvline() #绘制平行于y轴的水平参考线
plt.axhspan() #绘制垂直于y轴的参考区域
plt. axvspan() #绘制垂直于x轴的参考区域
x = np.linspace(1,10,10)
y = np.cos(x)
plt.plot(x,y,'g*-',linewidth=2,alpha=0.6,drawstyle='steps')
plt.grid(ls=':',c='b')
plt.axhline(y=0.15,color='blue',linewidth=4)
x = np.linspace(1,10,10)
y = np.cos(x)
plt.plot(x,y,'g*-',linewidth=2,alpha=0.6,drawstyle='steps')
plt.grid(ls=':',c='b')
plt.axhline(y=0.15,color='blue',linewidth=4)
x = np.linspace(1,10,10)
y = np.cos(x)
plt.plot(x,y,'g*-',linewidth=2,alpha=0.6,drawstyle='steps')
plt.grid(ls=':',c='b')
plt.axhline(y=0.15,color='blue',linewidth=4)
plt.axvline(6,c='r',lw=5,ls='-.') #把垂直的这条线更改为虚线
x = np.linspace(0,10,10)
y = np.cos(x)
plt.plot(x,y,'b*-',linewidth = 10,alpha = 0.6,drawstyle='steps')
plt.grid(ls=':',c='b')
plt.axhspan(-0.25,0.25) #绘制区域
plt.axvline(6,c='r',lw=5)
x = np.linspace(0,10,10)
y = np.cos(x)
plt.plot(x,y,'b*-',linewidth = 10,alpha = 0.6,drawstyle='steps')
plt.grid(ls=':',c='b')
plt.axhspan(-0.25,0.25,facecolor='yellow',alpha=0.6) #更改区域颜色
plt.axvline(6,c='r',lw=5)
x = np.linspace(0,10,10)
y = np.cos(x)
plt.plot(x,y,'b*-',linewidth = 10,alpha = 0.6,drawstyle='steps')
plt.grid(ls=':',c='b')
plt.axhspan(-0.25,0.25,facecolor='yellow',alpha=0.6)
plt.axvline(6,c='r',lw=5)
plt.axvspan(4,8,facecolor='c',alpha=0.3) #绘制垂直区域
常用的图像设置命令
plt.legend() #标示不同图形的文本标签图例
plt.xticks() #设置x轴的标签名称
plt.yticks() #设置y轴的标签名称
plt.text() #添加图形内容细节的无指向型注释文本
plt.annotate() #添加图形内容细节的指向型注释文本
x = np.linspace(0,10,10)
y = np.cos(x)
plt.plot(x,y,'b*-',linewidth = 10,alpha = 0.6,drawstyle='steps')
plt.grid(ls=':',c='b')
plt.axhspan(-0.25,0.25,facecolor='yellow',alpha=0.6)
plt.axvline(6,c='r',lw=5,ls='-.')
plt.axvspan(4,8,facecolor='c',alpha=0.3)
plt.legend(loc='upper right')
plt.yticks([-1.00,-0.75,-0.50,-0.25,0.00,0.25,0.50,0.75,1.00],['H','C','Y','I','L','O','V','E','Y'])
#更改了例图纵坐标的名称(示例)
x = np.linspace(0,10,10)
y = np.cos(x)
plt.plot(x,y,'b*-',linewidth = 10,alpha = 0.6,drawstyle='steps')
plt.grid(ls=':',c='b')
plt.axhspan(-0.25,0.25,facecolor='yellow',alpha=0.6)
plt.axvline(6,c='r',lw=5,ls='-.')
plt.axvspan(4,8,facecolor='c',alpha=0.3)
plt.legend(loc='upper right')
plt.text(1.5,0,'y=cos(x)',weight='bold',color='b') #1.5是表示标注距离纵坐标的距离
这篇就到这里啦,比较简短。下一篇会介绍Matplotlib中plt常见的二维图形绘制命令,包括各种图形的绘制。
下一篇: Matplotlib绘图和可视化