python matplot绘图legend_Python matplotlib绘图基本元素

数据准备

```

importmatplotlib.pyplot as pltimportnumpy as npx=np.linspace(0.5,3.5,100)y=np.sin(x)y1=np.random.random(10)

```

plot() 线图

plt.plot(x,y,ls='-',lw=2,label='plot figure')plt.legend()plt.show()

scatter() 散点图

importmatplotlib.pyplotaspltimportnumpyasnpx = np.linspace(0.5,10,1000)y = np.random.randn(1000)plt.scatter(x,y,label='scatter fugure')plt.legend()plt.show()

xlim() 坐标刻度范围

plt.scatter(x,y,label='scatter figure')plt.legend()plt.xlim(0.05,10)plt.ylim(0,1)plt.show()

xlabel() 坐标标签

x = np.linspace(0.05,10,1000)y = np.sin(x)plt.plot(x,y,ls='-.',lw=2,c='c',label='plot figure')plt.legend()plt.xlabel('x-axis')plt.ylabel('y-axis')plt.show()

grid() 绘制网格

plt.plot(x,y,ls='-.',lw=2,c='c',label='plot figure')plt.legend()plt.grid(linestyle=':',color='r')plt.show()

axhine() 绘制水平参考线

plt.plot(x,y,ls='-.',lw=2,c='c',label='plot figure')plt.legend()plt.axhline(y=0.0,c='r',ls='--',lw=2)plt.axvline(x=4.0,c='r',ls='--',lw=2)plt.show()

axvspan() 绘制垂直参考区域

plt.plot(x,y,ls='-.',lw=2,c='c',label='plot figure')plt.legend()plt.axvspan(xmin=4.0,xmax=6.0,facecolor='y',alpha=0.3)plt.axhspan(ymin=0.0,ymax=0.5,facecolor='y',alpha=0.3)plt.show()

annotate() 添加图形内容细节指向性注释文本

plt.plot(x,y,ls='-.',lw=2,c='c',label='plot figure')plt.legend()plt.annotate('maximum',            xy=(np.pi/2,1),            xytext=((np.pi/2)+1,0.8),            weight='bold',            color='b',            arrowprops=dict(arrowstyle='->',connectionstyle='arc3',color='b'))plt.show()

text() 添加图形内容注释文本

plt.plot(x,y,ls='-.',lw=2,c='c',label='plot figure')plt.text(3.1,0.09,'y=sin(x)',weight='bold',color='b')plt.show()

title() 标题

plt.plot(x,y,ls='-',label='plot figure')plt.legend(loc='lower left')plt.show()

legend() 图例

x = np.linspace(0.5,3.5,100)

y = np.sin(x)

y1 = np.random.randn(100)

plt.scatter(x,y1,c='0.25',label='scatter figure')

plt.plot(x,y,ls='--',lw=2,label='plot figure')

forspineinplt.gca().spines.keys():

if spine =='top'orspine =='right':

plt.gca().spines[spine].set_color('none')

plt.gca().xaxis.set_ticks_position('bottom')

plt.gca().yaxis.set_ticks_position('left')plt.xlim(0,4)plt.ylim(-3,3)plt.ylabel('y_axis')

plt.xlabel('x_axis')plt.grid(True,ls=':',color='r')

plt.axhline(y=0,c='r',ls='--',lw=2)plt.axvspan(xmin=1, xmax=2, facecolor='y', alpha=.3)

plt.annotate('maximum',xy=(np.pi/2,1), xytext=((np.pi/2)+.15,1.5), weight='bold',color='b',            arrowprops=dict(arrowstyle='->',connectionstyle='arc3',color='r'))

plt.annotate('spines',xy=(0.75,-3), xytext=(0.35,-2.25), weight='bold',color='b',  arrowprops=dict(arrowstyle='->',connectionstyle='arc3',color='r'))

plt.annotate('spines',xy=(0,-2.78), xytext=(0.4,-2.32), weight='bold',color='b', arrowprops=dict(arrowstyle='->',connectionstyle='arc3',color='r'))

plt.annotate('spines',xy=(3.5,-2.98), xytext=(3.6,-2.7), weight='bold',color='b', arrowprops=dict(arrowstyle='->',connectionstyle='arc3',color='r'))

plt.text(3.6,-2.7,"'| is tickline'",weight='bold',color='b')

plt.text(3.6,-2.95,"3.5 sis ticklabel",weight='bold',color='b')

plt.title('structure of matplotlib')plt.legend()plt.show()

组合图

x = np.linspace(0.5,3.5,100)

y = np.sin(x)

y1 = np.random.randn(100)

plt.scatter(x,y1,c='0.25',label='scatter figure')

plt.plot(x,y,ls='--',lw=2,label='plot figure')

forspineinplt.gca().spines.keys():

if spine =='top'orspine =='right':

plt.gca().spines[spine].set_color('none')        plt.gca().xaxis.set_ticks_position('bottom')

plt.gca().yaxis.set_ticks_position('left')

plt.xlim(0,4)plt.ylim(-3,3)plt.ylabel('y_axis')plt.xlabel('x_axis')

plt.grid(True,ls=':',color='r')plt.axhline(y=0,c='r',ls='--',lw=2)

plt.axvspan(xmin=1, xmax=2, facecolor='y', alpha=.3)

plt.annotate('maximum',xy=(np.pi/2,1), xytext=((np.pi/2)+.15,1.5), weight='bold',color='b',            arrowprops=dict(arrowstyle='->',connectionstyle='arc3',color='r'))

plt.annotate('spines',xy=(0.75,-3), xytext=(0.35,-2.25), weight='bold',color='b', arrowprops=dict(arrowstyle='->',connectionstyle='arc3',color='r'))

plt.annotate('spines',xy=(0,-2.78), xytext=(0.4,-2.32), weight='bold',color='b', arrowprops=dict(arrowstyle='->',connectionstyle='arc3',color='r'))

plt.annotate('spines',xy=(3.5,-2.98), xytext=(3.6,-2.7), weight='bold',color='b',            arrowprops=dict(arrowstyle='->',connectionstyle='arc3',color='r'))

plt.text(3.6,-2.7,"'| is tickline'",weight='bold',color='b')

plt.text(3.6,-2.95,"3.5 sis ticklabel",weight='bold',color='b')

plt.title('structure of matplotlib')plt.legend()plt.show()

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值