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")
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.
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.
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.
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'])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With