Animate a rotating 3D graph in matplotlib

前端 未结 3 1529
清歌不尽
清歌不尽 2021-01-30 11:17

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 drag

3条回答
  •  余生分开走
    2021-01-30 12:02

    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

    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
    

提交回复
热议问题