Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 散点图的Matplotlib动画_Python_Python 3.x_Numpy_Matplotlib - Fatal编程技术网

Python 散点图的Matplotlib动画

Python 散点图的Matplotlib动画,python,python-3.x,numpy,matplotlib,Python,Python 3.x,Numpy,Matplotlib,我正在尝试使用Matplotlib的FuncAnimation为每帧动画显示一个点设置动画 # modules #------------------------------------------------------------------------------ import numpy as np import matplotlib.pyplot as py from matplotlib import animation py.close('all') # close all pr

我正在尝试使用Matplotlib的
FuncAnimation
为每帧动画显示一个点设置动画

# modules
#------------------------------------------------------------------------------
import numpy as np
import matplotlib.pyplot as py
from matplotlib import animation

py.close('all') # close all previous plots

# create a random line to plot
#------------------------------------------------------------------------------

x = np.random.rand(40)
y = np.random.rand(40)

py.figure(1)
py.scatter(x, y, s=60)
py.axis([0, 1, 0, 1])
py.show()

# animation of a scatter plot using x, y from above
#------------------------------------------------------------------------------

fig = py.figure(2)
ax = py.axes(xlim=(0, 1), ylim=(0, 1))
scat = ax.scatter([], [], s=60)

def init():
    scat.set_offsets([])
    return scat,

def animate(i):
    scat.set_offsets([x[:i], y[:i]])
    return scat,

anim = animation.FuncAnimation(fig, animate, init_func=init, frames=len(x)+1, 
                               interval=200, blit=False, repeat=False)

不幸的是,最终的动画情节与原始情节不同。动画情节也会在动画的每一帧中闪烁几个点。关于如何使用
动画
软件包正确设置散点图动画的任何建议?

示例中唯一的问题是如何在
动画
功能中填充新坐标
set_offset
需要一个
Nx2
ndarray,您可以提供两个1d数组的元组

所以只要用这个:

def animate(i):
    data = np.hstack((x[:i,np.newaxis], y[:i, np.newaxis]))
    scat.set_offsets(data)
    return scat,
要保存动画,您可能需要调用:

anim.save('animation.mp4')

免责声明,我编写了一个库来尝试简化此过程,但使用了
ArtistAnimation
,名为。基本上,您可以像平常一样编写可视化代码,只需在绘制每个帧后拍照。下面是一个完整的示例:

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
from celluloid import Camera

fig = plt.figure()
camera = Camera(fig)

dots = 40
X, Y = np.random.rand(2, dots)
plt.xlim(X.min(), X.max())
plt.ylim(Y.min(), Y.max())
for x, y in zip(X, Y):
    plt.scatter(x, y)
    camera.snap()
anim = camera.animate(blit=True)
anim.save('dots.gif', writer='imagemagick')

效果很好。谢谢你的帮助。我没有意识到
set\u offsets
函数的要求。我的
x
y
值是一个Pythonic数字列表,因此
scat.set\u offsets(np.c\ux,y])
对我有效。除了不使用此库会为您节省三行代码之外。所以它的范围似乎很有限。我不确定你是如何数到3行的,但我并不反对你。我之所以编写这个库,是因为每次尝试使用
FuncAnimation
时,我至少要花一个小时。对我来说,这更多的是为了节省时间,而不是代码行:)当然,请注意,艺术化的成本相当高。一个20帧的2分钟动画在内存中创建了2400个艺术家,这与动画所需的单个艺术家相比是相当多的。如果你知道其中的含义,这没关系,但也许新用户会从理解Func和ArtistAnimation之间的差异以及如何使用它们中受益更多,而不是使用可能会在以后引起麻烦的blackbox工具。我想说的是,例如,drawnow,它旨在简化事情,但却会给几乎任何不了解它的功能的人带来问题。我同意你的观点。我认为它使用了更多的内存,但我不确定如何量化它。我需要添加更多关于这类事情的免责声明。我需要更多地考虑从初学者到高级的过渡。这个软件包可能不应该被那些想要超越matplotlib动画api初学者的人使用。它也适用于海生地块。非常感谢。很高兴我找到了这个帖子。