Monday, November 18, 2024
Google search engine
HomeLanguagesHow to animate 3D Graph using Matplotlib?

How to animate 3D Graph using Matplotlib?

Prerequisites: Matplotlib, NumPy

Graphical representations are always easy to understand and are adopted and preferable before any written or verbal communication. With Matplotlib we can draw different types of Graphical data. In this article, we will try to understand, How can we create a beautiful graph using matplotlib and create a 3D animated Graph using Matplotlib.

Approach:

  • Import required module.
  • Create a 3d figure
  • Create sample data
  • Animate 360 views of the graph.
  • Display Graph.

Step 1: Import library.

Python3




from numpy import linspace
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d


Step 2: The purpose of using plt.figure() is to create a figure object. We will use plt.axes () to create separate sets of axes in which you will draw each.

Python3




from numpy import linspace
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
 
 
fig = plt.figure(figsize = (8,8))
ax = plt.axes(projection = '3d')


Step 3: In this step, we will create our data and plot different graphs.

Python3




from numpy import linspace
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from scipy import signal
 
fig = plt.figure(figsize = (8,8))
ax = plt.axes(projection='3d')
 
t = np.linspace(0, 1, 1000, endpoint=True)
ax.plot3D(t, signal.square(2 * np.pi * 5 * t))


Step 4: 360-degree movement of the graph.

view_init(elev=, azim=)This can be used to rotate the axes programmatically.‘elev’ stores the elevation angle in the z plane. ‘azim’ stores the azimuth angle in the x,y plane.D constructor. The draw() function in pyplot module of the matplotlib library is used to redraw the current figure

Python3




from numpy import linspace
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from scipy import signal
 
fig = plt.figure(figsize = (8,8))
ax = plt.axes(projection='3d')
 
t = np.linspace(0, 1, 1000, endpoint=True)
ax.plot3D(t, signal.square(2 * np.pi * 5 * t))
 
for angle in range(0, 360):
   ax.view_init(angle,30)
   plt.draw()
   plt.pause(.001)


Example 1: In this example, we plot a square wave, and we will see its 360-degree view.

Linspace(): A linspace function is a tool in Python for creating numeric sequences.The plot3D() function of matplotlib library is used to make a 3D  plotting.

Python3




from numpy import linspace
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from scipy import signal
 
# Creating 3D figure
fig = plt.figure(figsize = (8, 8))
ax = plt.axes(projection = '3d')
 
# Creating Dataset
t = np.linspace(0, 1, 1000, endpoint = True)
ax.plot3D(t, signal.square(2 * np.pi * 5 * t))
 
# 360 Degree view
for angle in range(0, 360):
   ax.view_init(angle, 30)
   plt.draw()
   plt.pause(.001)
     
plt.show()


Output:

Example 2: In this example, we plot a spiral graph, and we will see its 360-degree view

Python3




from numpy import linspace
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from scipy import signal
 
# Creating 3D figure
fig = plt.figure(figsize = (8,8))
ax = plt.axes(projection='3d')
 
# Creating Dataset
z = np.linspace(0, 15, 1000)
x = np.sin(z)
y = np.cos(z)
ax.plot3D(x, y, z, 'green')
 
# 360 Degree view
for angle in range(0, 360):
    ax.view_init(angle, 30)
    plt.draw()
    plt.pause(.001)
 
plt.show()


Output:

Example 3: In this example, we will display the Parabola Graph.

plt.rcParams(axes.prop_cycle):- Calling the ‘axes.prop_cycle’ which returns an itertools.cycle.
Linspace(): A linspace function is a tool in Python for creating numeric sequences.

Python3




from numpy import linspace
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from scipy import signal
 
# Creating 3D figure
fig = plt.figure(figsize = (8,8))
ax = plt.axes(projection = '3d')
 
# Creating Dataset
color_cycle = plt.rcParams['axes.prop_cycle']()
x = linspace(0, 1, 51)
a = x*( 1 - x)   
b = 0.25 - a   
c = x*x*(1 - x)
d = 0.25-c   
 
ax.plot3D(x, a, **next(color_cycle))
 
# 360 Degree view
for angle in range(0, 360):
   ax.view_init(angle, 30)
   plt.draw()
   plt.pause(.001)
 
plt.show()


Output:

RELATED ARTICLES

Most Popular

Recent Comments