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.
Python3fromnumpyimportlinspaceimportmatplotlib.pyplotaspltfrommpl_toolkitsimportmplot3d
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.
Python3fromnumpyimportlinspaceimportmatplotlib.pyplotaspltfrommpl_toolkitsimportmplot3dfig=plt.figure(figsize=(8,8))ax=plt.axes(projection='3d')
Step 3:In this step, we will create our data and plot different graphs.
Python3fromnumpyimportlinspaceimportmatplotlib.pyplotaspltfrommpl_toolkitsimportmplot3dfromscipyimportsignalfig=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
Python3fromnumpyimportlinspaceimportmatplotlib.pyplotaspltfrommpl_toolkitsimportmplot3dfromscipyimportsignalfig=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))forangleinrange(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.
Python3fromnumpyimportlinspaceimportnumpyasnpimportmatplotlib.pyplotaspltfrommpl_toolkitsimportmplot3dfromscipyimportsignal# Creating 3D figurefig=plt.figure(figsize=(8,8))ax=plt.axes(projection='3d')# Creating Datasett=np.linspace(0,1,1000,endpoint=True)ax.plot3D(t,signal.square(2*np.pi*5*t))# 360 Degree viewforangleinrange(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
Python3fromnumpyimportlinspaceimportnumpyasnpimportmatplotlib.pyplotaspltfrommpl_toolkitsimportmplot3dfromscipyimportsignal# Creating 3D figurefig=plt.figure(figsize=(8,8))ax=plt.axes(projection='3d')# Creating Datasetz=np.linspace(0,15,1000)x=np.sin(z)y=np.cos(z)ax.plot3D(x,y,z,'green')# 360 Degree viewforangleinrange(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.
Python3fromnumpyimportlinspaceimportnumpyasnpimportmatplotlib.pyplotaspltfrommpl_toolkitsimportmplot3dfromscipyimportsignal# Creating 3D figurefig=plt.figure(figsize=(8,8))ax=plt.axes(projection='3d')# Creating Datasetcolor_cycle=plt.rcParams['axes.prop_cycle']()x=linspace(0,1,51)a=x*(1-x)b=0.25-ac=x*x*(1-x)d=0.25-cax.plot3D(x,a,**next(color_cycle))# 360 Degree viewforangleinrange(0,360):ax.view_init(angle,30)plt.draw()plt.pause(.001)plt.show()
Output: