当前位置:网站首页>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)
边栏推荐
- 从39个kaggle竞赛中总结出来的图像分割的Tips和Tricks
- 高温火烧浑不怕,钟薛高想留清白在人间
- How many are there (Lua)
- 多个kubernetes集群如何实现共享同一个存储
- 99% of people don't know that privatized deployment is also a permanently free instant messaging software!
- 炒股如何开户?请问一下手机开户股票开户安全吗?
- The top of slashdata developer tool is up to you!!!
- Solve the error reporting problem of rosdep
- L1-023 output gplt (Lua)
- 杰理之按键发起配对【篇】
猜你喜欢
5billion, another master fund was born in Fujian
ES6笔记一
Wechat web debugging 8.0.19 replace the X5 kernel with xweb, so the X5 debugging method can no longer be used. Now there is a solution
杰理之关于 TWS 声道配置【篇】
高温火烧浑不怕,钟薛高想留清白在人间
虚拟数字人里的生意经
Mathematical analysis_ Notes_ Chapter 11: Fourier series
App capture of charles+postern
一张图深入的理解FP/FN/Precision/Recall
杰理之相同声道的耳机不允许配对【篇】
随机推荐
LeetCode1051(C#)
Micro service remote debug, nocalhost + rainbow micro service development second bullet
【Base64笔记】「建议收藏」
Netease Yunxin participated in the preparation of the standard "real time audio and video service (RTC) basic capability requirements and evaluation methods" issued by the Chinese Academy of Communica
10 schemes to ensure interface data security
Desci: is decentralized science the new trend of Web3.0?
AI writes a poem
How many times is PTA 1101 B than a
How to estimate the value of "not selling pens" Chenguang?
Numpy——axis
6. About JWT
IP netns command (memo)
Solve the error reporting problem of rosdep
what‘s the meaning of inference
Redis cluster and expansion
POJ 2392 Space Elevator
99% of people don't know that privatized deployment is also a permanently free instant messaging software!
Learn open62541 -- [67] add custom enum and display name
In the first half of 2022, I found 10 books that have been passed around by my circle of friends
RISCV64