当前位置:网站首页>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)
边栏推荐
- 如何在博客中添加Aplayer音乐播放器
- Ray and OBB intersection detection
- Seaborn数据可视化
- Reflections on "product managers must read: five classic innovative thinking models"
- The server is completely broken and cannot be repaired. How to use backup to restore it into a virtual machine without damage?
- Read PG in data warehouse in one article_ stat
- mysql使用笔记一
- Flask搭建api服务
- 邮件服务器被列入黑名单,如何快速解封?
- LeetCode-SQL第一天
猜你喜欢
The top of slashdata developer tool is up to you!!!
正在准备面试,分享面经
Master this set of refined Android advanced interview questions analysis, oppoandroid interview questions
skimage学习(1)
Skimage learning (1)
如何选择合适的自动化测试工具?
The latest interview experience of Android manufacturers in 2022, Android view+handler+binder
Direct dry goods, 100% praise
Lowcode: four ways to help transportation companies enhance supply chain management
MRS离线数据分析:通过Flink作业处理OBS数据
随机推荐
Module VI
科普达人丨一文弄懂什么是云计算?
[Seaborn] combination chart: facetgrid, jointgrid, pairgrid
Shallow understanding Net core routing
Pycharm IDE下载
NeRF:DeepFake的最终替代者?
LeetCode 120. Triangle minimum path and daily question
How to add aplayer music player in blog
Flask搭建api服务-SQL配置文件
Flask build API service SQL configuration file
Rpcms method of obtaining articles under the specified classification
Smart logistics platform: make overseas warehouses smarter
LeetCode 1696. Jumping game VI daily question
LeetCode 1049. Weight of the last stone II daily question
LeetCode 1981. 最小化目标值与所选元素的差 每日一题
LocalStorage和SessionStorage
《产品经理必读:五种经典的创新思维模型》的读后感
LeetCode刷题day49
Mrs offline data analysis: process OBS data through Flink job
正在准备面试,分享面经