- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Creating 3D animation using matplotlib
To create a 3D animation using matplotlib, we can take the following steps −
- Import the required packages. For 3D animation, you need to import Axes3D from mpl_toolkits.mplot3d and matplotlib.animation.
- Set the figure size and adjust the padding between and around the subplots.
- Create t, x, y and data points using numpy.
- Create a new figure or activate an existing figure.
- Get the instance of 3D axes.
- Turn off the axes.
- Plot the lines with data.
- Create an animation by repeatedly calling a function *animate*.
- To display the figure, use show() method.
Example
import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation from mpl_toolkits.mplot3d import Axes3D plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True def animate(num, data, line): colors = ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd', '#8c564b', '#e377c2', '#7f7f7f', '#bcbd22', '#17becf'] line.set_color(colors[num % len(colors)]) line.set_alpha(0.7) line.set_data(data[0:2, :num]) line.set_3d_properties(data[2, :num]) return line t = np.arange(0, 20, 0.2) x = np.cos(t) - 1 y = 1 / 2 * (np.cos(2 * t) - 1) data = np.array([x, y, t]) N = len(t) fig = plt.figure() ax = Axes3D(fig) ax.axis('off') line, = plt.plot(data[0], data[1], data[2], lw=7, c='red') line_ani = animation.FuncAnimation(fig, animate, frames=N, fargs=(data, line), interval=50, blit=False) plt.show()
Output
It will produce the following output
- Related Articles
- Creating a 3D plot in Matplotlib from a 3D numpy array
- Creating 3D Text using React-three-fiber
- Creating 3D Globe using React-three-fiber
- Animation using Matplotlib with subplots and ArtistAnimation
- How to plot 3D graphs using Python Matplotlib?
- Updating the X-axis values using Matplotlib animation
- Creating a 3D donut-like structure in React using react-three-fiber
- Creating a 3D Metal Texture Box in React using React-Three-Fiber
- How can I render 3D histograms in Python using Matplotlib?
- Creating a Particle Animation in React JS
- How to update the plot title with Matplotlib using animation?
- Animation with contours in matplotlib
- How to save Matplotlib 3d rotating plots?
- Animate a rotating 3D graph in Matplotlib
- Plot 3D bars without axes in Matplotlib

Advertisements