Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize 3D drawing by Matplotlib in Python

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to achieve 3D drawing in Python Matplotlib, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Mpl_toolkits is the drawing kit for Matplotlib.

The first 3D drawing program

Let's write the first 3D drawing program.

To start by creating a 3D drawing area, the plt.axes () function provides a parameter projection that sets its parameter value to "3D". As follows:

# Import 3D toolkit mplot3dfrom mpl_toolkits import mplot3dimport numpy as npimport matplotlib.pyplot as pltfig = plt.figure () # create 3D drawing area ax = plt.axes (projection='3d')

With the 3D drawing area, the next step is to build a 3D image, as follows:

# build z = np.linspace (0,1100) x = z * np.sin (20 * z) y = z * np.cos (20 * z) from three dimensions

Finally, call the plot3D () method to draw the 3D graph, the code is as follows:

# call ax.plot3D to create a 3D line ax.plot3D (x, y, z, 'gray') ax.set_title (' 3D line plot') plt.show ()

The complete procedure is as follows:

From mpl_toolkits import mplot3dimport numpy as npimport matplotlib.pyplot as pltfig = plt.figure () # build z = np.linspace (0,1,100) x = z * np.sin (20 * z) y = z * np.cos (20 * z) # call ax.plot3D to create a 3D line ax.plot3D (x, y, z, 'gray') ax.set_title (' 3D line plot') plt.show ()

The output is as follows:

Figure 1: 3D Line Map (3D Line)

The ax.plot3D () function in the above code can draw a variety of three-dimensional graphics, which are created according to the triple class.

3D scatter plot

The 3D scatter chart can be drawn through the ax.scatter3D () function. The sample code is as follows:

From mpl_toolkits import mplot3dimport numpy as npimport matplotlib.pyplot as pltfig = plt.figure () # create drawing area ax = plt.axes (projection='3d') # build xyzz = np.linspace (0,1100) x = z * np.sin (20 * z) y = z * np.cos (20 * z) c = x + yax.scatter3D (x, y, z, c) ax.set_title ('3D Scatter plot') plt.show ()

Output result diagram:

Figure 2:Matplotlib 3D drawing

3D contour map

Ax.contour3D () can be used to create a three-dimensional contour map, which requires the input data to be in matrix coordinates in two-dimensional network format. At the same time, it can calculate a z value at each grid point (xther y).

The following example shows how to draw a 3D sinusoidal contour map. The code is as follows:

From mpl_toolkits import mplot3dimport numpy as npimport matplotlib.pyplot as pltdef f (x, y): return np.sin (np.sqrt (x * * 2 + y * * 2)) # build x, y data x = np.linspace (- 6,6,30) y = np.linspace (- 6,6,30) # gridding the data X, Y = np.meshgrid (x, y) Z = f (X) Y) fig = plt.figure () ax = plt.axes (projection='3d') # 50 represents the height level of the contours in the z-axis direction Binary color changes from white to black ax.contour3D (X, Y, Z, 50, cmap='binary') ax.set_xlabel ('x') ax.set_ylabel ('y') ax.set_zlabel ('z') ax.set_title ('3D contour') plt.show ()

The output result is as follows:

Figure 3: drawing a 3D outline

3D wireframe diagram

Wireframes also use data in the form of two-dimensional grids, similar to drawing contours.

Wireframe can project data onto a specified 3D surface and output 3D effects with high visualization. 3D wireframe can be drawn through plot_wireframe (). The code is as follows:

From mpl_toolkits import mplot3dimport numpy as npimport matplotlib.pyplot as plt# to draw the function image def f (XMagi y): return np.sin (np.sqrt (x * * 2 + y * * 2)) # prepare the x Magi y data x = np.linspace (- 6,6,30) y = np.linspace (- 6,6,30) # generate x, y gridded data X, Y = np.meshgrid (XMague y) # prepare z value Z = f (X) Y) # drawing image fig = plt.figure () ax = plt.axes (projection='3d') # call the wireframe drawing function plot_wireframe () ax.plot_wireframe (X, Y, Z, color='black') ax.set_title ('wireframe') plt.show ()

The output is as follows:

Drawing 4:Matplotlib drawing wireframe

3D surface graph

The surface graph represents the functional relationship between a specified dependent variable y and two independent variables x and z.

A 3D surface graph is a three-dimensional graph, which is very similar to a wireframe. The difference is that each face of the wireframe is filled with polygons. The plot_surface () function provided by Matplotlib can draw 3D surfaces, which takes three parameter values, x _ journal y and z. The sample code is as follows:

From mpl_toolkits import mplot3dimport numpy as npimport matplotlib.pyplot as plt# vector product (outer () method is also called outer product) x = np.outer (np.linspace (- 2,2,30), np.ones (30)) # Matrix transpose y = x.copy (). T # data zz = np.cos (x * * 2 + y * * 2) # drawing surface fig = plt.figure () ax = plt.axes (projection='3d') call plot_surface () function ax.plot_surface (x, y) Edgecolor='none') ax.set_title ('Surface plot') plt.show ()

Output result diagram:

Drawing 5:Matplotlib drawing a surface

The above is all the content of the article "how to realize 3D drawing with Matplotlib in Python". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report