python学习笔记——matplotlib的使用(4)

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()

猜你喜欢

转载自blog.csdn.net/qq_42947110/article/details/107390716