python之Matplotlib
环境:jupyter Notebook(Anaconda)
1.pyplot中的基础绘图语法
import numpy as np
import matplotlib.pyplot as plt
data=np.arange(0,1.1,0.01)
plt.title('lines')
plt.xlabel('x')
plt.xlabel('y')
plt.xlim((0,1))
plt.ylim((0,1))
plt.xticks([0,0.2,0.4,0.6,0.8,1])
plt.yticks([0,0.2,0.4,0.6,0.8,1])
plt.plot(data,data**2)
plt.plot(data,data**4)
plt.legend(['y=x^2','y=x^4'])
plt.savefig('D:/y=x^2.png') `在这里插入代码片`
plt.show()
2.包含子图绘制的基础语法
rad=np.arange(0,np.pi*2,0.01)
p1=plt.figure(figsize=(8,6),dpi=80)
ax1=p1.add_subplot(2,1,1)
plt.title('lines')
plt.xlabel('x')
plt.ylabel('y')
plt.xlim((0,1))
plt.ylim((0,1))
plt.xticks([0,0.1,0.4,0.6,0.8,1])
plt.yticks([0,0.1,0.4,0.6,0.8,1])
plt.plot(rad,rad**2)
plt.plot(rad,rad**4)
plt.legend(['y=x^2','y=x^4'])
ax2=p1.add_subplot(2,1,2)
plt.title('sin/cos')
plt.xlabel('rad')
plt.ylabel('value')
plt.xlim((0,np.pi*2))
plt.ylim((-1,-1))
plt.xticks([0,np.pi/2,np.pi,np.pi*1.5,np.pi*2])
plt.yticks([-1,-0.5,0,0.5,1])
plt.plot(rad,np.sin(rad))
plt.plot(rad,np.cos(rad))
plt.legend(['sin','cos'])
plt.savefig('D:/sincos.png')
plt.show()
3.设置pyplot的动态rc参数
x=np.linspace(0,4*np.pi)
y=np.sin(x)
plt.plot(x,y,label="$sin(x)$")
plt.title('sin')
plt.savefig('D:/默认sin曲线.png')
plt.show()
觉得写得不错的话,可以点个赞吗~
|