Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Working with Images in Python using Matplotlib
Next article icon

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
fromnumpyimportlinspaceimportmatplotlib.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.

Python3
fromnumpyimportlinspaceimportmatplotlib.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.

Python3
fromnumpyimportlinspaceimportmatplotlib.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

Python3
fromnumpyimportlinspaceimportmatplotlib.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.

Python3
fromnumpyimportlinspaceimportnumpyasnpimportmatplotlib.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

Python3
fromnumpyimportlinspaceimportnumpyasnpimportmatplotlib.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.

Python3
fromnumpyimportlinspaceimportnumpyasnpimportmatplotlib.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:


Improve
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp