Python Matplotlib implements the sample code for drawing 3D images, pythonmatplotlib
Matplotlib can also draw 3D images. Unlike 2D images, 3D images are drawn mainly through the mplot3d module. However, the use of Matplotlib to draw a three-dimensional image is actually displayed on a two-dimensional canvas. Therefore, when drawing a three-dimensional image, the pyplot module also needs to be loaded.
The mplot3d module consists of four main categories:
- Mpl_toolkits.mplot3d.axes3d ()
- Mpl_toolkits.mplot3d.axis3d ()
- Mpl_toolkits.mplot3d.art3d ()
- Mpl_toolkits.mplot3d.proj3d ()
Axes3d () mainly contains various classes and methods for drawing. Axis3d () mainly contains classes and methods related to coordinate axes. Art3d () Contains classes and methods that convert 2D images and use them for 3D rendering. Proj3d () contains several fragmented classes and methods, such as calculating the length of a three-dimensional vector.
Generally, mpl_toolkits.mplot3d.axes3d () is the mpl_toolkits.mplot3d.axes3d.Axes3D () class, while Axes3D () has a method to draw different types of 3D images. You can import Axes3D () in the following way ().
From mpl_toolkits.mplot3d.axes3d import Axes3D or from mpl_toolkits.mplot3d import Axes3D
3D scatter chart
First, we import numpy to randomly generate a group of data.
Import numpy as np # x, y, and z are all 100 random numbers between 0 and 1 x = np. random. normally (0, 1,100) y = np. random. normal (0, 1,100) z = np. random. normal (0, 1,100)
Next, start plotting. The first step is to load the 2D and 3D drawing modules.
from mpl_toolkits.mplot3d import Axes3Dimport matplotlib.pyplot as plt
Step 2: Use Axes3D () to create a 3D image object.
fig = plt.figure()ax = Axes3D(fig)
Finally, call the scatter plot drawing method and display it.
ax.scatter(x, y, z)plt.show()
3D line chart
A linear chart is similar to a scatter chart. You need to input values of the x, y, and z coordinates. The detailed code is as follows.
# Load module from mpl_toolkits.mplot3d import Axes3Dimport matplotlib. pyplot as pltimport numpy as np # generate data x = np. linspace (-6 * np. pi, 6 * np. pi, 1000) y = np. sin (x) z = np. cos (x) # create a 3D image object fig = plt. figure () ax = Axes3D (fig) # draw a line chart ax. plot (x, y, z) # displays the graph plt. show ()
3D bar chart
After drawing the line chart, we will continue to try to draw a three-dimensional column chart. In fact, the drawing steps are similar to those above.
# Load module from mpl_toolkits.mplot3d import Axes3Dimport matplotlib. pyplot as pltimport numpy as np # create a 3D image object fig = plt. figure () ax = Axes3D (fig) # generate data and plot x = [0, 1, 2, 3, 4, 5, 6] for I in x: y = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] z = abs (np. random. normal (1, 10, 10) ax. bar (y, z, I, zdir = 'y', color = ['R', 'G', 'B', 'y']) plt. show ()
3D plot and surface Diagram
Next we need to draw a 3D curved surface map, which is a little complicated. We need to process the data in a matrix. In fact, it is very similar to drawing a two-dimensional contour map, but it only adds a dimension.
# Import numpy as npimport matplotlib. pyplot as pltfrom mpl_toolkits.mplot3d import Axes3D # create a 3D image object fig = plt. figure () ax = Axes3D (fig) # generate data X = np. arange (-2, 2, 0.1) Y = np. arange (-2, 2, 0.1) X, Y = np. meshgrid (X, Y) Z = np. sqrt (X ** 2 + Y ** 2) # plot a curved surface and use cmap to color ax. plot_surface (X, Y, Z, cmap = plt. cm. winter) plt. show ()
Cmap = plt. cm. winter indicates that the winter color scheme is used, which means gradient.
Hybrid graph rendering
A hybrid graph is used to draw two different types of graphs in one worker. There are usually preconditions for creating a hybrid graph, that is, the range of the two different types of graphs is roughly the same, otherwise there will be serious proportional inconsistencies, making the hybrid graph meaningless.
#-*-Coding: UTF-8-* # loading module from mpl_toolkits.mplot3d import Axes3Dimport numpy as npimport matplotlib. pyplot as plt # create a 3D image object fig = plt. figure () ax = Axes3D (fig) # generate the data and draw the figure 1x1 = np. linspace (-3 * np. pi, 3 * np. pi, 500) y1 = np. sin (x1) ax. plot (x1, y1, zs = 0, c = 'red') # generate data and draw Figure 2x2 = np. random. normally (0, 1,100) y2 = np. random. normally (0, 1,100) z2 = np. random. normal (0, 1,100) ax. scatter (x2, y2, z2) # displays the graph plt. show ()
Subgraph Rendering
#-*-Coding: UTF-8-* # loading module from mpl_toolkits.mplot3d import Axes3Dimport matplotlib. pyplot as pltimport numpy as np # create a canvas fig = plt. figure () #==================## Add a subgraph 1 ax1 = fig to the canvas. add_subplot (1, 2, 1, projection = '3d ') # generate subgraph 1 Data x = np. linspace (-6 * np. pi, 6 * np. pi, 1000) y = np. sin (x) z = np. cos (x) # Draw 1st images ax1.plot (x, y, z) #==================## Add a subgraph 2ax2 = fig to the canvas. add_subplot (1, 2, 2, projection = '3d ') # generate subgraph 2 Data X = np. arange (-2, 2, 0.1) Y = np. arange (-2, 2, 0.1) X, Y = np. meshgrid (X, Y) Z = np. sqrt (X ** 2 + Y ** 2) # plot the ax2.plot _ surface (X, Y, Z, cmap = plt. cm. winter) # display the graph plt. show ()
Let's take a look at the code. Because the two subgraphs are drawn on one canvas, you need to create one canvas in advance. Then, add a subgraph through. add_subplot (). The sequence number of the subgraph is similar to that of the two-dimensional drawing. You only need to add the projection = '3d 'parameter when drawing 3D.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.