当前位置:网站首页>Matplotlib drawing 3D graphics

Matplotlib drawing 3D graphics

2022-07-07 19:22:00 En^_^ Joy

use Matplotlib Draw a three-dimensional picture

Three dimensional drawing needs mplot3d modular , Add projection='3d' keyword , To create a three-dimensional coordinate axis

import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
import numpy as np

fig = plt.figure()
ax = plt.axes(projection='3d')

#  Drawing code 

#  display picture 
plt.show()

The following code omits these codes , Put the following code into the drawing code
 Insert picture description here

3D data points and lines

use ax.plot3D And ax.scatter3D To create line and scatter diagrams of three-dimensional coordinates , The parameters of three-dimensional function are basically the same as those of two-dimensional function

#  Data of 3D lines 
zline = np.linspace(0, 15, 1000)
xline = np.sin(zline)
yline = np.cos(zline)
ax.plot3D(xline, yline, zline, 'gray')

#  Three dimensional scatter data 
zdata = 15*np.random.random(100)
xdata = np.sin(zdata)+0.1*np.random.randn(100)
ydata = np.cos(zdata)+0.1*np.random.randn(100)
ax.scatter3D(xdata, ydata, zdata, c=zdata, cmap='Greens')

 Insert picture description here
By default , Scatter will automatically change the transparency , To present a three-dimensional sense on the plane

Three dimensional contour map

ax.contour3D All data is required to be in the format of two-dimensional grid data , Calculated by function z Axis numerical

def f(x, y):
    return np.sin(np.sqrt(x**2 + y**2))

x = np.linspace(-6, 6, 30)
y = np.linspace(-6, 6, 30)

X, Y = np.meshgrid(x, y)
Z = f(X, Y)

ax.contour3D(X, Y, Z, 50, cmap='binary')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')

 Insert picture description here
view_init You can adjust the observation angle and azimuth , In this case , Adjust the pitch angle to 60 degree , The azimuth is adjusted to 35 degree , Add the following line of code to the above code :

ax.view_init(60, 35)

 Insert picture description here
You can also click and drag the figure in the figure to change the angle

Wireframe and surface diagrams

ax.plot_wireframe Drawing wireframes

def f(x, y):
    return np.sin(np.sqrt(x**2 + y**2))

x = np.linspace(-6, 6, 30)
y = np.linspace(-6, 6, 30)

X, Y = np.meshgrid(x, y)
Z = f(X, Y)

ax.plot_wireframe(X, Y, Z, color='black')
ax.set_title('wireframe')

 Insert picture description here
ax.plot_surface Draw the surface

def f(x, y):
    return np.sin(np.sqrt(x**2 + y**2))

x = np.linspace(-6, 6, 30)
y = np.linspace(-6, 6, 30)

X, Y = np.meshgrid(x, y)
Z = f(X, Y)

ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='viridis', edgecolor='none')
ax.set_title('surface')

 Insert picture description here
The two-dimensional data of drawing surface graph can be rectangular coordinate system data , It can also be polar data , Here is Polar data Created diagram

def f(x, y):
    return np.sin(np.sqrt(x**2 + y**2))

r = np.linspace(0, 6, 20)
theta = np.linspace(-0.9*np.pi, 0.8*np.pi, 40)
r, theta = np.meshgrid(r, theta)

X = r * np.sin(theta)
Y = r * np.cos(theta)
Z = f(X, Y)

ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='viridis', edgecolor='none')

 Insert picture description here

Surface triangulation

Which of the above grid data that requires uniform sampling is too rigorous , It's not easy to achieve , If there is no uniform drawing of Cartesian or polar grids , You can use triangulation graphics

ax.scatter Draw the surface graph of 3D sampling

def f(x, y):
    return np.sin(np.sqrt(x**2 + y**2))

r = 6*np.random.random(1000)
theta = 2*np.pi*np.random.random(1000)

X = np.ravel(r * np.sin(theta))
Y = np.ravel(r * np.cos(theta))
Z = f(X, Y)

ax.scatter(X, Y, Z, c=Z, cmap='viridis', linewidth=0.5)

 Insert picture description here
There are still many places in this picture that need to be repaired , Can be ax.plot_trisurf Function to complete , It first finds a set of triangles with all points connected , Then use this triangle to create a surface

def f(x, y):
    return np.sin(np.sqrt(x**2 + y**2))

r = 6*np.random.random(1000)
theta = 2*np.pi*np.random.random(1000)

X = np.ravel(r * np.sin(theta))
Y = np.ravel(r * np.cos(theta))
Z = f(X, Y)

ax.plot_trisurf(X, Y, Z, cmap='viridis', edgecolor='none')

 Insert picture description here
You can draw Mobius takes

theta = np.linspace(0, 2*np.pi, 30)
w = np.linspace(-0.25, 0.25, 8)
w, theta = np.meshgrid(w, theta)

phi = 0.5*theta

# x-y Radius in plane 
r = 1+w*np.cos(phi)

x = np.ravel(r*np.cos(theta))
y = np.ravel(r*np.sin(theta))
z = np.ravel(w*np.sin(phi))

#  Define triangulation with basic parametric method 
from matplotlib.tri import Triangulation
tri = Triangulation(np.ravel(w), np.ravel(theta))

ax.plot_trisurf(x, y, z, triangles=tri.triangles, cmap='viridis', linewidths=0.2)

ax.set_xlim(-1,1)
ax.set_ylim(-1,1)
ax.set_zlim(-1,1)

 Insert picture description here

原网站

版权声明
本文为[En^_^ Joy]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071514164172.html