python动画实现1-animation

参考这篇文章
进行实现

#动画演示

from matplotlib import animation
from matplotlib import pyplot as plt 
import numpy as np

xdata = np.linspace(0,2*np.pi,50)
fig,ax = plt.subplots(1,1,figsize=(6,5))
b_cir = ax.plot(5*np.cos(xdata),5*np.sin(xdata))

# ax.spines['right'].set_color('none')
# ax.spines['left'].set_color('none')
# ax.spines['top'].set_color('none')
# ax.spines['bottom'].set_color('none')

# plt.xticks([])
# plt.yticks([])
plt.xlim((-10,20))
plt.ylim((-10,10))
# plt.axis('equal')


#生成第一个移动点
movp1, = ax.plot([], [], 'ro', animated=True)
#生成第一个移动圆
c_sub1, = ax.plot([], [], animated=True)
#生成第二个移动点
movp2, = ax.plot([], [], 'ro', animated=True)
#生成第二个移动圆
c_sub2, = ax.plot([], [], animated=True)

#生成正弦曲线
sine, = ax.plot(xdata + 10, np.zeros(50))

#添加第3个圆上的移动点
movp3, = ax.plot([], [], 'ro', animated=True) 
ln, = ax.plot([], [], animated=True)

def update(frame):
    #计算第一个移动点
    movX1 = 5 * np.cos(frame * 1)
    movY1 = 5 * np.sin(frame * 1)
    
    #计算第一个移动圆
    c_sub1X = 2 * np.cos(xdata) + movX1
    c_sub1Y = 2 * np.sin(xdata) + movY1

    #第二个移动点
    movX2 = movX1 + 2 * np.cos(frame * 3)
    movY2 = movY1 + 2 * np.sin(frame * 3)
    #第二个移动圆
    c_sub2X = 1 * np.cos(xdata) + movX2
    c_sub2Y = 1 * np.sin(xdata) + movY2

    # 更新移动点数据
    movp1.set_data(movX1, movY1)    # 更新移动点数据
    c_sub1.set_data(c_sub1X, c_sub1Y)
    # 更新第1个移动点和圆
    # 更新第2个移动点和圆
    movp2.set_data(movX2, movY2)
    c_sub2.set_data(c_sub2X, c_sub2Y)

    s = 5 * np.sin((xdata+frame) * 1) + \
        2 * np.sin((xdata+frame) * 3) + \
        1 * np.sin((xdata+frame) * 5)

    sine.set_ydata(s)

    movX3 = movX2 + 1 * np.cos(frame * 5)
    movY3 = movY2 + 1 * np.sin(frame * 5)
    
    ln.set_data([movX3, 10 + 2 * np.pi], [movY3, movY3])
    movp3.set_data(movX3, movY3)

    return movp1, c_sub1, movp2, c_sub2, sine, ln, movp3,

ani = animation.FuncAnimation(fig=fig,
                            func=update,
                            frames=xdata,
                            # init_func=init,
                            # interval=100,
                            blit=True)
plt.show()

你可能感兴趣的:(python学习笔记)