gpt4 book ai didi

python - 用红点动画正弦函数

转载 作者:行者123 更新时间:2023-12-02 00:46:16 27 4
gpt4 key购买 nike

我正在尝试为正在绘制的正弦函数制作动画,但在新绘制的图形部分上有一个红点。我已经画好了,但红点一直被绘制了好几次。这是我的代码:

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

X = np.linspace(0, 2*np.pi, 200)
Y = np.sin(X)


fig, ax = plt.subplots(1,1)
ax.set_xlim([0, 6*np.pi])
ax.set_ylim([-1.1, 1.1])

sinegraph, = ax.plot([], [])

def sine(i):
sinegraph.set_data(X[:i],Y[:i])
ax.plot(X[i], Y[i], 'o', color='red')

anim = animation.FuncAnimation(fig, sine, frames=400, interval=50)
plt.show()

基本上我需要删除红点并在每一帧重新绘制。

最佳答案

就像更新线一样,您可以使用先前定义的线图的 set_data() 方法更新红点。

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

X = np.linspace(0, 2*np.pi, 100)
Y = np.sin(X)

fig, ax = plt.subplots(1,1)
ax.set_xlim([0, 2*np.pi])
ax.set_ylim([-1.1, 1.1])

sinegraph, = ax.plot([], [])
dot, = ax.plot([], [], 'o', color='red')

def sine(i):
sinegraph.set_data(X[:i],Y[:i])
dot.set_data(X[i],Y[i])

anim = animation.FuncAnimation(fig, sine, frames=len(X), interval=50)
plt.show()

enter image description here

关于python - 用红点动画正弦函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43559337/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com