python - 在 matplotlib 中为旋转的 3D 图形制作动画

标签 python animation matplotlib

我设置了散点图并按照我想要的方式绘制,我想创建一个在空间中旋转的图形的 .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")

最佳答案

如果你想了解更多关于 matplotlib 动画的知识,你真的应该关注 this tutorial .它详细解释了如何创建动画情节。

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

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

# 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'])

关于python - 在 matplotlib 中为旋转的 3D 图形制作动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18344934/

相关文章:

python - 绘制 GeoDataFrame 一行的几何图形

javascript - Phaser3 旋转动画

android - 同一 Activity 中 RecyclerView 项目和 CollapsingToolbar 之间的共享元素动画

python - 如何使用 Matplotlib 设置图形背景颜色的不透明度

python - 在 Python 中迭代对象列表时打印 None

python - 使用 Regex + BeautifulSoup 抓取 XML 并存储到 Pandas

python - 如何加速seaborn热图

animation - 在 Three.js 中沿样条(圆)移动对象

python - 散点矩阵在图表上显示太多浮点值

python - matplotlib 中用于 SciPy 树状图的更大调色板 (Python)