我有一个散点图设置并按照我想要的方式绘制,我想创建一个在空间中旋转的图形的.mp4视频,就像我使用plt.show()
并拖动视点一样。
This answer几乎就是我想要的,除了保存电影我必须手动调用带有图像文件夹的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")
答案 0 :(得分:27)
如果您想了解有关matplotlib
动画的更多信息,请务必遵循this tutorial。它详细解释了如何创建动画图。
注意:创建动画图需要安装ffmpeg
或mencoder
。
以下是他的第一个示例的版本已更改为与散点图一起使用。
# 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'])
答案 1 :(得分:9)
当我偶然发现这个时,我正在研究用matplotlib制作动画的动画: http://zulko.wordpress.com/2012/09/29/animate-your-3d-plots-with-pythons-matplotlib/
它提供了简单的功能,可以围绕绘图和多种格式的输出进行动画处理。
答案 2 :(得分:1)
这有点骇人听闻,但如果您使用Jupyter笔记本,则可以使用单元魔术直接从笔记本本身运行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)
现在,在新的笔记本单元格中,输入以下内容,然后运行该单元格
%%bash
ffmpeg -r 30 -i movie%d.png -c:v libx264 -vf fps=25 -pix_fmt yuv420p out.mp4
答案 3 :(得分:0)
Viktor Kerkez 提供的解决方案对我不起作用(matplotlib 版本 3.4.2)。我收到与 user3015729 报告相同的错误。以下是产生相同结果但适用于 matplotlib 3.4.2 的改编版本。
# 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)
scat = ax.scatter(xx, yy, zz, marker='o', s=20, c="goldenrod", alpha=0.6)
# 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.view_init(elev=10., azim=0)
return [scat]
def animate(i):
ax.view_init(elev=10., azim=i)
return [scat]
# 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'])