gpt4 book ai didi

python - 在使用 Python 的 matplotlib 制作动画期间,第一组(散点)绘图数据保留在图形上

转载 作者:太空宇宙 更新时间:2023-11-04 03:54:40 24 4
gpt4 key购买 nike

我正在尝试为图表上的一条线和 3 个散点设置动画。一切似乎都在工作,除了第一组散点在图表上没有被删除。

这是代码,你可以尝试将 n 设置为 1、2 或 3

import numpy as np
from math import *
from pylab import *
from matplotlib import pyplot as plt
from matplotlib import animation

# Constants
isqrt = 2**(-0.5)
omega = np.sqrt(2-np.sqrt(2)) #Angular velocity
L=4 #Length of the system

n = 1 #Normal mode number
if n==1:
z = [isqrt,1,isqrt] #mode 1
elif n==2:
z = [1,0,-1] #mode 2
elif n==3:
z = [isqrt,-1,isqrt] #mode 3

ex = [1,2,3] #x-coordinates of scatter points

# First set up the figure, the axis, and the plot element we want to animate
fig = plt.figure()
ax = plt.axes(xlim=(0, L), ylim=(-1.1, 1.1))

line, = ax.plot([], [], lw=2)
scat = ax.scatter([],[])
# initialization function: plot the background of each frame
def init():
line.set_data([], [])
scat.set_array(None)
return [scat,line,]

# animation function. This is called sequentially
def animate(t):
xinterval = np.arange(0,10,0.05)
wave = np.cos(0.1*omega*t)*np.sin(n*xinterval*np.pi/L)
line.set_data(xinterval, wave)
dots = z*real(np.exp(0+(omega*0.1*t)*1j))

scat = plt.scatter(ex, dots, s=50)
return [scat,line,]

# call the animator.
anim = animation.FuncAnimation(fig, animate,init_func=init, frames=200, interval=20, blit=True)
plt.grid(True)
plt.show()

最佳答案

import numpy as np
from math import *
from pylab import *
from matplotlib import pyplot as plt
from matplotlib import animation

# Constants
isqrt = 2**(-0.5)
omega = np.sqrt(2-np.sqrt(2)) #Angular velocity
L=4 #Length of the system

n = 1 #Normal mode number
if n==1:
z = [isqrt,1,isqrt] #mode 1
elif n==2:
z = [1,0,-1] #mode 2
elif n==3:
z = [isqrt,-1,isqrt] #mode 3

ex = [1,2,3] #x-coordinates of scatter points

# First set up the figure, the axis, and the plot element we want to animate
fig = plt.figure()
ax = plt.axes(xlim=(0, L), ylim=(-1.1, 1.1))

line, = ax.plot([], [], lw=2, color='b')
scat, = ax.plot([],[], linestyle='', marker='o', color='b')
# initialization function: plot the background of each frame
def init():
line.set_data([], [])
scat.set_data([], [])
return [scat,line,]

# animation function. This is called sequentially
def animate(t):
xinterval = np.arange(0,10,0.05)
wave = np.cos(0.1*omega*t)*np.sin(n*xinterval*np.pi/L)
line.set_data(xinterval, wave)
dots = z*real(np.exp(0+(omega*0.1*t)*1j))

scat.set_data(ex, dots)
return [scat,line,]

# call the animator.
anim = animation.FuncAnimation(fig, animate,init_func=init, frames=range(200), interval=20, blit=True)
plt.grid(True)
plt.show()

除非您有充分的理由使用scatter(即您希望每个标记具有不同的颜色或大小,而您的示例代码并未显示),否则上面将生成相同的动画更高效。

你的原始代码的问题是你不是每次都更新 scatter 艺术家,你添加了一个新的艺术家,它以奇怪的方式与 blitting 代码交互(在我的机器上,< em>所有 之前的点随机可见或不可见)。

关于python - 在使用 Python 的 matplotlib 制作动画期间,第一组(散点)绘图数据保留在图形上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19470324/

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