相关疑难解决方法(0)

将 matplotlib 动画保存为 mp4

我想将以下程序的输出动画保存为mp4。该程序确实创建了一个 mp4 文件,但该文件是一个空白文件,它不包含我想要的动画。我在这里做错了什么?

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

plt.rcParams['animation.ffmpeg_path'] ='C:\\ffmpeg\\bin\\ffmpeg.exe'
fig=plt.figure()
ax=fig.add_subplot(111,projection="3d")

x=np.linspace(100,150,100)
t=(x-100)/0.5
y=-.01*np.cos(t)+.5*np.sin(t)+100.01
z=.01*np.sin(t)+.5*np.cos(t)+99.5

def animate(i):
    line.set_data(x[:i],y[:i])
    line.set_3d_properties(z[:i])

ax.set_xlim3d([min(x),max(x)])
ax.set_ylim3d([min(y),max(y)])
ax.set_zlim3d([min(z),max(z)])
ax.set_title("Particle in magnetic field") 
ax.set_xlabel("X")
ax.set_xlabel("Y")
ax.set_xlabel("Z")
line,=ax.plot([],[],[])
lin_ani=animation.FuncAnimation(fig,animate)
plt.legend()

FFwriter = animation.FFMpegWriter()
lin_ani.save('animation.mp4', writer = FFwriter, fps=10)
# plt.show()
Run Code Online (Sandbox Code Playgroud)

python animation matplotlib

10
推荐指数
2
解决办法
3万
查看次数

使用matplotlib保存散点图动画

我一直在试图挽救一个动画散点图与matplotlib,我宁愿它并不需要完全不同的代码查看为动画人物和保存一份副本.该图显示了保存完成后的所有数据点.

此代码是修改后的版本Giggi的动画中matplotlib 3D散点图,从对颜色的修复晏的回答Matplotlib三维散彩丢失重绘后(因为颜色将是我的视频很重要,所以我要确保他们的工作).

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 …
Run Code Online (Sandbox Code Playgroud)

python 3d matplotlib avi scatter-plot

5
推荐指数
1
解决办法
5074
查看次数

标签 统计

matplotlib ×2

python ×2

3d ×1

animation ×1

avi ×1

scatter-plot ×1