Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animate a rotating 3D graph in matplotlib

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.

This answer is 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:

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") 
like image 990
Nate Avatar asked Aug 20 '13 21:08

Nate


People also ask

How do you animate a 3D graph in Python?

To pass a function into animation class, make a user-defined function that removes the previous plot and plot a surface using x, y and z-array. Create a new figure or activate an existing figure. Add a subplot arrangement using subplots() method. Set the Z-axis limit using set_zlim() method.

How do I rotate a graph in matplotlib?

Rotate X-Axis Tick Labels in Matplotlib There are two ways to go about it - change it on the Figure-level using plt. xticks() or change it on an Axes-level by using tick. set_rotation() individually, or even by using ax.

How do you rotate a 3D graph?

Rotating 3D GraphsTo rotate the 3D graph around the X axis press CTRL and drag. To rotate around the Y axis, press SHIFT and drag. To rotate the 3D graph in any direction, press CTRL + SHIFT and drag the graph.


1 Answers

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

Note: Creating animated plots require ffmpeg or mencoder to be installed.

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']) 
like image 158
Viktor Kerkez Avatar answered Sep 28 '22 06:09

Viktor Kerkez