1. 输出方程
x=np.linspace(-1, 1, 50)
y=2*x+1
plt.plot(x,y)
plt.show()
2.figure
import matplotlib.pyplot as plt
import numpy as np
x=np.linspace(-1, 1, 50)
y1=2*x+1
y2=x**2
plt.figure()
plt.plot(x,y1)
plt.figure(num=3,figsize=(8,5))
plt.plot(x,y2)
plt.plot(x,y1,color="red",linewidth=1.0,linestyle='--')
plt.show()
3. 设置坐标轴
3.1设置坐标轴 描述、小标
plt.xlim((-1,2))
plt.ylim((-2,3))
plt.xlabel('x label')
plt.ylabel('y label')
new_ticks=np.linspace(-1,2,5)
print(new_ticks)
plt.xticks(new_ticks)
plt.yticks([-1,-1.8,-1,1.22,3,],['really bad','bad','good','really good'])
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/4458069879a242775c6a66e1d3439f52.png)
3.2 修改坐标轴位置
ax=plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.spines['bottom'].set_position(('data',-1))
ax.spines['left'].set_position(('data',0))
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/f19e16731337befe392161e90e36e7f0.png)
4.图例 legend
plt.plot(x,y2,label='up')
plt.plot(x,y1,color="red",linewidth=1.0,linestyle='--',label='down')
plt.legend()
l1,=plt.plot(x,y2)
l2,=plt.plot(x,y1,color="red")
plt.legend(labels=['aaa','bbb'],loc="best")
5.注解 annotation
import matplotlib.pyplot as plt
import numpy as np
plt.figure()
x=np.linspace(-3, 3, 50)
y=2*x+1
plt.plot(x,y)
x0=1;
y0=2*x0+1
plt.scatter(x0,y0,color='red')
plt.show()
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/8a7661e2a7665c9c79a3c2cb22aa2f6a.png)
plt.scatter(x0,y0,s=20,color='blue')
plt.plot([x0,x0],[y0,0],'k--',lw=2.5)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/12445c80355321593c009e7b7698d8da.png)
方法一
plt.annotate(r'$2x+1=%s$'%y0,xy=(x0,y0),xycoords='data',
xytext=(30,-30),textcoords='offset points',
fontsize=16,arrowprops=dict(arrowStyle='>',
connectionstyle='arc3,rad=0.2'))
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/dc52928a9c486fee9992efe7e3f37628.png)
方法二
plt.text(-3.7, 3, r'$This\ is\ a\ line\ of\ y=2x+1$',fontdict={'size':16,'color':'orange'})
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/a54f11c139a975fa0ef5ee39b08a574e.png)
6.能见度 tick
Reference
Matplotlib Python 画图教程 (莫烦Python)