gpt4 book ai didi

python - 使用 matplotlib 保存散点图动画

转载 作者:太空狗 更新时间:2023-10-30 01:29:42 25 4
gpt4 key购买 nike

我一直在尝试使用 matplotlib 保存动画散点图,我更希望它不需要完全不同的代码来查看动画人物和保存副本。图中完美显示了保存完成后的所有数据点。

此代码是 Giggi's 的修改版本在 Animating 3d scatterplot in matplotlib ,修复了 Yann's answer 中的颜色在 Matplotlib 3D scatter color lost after redraw (因为颜色在我的视频中很重要,所以我想确保它们有效)。

import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

FLOOR = -10
CEILING = 10

class AnimatedScatter(object):
def __init__(self, numpoints=5):
self.numpoints = numpoints
self.stream = self.data_stream()
self.angle = 0

self.fig = plt.figure()
self.fig.canvas.mpl_connect('draw_event',self.forceUpdate)
self.ax = self.fig.add_subplot(111,projection = '3d')
self.ani = animation.FuncAnimation(self.fig, self.update, interval=100,
init_func=self.setup_plot, blit=True,frames=20)

def change_angle(self):
self.angle = (self.angle + 1)%360

def forceUpdate(self, event):
self.scat.changed()

def setup_plot(self):
X = next(self.stream)
c = ['b', 'r', 'g', 'y', 'm']
self.scat = self.ax.scatter(X[:,0], X[:,1], X[:,2] , c=c, s=200, animated=True)

self.ax.set_xlim3d(FLOOR, CEILING)
self.ax.set_ylim3d(FLOOR, CEILING)
self.ax.set_zlim3d(FLOOR, CEILING)

return self.scat,

def data_stream(self):
data = np.zeros(( self.numpoints , 3 ))
xyz = data[:,:3]
while True:
xyz += 2 * (np.random.random(( self.numpoints,3)) - 0.5)
yield data

def update(self, i):
data = next(self.stream)
#data = np.transpose(data)

self.scat._offsets3d = ( np.ma.ravel(data[:,0]) , np.ma.ravel(data[:,1]) , np.ma.ravel(data[:,2]) )

plt.draw()
return self.scat,

def show(self):
plt.show()

if __name__ == '__main__':
a = AnimatedScatter()
a.ani.save("movie.avi", codec='avi')
a.show()

由此生成了一个完全有效的 .avi,但除了轴之外,它在所有四秒钟内都是空白的。实际的数字总是准确地显示我想看到的。我怎样才能像填充正常运行的动画一样填充保存函数的绘图,或者在 matplotlib 中是否可行?

编辑: 在更新中使用分散调用(没有像在初始化程序中那样设置边界)导致 .avi 显示轴增长,表明数据每次都在运行,它是只是没有显示在视频本身上。我在 Python 2.7.3 中使用 matplotlib 1.1.1rc。

最佳答案

FuncAnimation 中删除 blit=True 并从 scatter 中删除 animated=True 并且它有效。我怀疑确保只有需要更新的艺术家在帧之间更新/重绘(而不是仅仅重绘所有内容)的逻辑出了问题。

下面正是我运行的,我得到了预期的输出电影:

import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

FLOOR = -10
CEILING = 10

class AnimatedScatter(object):
def __init__(self, numpoints=5):
self.numpoints = numpoints
self.stream = self.data_stream()
self.angle = 0

self.fig = plt.figure()
self.fig.canvas.mpl_connect('draw_event',self.forceUpdate)
self.ax = self.fig.add_subplot(111,projection = '3d')
self.ani = animation.FuncAnimation(self.fig, self.update, interval=100,
init_func=self.setup_plot, frames=20)

def change_angle(self):
self.angle = (self.angle + 1)%360

def forceUpdate(self, event):
self.scat.changed()

def setup_plot(self):
X = next(self.stream)
c = ['b', 'r', 'g', 'y', 'm']
self.scat = self.ax.scatter(X[:,0], X[:,1], X[:,2] , c=c, s=200)

self.ax.set_xlim3d(FLOOR, CEILING)
self.ax.set_ylim3d(FLOOR, CEILING)
self.ax.set_zlim3d(FLOOR, CEILING)

return self.scat,

def data_stream(self):
data = np.zeros(( self.numpoints , 3 ))
xyz = data[:,:3]
while True:
xyz += 2 * (np.random.random(( self.numpoints,3)) - 0.5)
yield data

def update(self, i):
data = next(self.stream)
self.scat._offsets3d = ( np.ma.ravel(data[:,0]) , np.ma.ravel(data[:,1]) , np.ma.ravel(data[:,2]) )
return self.scat,

def show(self):
plt.show()

if __name__ == '__main__':
a = AnimatedScatter()
a.ani.save("movie.avi", codec='avi')
a.show()

关于python - 使用 matplotlib 保存散点图动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14739969/

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