Python 在 matplotlib 中为旋转 3D 图形设置动画

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18344934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:31:18  来源:igfitidea点击:

Animate a rotating 3D graph in matplotlib

pythonanimationmatplotlib

提问by Nate

I have a scatter plot set up and plotted the way I want it, and I want to create an .mp4 video of the figure rotating in space, as if I had used plt.show()and dragged the viewpoint around.

我设置了一个散点图并按照我想要的方式绘制,我想创建一个在空间中旋转的图形的 .mp4 视频,就好像我已经使用plt.show()并拖动了视点一样。

This answeris almost exactly what I want, except to save a movie I would have to manually call into FFMpeg with a folder of images. Instead of saving individual frames I'd prefer to use Matplotlib's built in animation support. Code reproduced below:

这个答案几乎正是我想要的,除了要保存电影,我必须手动调用带有图像文件夹的 FFMpeg。我宁愿使用 Matplotlib 的内置动画支持,而不是保存单个帧。代码转载如下:

from mpl_toolkits.mplot3d import Axes3D
ax = Axes3D(fig)
ax.scatter(xx,yy,zz, marker='o', s=20, c="goldenrod", alpha=0.6)
for ii in xrange(0,360,1):
    ax.view_init(elev=10., azim=ii)
    savefig("movie"%ii+".png")

采纳答案by Viktor Kerkez

If you want to learn more about matplotlibanimations you should really follow this tutorial. It explains in great length how to create animated plots.

如果您想了解更多关于matplotlib动画的知识,您应该真正遵循本教程。它详细解释了如何创建动画情节。

Note:Creating animated plots require ffmpegor mencoderto be installed.

注意:创建动画图需要安装ffmpegmencoder安装。

Here is a version of his first example changed to work with your scatterplot.

这是他的第一个示例的一个版本,已更改为适用于您的散点图。

# First import everthing you need
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
from mpl_toolkits.mplot3d import Axes3D

# Create some random data, I took this piece from here:
# http://matplotlib.org/mpl_examples/mplot3d/scatter3d_demo.py
def randrange(n, vmin, vmax):
    return (vmax - vmin) * np.random.rand(n) + vmin
n = 100
xx = randrange(n, 23, 32)
yy = randrange(n, 0, 100)
zz = randrange(n, -50, -25)

# Create a figure and a 3D Axes
fig = plt.figure()
ax = Axes3D(fig)

# Create an init function and the animate functions.
# Both are explained in the tutorial. Since we are changing
# the the elevation and azimuth and no objects are really
# changed on the plot we don't have to return anything from
# the init and animate function. (return value is explained
# in the tutorial.
def init():
    ax.scatter(xx, yy, zz, marker='o', s=20, c="goldenrod", alpha=0.6)
    return fig,

def animate(i):
    ax.view_init(elev=10., azim=i)
    return fig,

# Animate
anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=360, interval=20, blit=True)
# Save
anim.save('basic_animation.mp4', fps=30, extra_args=['-vcodec', 'libx264'])

回答by Jameshobbs

Was looking into animating my plots with matplotlib when I stumbled upon this: http://zulko.wordpress.com/2012/09/29/animate-your-3d-plots-with-pythons-matplotlib/

当我偶然发现这个时,正在研究使用 matplotlib 为我的绘图设置动画:http: //zulko.wordpress.com/2012/09/29/animate-your-3d-plots-with-pythons-matplotlib/

It provides simple function to animate around a plot and output in a number of formats.

它提供了简单的功能来围绕绘图设置动画并以多种格式输出。

回答by wil3

It's a bit of a hack, but if you are using Jupyter notebook you can use cell magics to run the command line version of ffmpeg directly from the notebook itself. In one cell run your script to generate raw frames

这有点小技巧,但如果您使用的是 Jupyter notebook,则可以使用 cell magics 直接从 notebook 本身运行命令行版本的 ffmpeg。在一个单元格中运行您的脚本以生成原始帧

from mpl_toolkits.mplot3d import Axes3D
ax = Axes3D(fig)
ax.scatter(xx,yy,zz, marker='o', s=20, c="goldenrod", alpha=0.6)
for ii in xrange(0,360,1):
    ax.view_init(elev=10., azim=ii)
    savefig("movie%d.png" % ii)

Now, in a new notebook cell, enter the following and then run the cell

现在,在一个新的笔记本单元格中,输入以下内容,然后运行该单元格

%%bash 
ffmpeg -r 30 -i movie%d.png -c:v libx264 -vf fps=25 -pix_fmt yuv420p out.mp4