python学习笔记
一、matplotlib的使用(4)
1、主次坐标轴设置
import numpy as np
import matplotlib.pyplot as plt
x=np.arange(0,10,1)
y1=x
y2=x**2-10*x
fig,ax1=plt.subplots()
ax2=ax1.twinx()#利用twinx则设置y的主次坐标轴,twiny则设置x的主次坐标轴
ax1.plot(x,y1,'b-')
ax2.plot(x,y2,'r--')
plt.show()
显示结果:
2、动画显示animation
此处代码仍有问题,无法在jupternotebook上正确显示,待日后解决更新
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
animation,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))
return line,
def init():
line.set_ydata(np.sin(x+i/10))
return line,
ani=animation.FuncAnimation(fig=fig,func=animate,frames=100,init_func=init,interval=20,blit=False)
#frames帧数interval频率(毫秒)
plt.show()