current position:Home>Python Matplotlib drawing 3D graphics
Python Matplotlib drawing 3D graphics
2022-02-01 02:15:08 【Little cute in the circle of friends】
This is my participation 11 The fourth of the yuegengwen challenge 19 God , Check out the activity details :2021 One last more challenge
Preface
stay matplotlib At first, the module only supported drawing 2D graphics , For example, we have also learned to draw a line chart before 、 Histogram 、 Histogram and other statistical charts , At the same time, learn to draw multiple subgraphs 、 Common graphics, etc . Here are some examples of previous articles , For reference
-
matplotlib The underlying structure :matplotlib The module is divided into script layer 、 Art layer and back end , Collaborative workflow learning
-
matplotlib Draw line chart :pyplot.plot() Draw a line chart and summarize the related attributes
-
matplotlib Draw a histogram :pyplot.bar() Draw the relevant attributes of the Shao histogram to summarize and explain
-
matplotlib Draw histogram :pyolot.hist() Draw histogram related attributes for summary description
-
matplotlib Drawing graphics : For common rectangles 、 Introduction to drawing methods of circle and other graphics
With the development of technology ,matplotlib Modules in 2D The basis of drawing , Encapsulate practical 3D Drawing toolkit mplot3d To support us to draw faster 3D graphics .
In this issue , We will study in detail 3D Drawing module mplot3d Related drawing methods ,Let's go~
1. mplot3d summary
mplot3d Kuo is matplotlib The module is dedicated to drawing 3D The image provides 2D Projective Axes object , by Matplotlib Added the ability to plot scatter plot 、 Surface graph 、 Broken line diagram 、 Grid, etc .
-
mplot3d characteristic
- mplot3d Allow users to create matplotlib 2D Drawing is simple 3D graphics
- mplot3d The method is easy to use , Directly available 2D Method to draw
- mplot3d draw 3D Image and 2D Images share the same rendering engine , There may be Z Deviation of axis projection
- mplot3d Styles can be personalized by changing parameters
-
mplot3d Use
We are using mplot3d When it comes to toolkits , Need extra use from...import Import Axes3D class
from mpl_toolkits.mplot3d import Axes3D
Copy code
PS:
- pyplot Class cannot 3D Add content to the drawing 、 Handle 3D Additional information, etc , You have to use Axes3D Object to create
- mplot3d draw 3D Graphical methods are not yet mature , For complex 3D Figure scene , It is recommended to use Mayavi
2. draw 3D Graphic steps
stay matplotlib Drawing is provided in the module 3D Graphics need mplot3d in Axes3D Object and the pyplot Methods used in combination , So draw 3D There are the following steps for graphics
- Import matplotlib.pyplot and mpl_toolkits.mplot3d in Axes3D class
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
Copy code
- Use Pyplot.figure Create a fig Canvas object
fig = plt.figure()
Copy code
-
stay fig Create a band in the canvas 3D Coordinate Axes object
- Mode one : Use to create subgraphs through projection='3D' To create
ax = fig.add_subplot(projection='3d') Copy code
- Mode two : call Axes3D Class to create objects
ax = Axes3D(fig) Copy code
-
call numpy.random perhaps numpy.arange() And so on x,y Axis data
x = np.arange(-5,5,0.25)
y = np.arange(-5,5,0.25)
Copy code
- call numpy.meshgrid() Method pair x,y Mapping matrix
x,y = np.meshgrid(x,y)
Copy code
- according to x,y The matrix data is calculated according to the demand Z Axis data , For example, call numpy.sin()、cos() function
R = np.sqrt(x**2+y**2)
z = np.cos(R)
Copy code
- Axes Object call pyplot Charting 、 Drawing by graphic method , For example, call contour map contour、 Surfaces plot_surface()
ax.plot_surface(x,y,z,rstride=1,cstride=1,alpha=0.5,cmap=cm.coolwarm)
ax.contour(x,y,z,zdir='z',offset=-2)
Copy code
- Axes Object call xlim,ylim,zlim Method setting x,y,z Range of axis values
ax.set_zlim(-2,2)
Copy code
- Last call pyplot.show() Show the drawn image
plt.show()
Copy code
- The effect of the display is shown in the figure
3. draw 3D Scatter plot
- Use numpy.arange()|numpy.random.randint() Get ready x,y,z Axis of the data
- Axes Object call scatter Scatter plot method to draw scatter plot
x = np.arange(0,200)
y = np.arange(0,100)
x,y = np.meshgrid(x,y)
z = np.random.randint(0,200,size=(100,200))
yc = np.arctan2(x,y)
ax.scatter(x,y,z,c=yc,s=50,marker=".")
Copy code
4. draw 3D Broken line diagram
- Use np.linspace() and np.sin() Yes x,y Axis data preparation
- Axes Object call plot() Draw a line chart with the line method
x = np.linspace(0,1,100)
y = np.sin(x*2*np.pi)/2+0.5
ax.plot(x,y,zs=0,zdir="z")
Copy code
5. draw 3D Histogram
for z in range(0,3):
x = np.arange(1,13)
y = 1000*np.random.rand(12)
color = plt.cm.Set2(random.choice(range(plt.cm.Set2.N)))
ax.bar(x,y,zs=z,zdir="y",color=color,alpha=0.8)
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("z")
Copy code
summary
In this issue , Yes matplotlib Module supply mplot3d Library drawing 3D Learn the steps of graphics , At the same time, manually operate the scatter diagram 、 Broken line 、 Histogram 3D chart .
about , draw 3D Graphics must now use Axes3D Object to draw , For complex 3D graphics matplotlib The rendering engine renders poorly , Official recommended use Mayavi To deal with .
The above is the content of this issue , Welcome big guys to praise and comment , See you next time ~
copyright notice
author[Little cute in the circle of friends],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202010215053746.html
The sidebar is recommended
- Python avatar animation, come and generate your own animation avatar
- leetcode 1884. Egg Drop With 2 Eggs and N Floors(python)
- leetcode 1910. Remove All Occurrences of a Substring(python)
- Python and binary
- First acquaintance with Python class
- [Python data collection] scrapy book acquisition and coding analysis
- Python crawler from introduction to mastery (IV) extracting information from web pages
- Python crawler from entry to mastery (III) implementation of simple crawler
- The apscheduler module in Python implements scheduled tasks
- 1379. Find the same node in the cloned binary tree (Java / C + + / Python)
guess what you like
-
Python connects redis, singleton and thread pool, and resolves problems encountered
-
Python from 0 to 1 (day 11) - Python data application 1
-
Python bisect module
-
Python + OpenGL realizes real-time interactive writing on blocks with B-spline curves
-
Use the properties of Python VTK implicit functions to select and cut data
-
Learn these 10000 passages and become a humorous person in the IT workplace. Python crawler lessons 8-9
-
leetcode 986. Interval List Intersections(python)
-
leetcode 1860. Incremental Memory Leak(python)
-
How to teach yourself Python? How long will it take?
-
Python Matplotlib drawing pie chart
Random recommended
- Django paging (II)
- Concurrent. For Python concurrent programming Futures or multiprocessing?
- Programmers over the age of 25 can't know a few Chinese herbal medicines. Python crawler lessons 9-9
- Python crawler from introduction to pit full series of tutorials (detailed tutorial + various practical combat)
- The second bullet of class in Python
- Python object oriented programming 03: class inheritance and its derived terms
- How IOS developers learn Python Programming 13 - function 4
- Python crawler from introduction to mastery (VI) form and crawler login
- Python crawler from entry to mastery (V) challenges of dynamic web pages
- Deeply understand pandas to read excel, TXT, CSV files and other commands
- Daily python, Chapter 18, class
- "I just want to collect some plain photos in Python for machine learning," he said. "I believe you a ghost!"
- Django view
- Python implements filtering emoticons in text
- When winter comes, python chooses a coat with temperament for mom! Otherwise, there's really no way to start!
- Python crawler - get fund change information
- Highlight actor using Python VTK
- Python crawler actual combat: crawling southern weekend news articles
- leetcode 406. Queue Reconstruction by Height(python)
- leetcode 1043. Partition Array for Maximum Sum (python)
- Python * * packaging and unpacking details
- Python realizes weather query function
- Python from 0 to 1 (day 12) - Python data application 2 (STR function)
- Python from 0 to 1 (day 13) - Python data application 3
- Numpy common operations of Python data analysis series Chapter 8
- How to implement mockserver [Python version]
- Van * Python! Write an article and publish the script on multiple platforms
- Python data analysis - file reading
- Python data De duplication and missing value processing
- Python office automation - play with browser
- Python series tutorial 127 -- Reference vs copy
- Control flow in Python: break and continue
- Teach you how to extract tables in PDF with Python
- leetcode 889. Construct Binary Tree from Preorder and Postorder Traversal(python)
- leetcode 1338. Reduce Array Size to The Half(python)
- Object oriented and exception handling in Python
- How to configure load balancing for Django service
- How to embed Python in go
- Python Matplotlib drawing graphics
- Python object-oriented programming 05: concluding summary of classes and objects