且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

第一组(散)绘图数据的动画制作过程中保持在图上用Python的matplotlib

更新时间:2023-02-20 14:34:20

 导入numpy的是NP
从数学进口*
从pylab进口*
从matplotlib进口pyplot如PLT
从matplotlib进口动画#常量
isqrt = 2 **( - 0.5)
欧米加= np.sqrt(2- np.sqrt(2))#Angular速度
L = 4 #Length系统N = 1 #Normal模数
如果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的散点前= [1,2,3]#x坐标#先设置好身材,轴,我们要以动画的情节元素
图= plt.figure()
斧= plt.axes(XLIM =(0,L)ylim =( - 1.1,1.1))行= ax.plot([],[],LW = 2,颜色='B')
SCAT,= ax.plot([],[],线型='',标记='O',颜色='B')
#初始化函数:积每帧的背景
高清的init():
    line.set_data([],[])
    scat.set_data([],[])
    返回[SCAT,线,]#动画功能。这就是所谓的依次
高清动画(T):
    xinterval = np.arange(0,10,0.05)
    波= np.cos(0.1 *欧米茄* T)* np.sin(N * xinterval * np.pi / L)
    line.set_data(xinterval,波)
    点= Z *实(np.exp(0+(欧米茄* 0.1 * T)* 1J))    scat.set_data(例如,点)
    返回[SCAT,线,]#拨打动画。
动画= animation.FuncAnimation(图,动画,=的init_func初始化,帧=范围(200),间隔= 20,blit的= TRUE)
plt.grid(真)
plt.show()

除非你有complelling理由使用分散(这是你想要的每个标记,以不同的颜色或大小,你的例子code不显示)上面会更有效地产生同样的动画

与你原来的code的问题是,你不更新分散艺术家每次经过,你添加新的艺术家它与位图传输$互动C $ç以奇怪的方式(在我的机器上的的previous点的所有的随机均可见或不可见)。

I'm trying to animate a line and 3 scatter points on a graph. Everything seems to be working except the first set of scatter points don't get removed on the graph.

Here is the code, you can try setting n equal to 1, 2 or 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()

Unless you have a complelling reason to use scatter (which is you want each marker to be a different color or size, which your example code does not show) the above will generate the same animation much more efficiently.

The issue with your original code is that you are not updating the scatter artist each time through, you are adding a new artist which interacts with the blitting code in strange ways (on my machine, all of the previous dots randomly were visible or not).