#342 Animation on 3D plot

It is possible to create a 3D object with python. See the dedicated section. Once this is done, we can make evolute the angle of view (‘camera position’) and use each image to make an animation. Once more, the image are transformed to a GIF using Image magic.

# library
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

# Get the data (csv file is hosted on the web)
url = 'https://python-graph-gallery.com/wp-content/uploads/volcano.csv'
data = pd.read_csv(url)

# Transform it to a long format

# And transform the old column name in something numeric

# We are going to do 20 plots, for 20 different angles
for angle in range(70,210,2):

# Make the plot
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_trisurf(df['Y'], df['X'], df['Z'], cmap=plt.cm.viridis, linewidth=0.2)

# Set the angle of the camera

# Save it
plt.savefig(filename, dpi=96)

Then use image magic to transform all the png files into an GIF file.

# Then use image magick (this is bash, not python)
convert -delay 50 Volcano*.png animated_volcano.gif

Leave a Reply

Your email address will not be published.