如何使用Matplotlib从头开始制作曲线动画

时间:2019-04-16 12:55:14

标签: python matplotlib animation

请注意,这是How to make an animation of a Lissajous curve的后续问题;

我的第一个想法是编辑我的原始问题并要求动画,但我理解并尊重SO的操作方式。因此,最好的方法是提出另一个问题。

我想用参数化制作曲线的动画(在其中进行增量绘制):x(t)= sin(3t)和y(y)= sin(4t),其中t [0,2pi]。 / p>

为此,我将添加代码:

from matplotlib.animation import FuncAnimation

fig, ax = plt.subplots()
ln, = plt.plot([], [], 'b')

def init():
    ax.set_xlim(-1, 1)
    ax.set_ylim(-1, 1)
    return ln,

def update(frame):
    x.append(np.sin(4*frame))
    y.append(np.sin(3*frame))
    ln.set_data(x, y)
    return ln,

ani = FuncAnimation(fig, update, frames=np.linspace(0, 2*np.pi, 128),
                    init_func=init, blit=True)

问题在于,使用此代码不会从头开始绘制整个曲线。所做的是重绘它,变得重叠。

如何从头开始绘制(即从白色背景开始)?我一直在想如果没有,但一无所获。

谢谢

编辑

让我向您展示整个代码:

%matplotlib notebook

import matplotlib.pyplot as plt
import math
import numpy as np
from matplotlib.animation import FuncAnimation

# set the minimum potential
rm = math.pow(2, 1 / 6)
t = np.linspace(-10, 10, 1000, endpoint = False)
x = []
y = []

for i in t: #TypeError 'int' object is not iterable
    x_i = np.sin( 3 * i )
    y_i = np.sin( 4 * i )
    x.append(x_i)
    y.append(y_i)

# set the title
plt.title('Plot sin(4t) Vs sin(3t)')

# set the labels of the graph
plt.xlabel('sin(3t)')
plt.ylabel('sin(4t)')

fig, ax = plt.subplots()
ln, = plt.plot([], [], 'b')

def init():
    ax.set_xlim(-1, 1)
    ax.set_ylim(-1, 1)
    return ln,

def update(frame):
    x.append(np.sin(4*frame))
    y.append(np.sin(3*frame))
    ln.set_data(x, y)
    return ln,

ani = FuncAnimation(fig, update, frames=np.linspace(0, 2*np.pi, 128),
                    init_func=init, blit=True)

# display the graph
plt.show()

这是我刚开始获得的图像(开始运行大约1秒钟后拍摄的屏幕快照;这就是为什么您看到那条有趣的线):https://imgur.com/a/bNoViDA。如您所见,它不是从头开始的(即不是从白色背景开始的)

这是我最后得到的情节:https://imgur.com/a/WQHHUk9

我正在寻找最终的终点,但要从头开始绘制所有内容,而不必从所示的图开始。

0 个答案:

没有答案