SoFunction
Updated on 2024-11-17

Python three-dimensional plotting of Matplotlib library to use

preamble

When encountering three-dimensional data, three-dimensional images can give us a deeper understanding of the data. python's matplotlib library contains a rich set of three-dimensional plotting tools.

1. Creating 3D Axes Object Axes3D

There are two main ways to create Axes3D, one is to utilize the keywordprojection='3d'l to do so, and the other is achieved by taking the data from thempl_toolkits.mplot3dImporting objects into Axes3D to achieve this is all about generating objects in Axes3D with a 3D format.

#Method 1, using keywords
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

#Define the axes
fig = ()
ax1 = (projection='3d')
#ax = fig.add_subplot(111,projection='3d') #This method can also draw multiple subplots


#Method two, utilizing the three-dimensional axis method
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# Define image and 3D format axes
fig=()
ax2 = Axes3D(fig)

2. 3D curves and scattering points

Subsequently graph on the defined axes:

import numpy as np
z = (0,13,1000)
x = 5*(z)
y = 5*(z)
zd = 13*(100)
xd = 5*(zd)
yd = 5*(zd)
ax1.scatter3D(xd,yd,zd, cmap='Blues') # Plotting scatterplots
ax1.plot3D(x,y,z,'gray') # Plotting space curves
()

在这里插入图片描述

3. Three-dimensional surfaces

The next step is to draw three-dimensionalmeshes

fig = () # Define new 3D axes
ax3 = (projection='3d')

#Define 3D data
xx = (-5,5,0.5)
yy = (-5,5,0.5)
X, Y = (xx, yy)
Z = (X)+(Y)


#Mapping
ax3.plot_surface(X,Y,Z,cmap='rainbow')
#(X,Y,Z, zdim='z',offset=-2, cmap='rainbow) #contour map, to set offset, as the minimum value of Z
()

在这里插入图片描述

If you add the step size during rendering, you will get a sharper and more detailed image:
ax3.plot_surface(X,Y,Z,rstride = 1, cstride = 1,cmap='rainbow')The row and cloum_stride are the sampling steps of the drawing in horizontal and vertical directions, and the smaller they are, the finer the drawing will be.

在这里插入图片描述

4. Contour lines

It is also possible to placecontour lineProjection onto different surfaces:

from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

#Define the axes
fig4 = ()
ax4 = (projection='3d')

# Generate 3D data
xx = (-5,5,0.1)
yy = (-5,5,0.1)
X, Y = (xx, yy)
Z = ((X**2+Y**2))

#Mapping
ax4.plot_surface(X,Y,Z,alpha=0.3,cmap='winter')   # Generate surfaces, alpha is used to control transparency
(X,Y,Z,zdir='z', offset=-3,cmap="rainbow") # Generate a z-direction projection onto the x-y plane
(X,Y,Z,zdir='x', offset=-6,cmap="rainbow") # Generate x-direction projection to y-z plane
(X,Y,Z,zdir='y', offset=6,cmap="rainbow")  # Generate a y-direction projection onto the x-z plane
#(X,Y,Z,zdir='y', offset=6,cmap="rainbow") #Generate y-direction projection fill, cast to x-z plane, contourf() function

#Set display range
ax4.set_xlabel('X')
ax4.set_xlim(-6, 4) # Pull apart the range of axes to show the projection
ax4.set_ylabel('Y')
ax4.set_ylim(-4, 6)
ax4.set_zlabel('Z')
ax4.set_zlim(-3, 3)

()

在这里插入图片描述在这里插入图片描述

5. Randomized Scatterplot

You can use scatter() to generate scatter plots of various sizes and colors with the following parameters:

# Function Definition
(x, y, 
	s=None,  #array scalar
	c=None,  # Color sequence array, sequence
	marker=None,  # Points of Style
	cmap=None,  #colormap Color Styles
	norm=None,  #Normalized Normalized color camp
	vmin=None, vmax=None,  # corresponds to the normalized range above
 	alpha=None,   #Transparency
	linewidths=None,  #Line width
	verts=None,  #
	edgecolors=None, #Edge color
	data=None, 
	**kwargs
	)
#ref:/api/_as_gen/
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

#Define the axes
fig4 = ()
ax4 = (projection='3d')

# Generate 3D data
xx = (20)*10-5  # Take 100 random numbers in the range of 5~5
yy = (20)*10-5
X, Y = (xx, yy)
Z = ((X**2+Y**2))

#Mapping
(X,Y,Z,alpha=0.3,c=(400),s=(10,20, size=(20, 40)))   # Generate scatter. Use c to control the color sequence and s to control the size.

#Set display range

()

在这里插入图片描述

Finish

Todo bar

summarize

This article on the use of Python three-dimensional plotting Matplotlib library is introduced to this article, more related to Python three-dimensional plotting Matplotlib library content, please search for my previous posts or continue to browse the following related articles I hope you will support me in the future!