Introduction to the Matplotlib implementation of the 3D diagram method in Python

Source: Internet
Author: User
This article mainly introduces the Python matplotlib implementation of the 3D diagram of the sample code, with a certain reference value, interested can understand

Matplotlib can also draw 3D images, unlike two-dimensional images, drawing three-dimensional images mainly through the Mplot3d module implementation. However, using matplotlib to draw a three-dimensional image is actually shown on a two-dimensional canvas, so it is also necessary to load the Pyplot module when drawing a three-dimensional image.
The Mplot3d module consists of 4 major categories, namely:

    • Mpl_toolkits.mplot3d.axes3d ()

    • Mpl_toolkits.mplot3d.axis3d ()

    • Mpl_toolkits.mplot3d.art3d ()

    • Mpl_toolkits.mplot3d.proj3d ()

Among them, Axes3d () below mainly contains a variety of classes and methods to implement the drawing. Axis3d () mainly contains classes and methods related to axes. Art3d () contains classes and methods that convert 2D images and are used for 3D plotting. Proj3d () contains some fragmented classes and methods, such as calculating three-dimensional vector lengths.

In general, we use most of the Mpl_toolkits.mplot3d.axes3d.Axes3D () class in Mpl_toolkits.mplot3d.axes3d (), and Axes3d () has a method to draw different types of 3D graphs. You can import Axes3d () in the following way.

From Mpl_toolkits.mplot3d.axes3d import axes3d or from Mpl_toolkits.mplot3d import axes3d

Three-dimensional scatter plot

First, we import numpy to randomly generate a set of data.


Import NumPy as np# x, Y, Z are 100 random numbers from 0 to 1 x = Np.random.normal (0, 1, +) y = np.random.normal (0, 1, +) z = Np.rand Om.normal (0, 1, 100)

Next, start the drawing. The first step is to load the 3D drawing module.


From Mpl_toolkits.mplot3d import Axes3dimport Matplotlib.pyplot as Plt

The second step is to create a 3D drawing object using Axes3d ().


Fig = plt.figure () ax = axes3d (Fig)

Finally, the scatter plot plot method is called and displayed.


Ax.scatter (x, Y, z) plt.show ()

Three-dimensional line diagram

The line and scatter graphs are similar and need to pass in the values of x, Y, z three 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 * n P.pi, 6 * np.pi, +) y = np.sin (x) z = Np.cos (x) # Create 3D Graphics Object fig = plt.figure () ax = axes3d (Fig) # Draw Linetype Graph Ax.plot (x, Y, z) # Show graph Plt.show ()

Three-dimensional bar chart

After drawing the line chart, we continue to try to draw the three-dimensional histogram, in fact, its drawing steps are similar to the above.


# Load module from Mpl_toolkits.mplot3d import Axes3dimport Matplotlib.pyplot as Pltimport numpy as np# create 3D Graphics Object fig = Plt.figure () ax = axes3d (FIG) # generates data and plots 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.rand Om.normal (1, ten, ten))  Ax.bar (y, z, I, zdir= ' y ', color=[' r ', ' G ', ' B ', ' Y ')) plt.show ()

Three-dimensional graph surface diagram

The next three-dimensional surface plot that needs to be drawn is a bit cumbersome, and we need to do a matrix processing of the data. In fact, it is similar to drawing a two-dimensional contour, but it only adds a dimension.


# Load Module Import numpy as Npimport Matplotlib.pyplot as Pltfrom mpl_toolkits.mplot3d import axes3d# Create 3D Graphics Object fig = Plt.figure () ax = axes3d (FIG) # generates 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) # Draw a surface chart and use CMap coloring Ax.plot_surface (X, Y, Z, Cmap=plt.cm.winter) plt.show ()

Cmap=plt.cm.winter said that the winter color scheme, which is the gradient color, was adopted.

Mixed graph Drawing

A mixed graph is one in which two different types of graphs are plotted in a single picture. There are general prerequisites for drawing mixed graphs, that is, the extent of the two different types of graphs is roughly the same, otherwise there will be serious scale uncoordinated, which makes the mixed graph meaningless.


#-*-coding:utf-8-*# load module from Mpl_toolkits.mplot3d import Axes3dimport numpy as Npimport matplotlib.pyplot as plt# create 3  D Graphical Object fig = plt.figure () ax = axes3d (Fig) # Generate data and draw diagram 1x1 = Np.linspace ( -3 * np.pi, 3 * np.pi, +) y1 = Np.sin (x1) ax.plot (x1,  Y1, zs=0, c= ' red ') # Generate data and draw the graph 2x2 = np.random.normal (0, 1, 0) y2 = np.random.normal (1, +) z2 = np.random.normal (0, 1, Ax.scatter (x2, Y2, Z2) # Show Figure Plt.show ()

Child Graph Drawing


#-*-coding:utf-8-*# load module from Mpl_toolkits.mplot3d import Axes3dimport Matplotlib.pyplot as Pltimport numpy as np# create 1 Drawing Canvas Fig = plt.figure () #===============# Adding a sub-figure to the canvas 1 ax1 = Fig.add_subplot (1, 2, 1, projection= ' 3d ') # Generate sub-figure 1 Data x = Np.linspac E ( -6 * np.pi, 6 * np.pi, +) y = np.sin (x) z = Np.cos (x) # Draw 1th diagram Ax1.plot (x, Y, z) #===============# Add a sub-diagram to the canvas 2ax2 = Fig.ad D_subplot (1, 2, 2, projection= ' 3d ') # Generate sub-figure 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) # Draw 2nd Figure Ax2.plot_surface (x, Y, Z, Cmap=plt.cm.winter) # Show Figure Plt.show ()

We can look at the code. Since the two sub-graphs are drawn on top of 1 canvases, it is necessary to create 1 canvases in advance. Then add the sub-diagram by. Add_subplot (), with the sub-chart ordinal and the two-dimensional drawing similar, just note that the 3D drawing is added with the projection= ' 3d ' parameter.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.