当前位置:网站首页>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
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')
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')
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)
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')
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')
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')
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)
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')
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)
边栏推荐
猜你喜欢
随机推荐
Initial experience of cache and ehcache "suggestions collection"
String - string (Lua)
2022.07.05
现在股票开户可以直接在网上开吗?安全吗。
POJ 2392 Space Elevator
解决远程rviz报错问题
The moveposition function of rigidbody2d of unity2d solves the problem of people or screen jitter when moving
Business experience in virtual digital human
杰理之关于 TWS 配对方式配置【篇】
【HDU】5248-序列变换(贪心+二分)「建议收藏」
"Decryption" Huawei machine vision Corps: Huawei is moving up and the industry is moving forward
5billion, another master fund was born in Fujian
Numpy——axis
2022.07.04
In the first half of 2022, I found 10 books that have been passed around by my circle of friends
基于图像和激光的多模态点云融合与视觉定位
博睿数据入选《2022爱分析 · IT运维厂商全景报告》
3. About cookies
App capture of charles+drony
Download from MySQL official website: mysql8 for Linux X Version (Graphic explanation)