当前位置:网站首页>Matplotlib绘制三维图形
Matplotlib绘制三维图形
2022-07-07 15:38:00 【En^_^Joy】
用Matplotlib画三维图
画三维图需要mplot3d模块,在创建任意一个普通坐标轴的过程中加入projection='3d'关键字,从而创建三维坐标轴
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
import numpy as np
fig = plt.figure()
ax = plt.axes(projection='3d')
# 画图代码
# 显示图片
plt.show()
后序代码将这些代码省略了,将后面的代码套入画图代码里面即可
三维数据点与线
用ax.plot3D与ax.scatter3D来创建三维坐标的线图与散点图,三维函数的参数与二维函数的参数基本相同
# 三维线的数据
zline = np.linspace(0, 15, 1000)
xline = np.sin(zline)
yline = np.cos(zline)
ax.plot3D(xline, yline, zline, 'gray')
# 三维散点的数据
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')

默认情况,散点会自动改变透明度,以在平面上呈现出立体感
三维等高线图
ax.contour3D要求所有数据都是二维网格数据的格式,由函数计算z轴数值
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可以调整观察角度和方位角,在这个例子中,将俯仰角调整为60度,方位角调整为35度,在上面的代码基础上加上下面这行代码:
ax.view_init(60, 35)

也可以在图中点击拖拽图形装换角度
线框图和曲面图
ax.plot_wireframe绘制线框图
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绘制曲面图
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')

画曲面图的二维数据可以是直角坐标系数据,也可以是极坐标数据,下面是极坐标数据创建的图
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')

曲面三角剖分
上面哪些要求均匀采样的网格数据显得太过严谨,不容易实现,如果没有笛卡尔或极坐标网格的均匀绘图,可以使用三角剖分图形
ax.scatter绘制三维采样的曲面图
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)

这图还有很多地方需要修补,可以由ax.plot_trisurf函数来完成,它首先找到一组所有点都连接起来的三角形,然后用这三角形创建曲面
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')

可以用该方法画莫比乌斯带
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平面内的半径
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))
# 用基本参数化方法定义三角剖分
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)

边栏推荐
- NeRF:DeepFake的最终替代者?
- Reflections on "product managers must read: five classic innovative thinking models"
- Flask搭建api服务-生成API文档
- LeetCode 120. 三角形最小路径和 每日一题
- 【图像传感器】相关双采样CDS
- LeetCode 1981. Minimize the difference between the target value and the selected element one question per day
- LeetCode 1186. Delete once to get the sub array maximum and daily question
- skimage学习(1)
- Test case management tool recommendation
- Rpcms method of obtaining articles under the specified classification
猜你喜欢

自定义View必备知识,Android研发岗必问30+道高级面试题

skimage学习(3)——Gamma 和 log对比度调整、直方图均衡、为灰度图像着色

NeRF:DeepFake的最终替代者?

Talk about the realization of authority control and transaction record function of SAP system

skimage学习(3)——使灰度滤镜适应 RGB 图像、免疫组化染色分离颜色、过滤区域最大值

谈谈 SAP 系统的权限管控和事务记录功能的实现

How to add aplayer music player in blog

Nerf: the ultimate replacement for deepfake?

【视频/音频数据处理】上海道宁为您带来Elecard下载、试用、教程

SlashData开发者工具榜首等你而定!!!
随机推荐
A tour of gRPC:03 - proto序列化/反序列化
LeetCode 1981. Minimize the difference between the target value and the selected element one question per day
Master this set of refined Android advanced interview questions analysis, oppoandroid interview questions
LeetCode 213. Home raiding II daily question
最新2022年Android大厂面试经验,安卓View+Handler+Binder
【饭谈】如何设计好一款测试平台?
LeetCode 1986. The minimum working time to complete the task is one question per day
LeetCode 120. Triangle minimum path and daily question
LeetCode 120. 三角形最小路径和 每日一题
QT 图片背景色像素处理法
LeetCode 1774. 最接近目标价格的甜点成本 每日一题
LeetCode 1031. Maximum sum of two non overlapping subarrays
The server is completely broken and cannot be repaired. How to use backup to restore it into a virtual machine without damage?
智慧物流平台:让海外仓更聪明
LeetCode 1654. The minimum number of jumps to get home one question per day
[Seaborn] implementation of combined charts and multi subgraphs
浅谈 Apache Doris FE 处理查询 SQL 源码解析
First in China! Todesk integrates RTC technology into remote desktop, with clearer image quality and smoother operation
How to add aplayer music player in blog
Sator launched Web3 game "satorspace" and launched hoobi