python画图|3D surface基础教程

画三维图在当前的需求越来越强烈,因此掌握3D图的画法至关重要。

让我们先来学习3D surface基础教程。

【1】官网教程

首先是来到官网,找到教程,详见下述链接:

3D surface (colormap) --- Matplotlib 3.9.2 documentation

教程页面给出了漂亮的3D surface图形,具体代码解释如下。

【2】代码解读

首先是引入numpyh和matplotlib:

复制代码
import matplotlib.pyplot as plt #引入matplotlib模块画图
import numpy as np #引入numpy模块做数学计算

from matplotlib import cm #cm即是colormap,引入该模块支持渐变颜色绘制
from matplotlib.ticker import LinearLocator #引入线性化画图模块

然后定义了变量:

复制代码
fig, ax = plt.subplots(subplot_kw={"projection": "3d"}) #定义画图

# Make data.
X = np.arange(-5, 5, 0.25) #定义X范围
Y = np.arange(-5, 5, 0.25) #定义Y范围
X, Y = np.meshgrid(X, Y) #输出组合组矩阵
R = np.sqrt(X**2 + Y**2) #定义R变量
Z = np.sin(R) #定义Z变量

之后定义了要画surface图:

复制代码
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
                       linewidth=0, antialiased=False) #定义画surface图

剩下的部分先定义了坐标轴:

复制代码
ax.set_zlim(-1.01, 1.01) #限定Z轴
ax.zaxis.set_major_locator(LinearLocator(10)) #限定线性化位置
# A StrMethodFormatter is used automatically
ax.zaxis.set_major_formatter('{x:.02f}') #设置坐标格式

最后定义了图形颜色和要求输出图形:

复制代码
fig.colorbar(surf, shrink=0.5, aspect=5) #定义图形渐变

plt.show()

++图1++

【2】代码修改

尝试修改LinearLocator(30)为30,改后:

复制代码
ax.zaxis.set_major_locator(LinearLocator(30)) #限定线性化位置
复制代码
输出结果为:

++图2++

图形层数变得密集。

【3】总结

学习了3Dsurface的基础画法。

相关推荐
呱呱复呱呱21 小时前
Django CBV 源码解读:一个请求是怎么找到你的 get() 方法的
python·django
曲幽1 天前
刚部署的 LibreTranslate 频频翻车?我掏出了 20 年前的 StarDict 词典,用 FastAPI 搭了个本地词典翻译 API
python·fastapi·web·translate·goldendict·libretranslate·stardict·pystardict
荣码1 天前
用Streamlit给AI应用套个界面,10行代码出Web页面
java·python
兵慌码乱1 天前
基于Python+PyQt5+SQLite的药房管理系统实现:事务一致性与界面解耦全流程解析
python·sqlite·信号与槽·pyqt5·数据库设计·桌面应用开发·事务处理
金銀銅鐵2 天前
[Python] 体验用欧几里得算法计算最大公约数的过程
python·数学
FreakStudio2 天前
W55MH32L-EVB 上手测评:硬件 TCP/IP 加持的以太网单片机,MicroPython 零门槛开发
python·单片机·嵌入式·大学生·面向对象·并行计算·电子diy·电子计算机
用户0332126663672 天前
使用 Python 从零创建 Word 文档
python
Csvn2 天前
Python 两大经典坑点 —— 可变默认参数 & 闭包延迟绑定
后端·python
曲幽2 天前
别再用网页翻译看源码了!你的私人翻译神器LibreTranslate,部署避坑指南来了
python·docker·web·pot·translate·libretranslate·arogstranslate
用户556918817532 天前
#从脚本到独立程序:Python + Playwright 批量抓取的完整踩坑记录
python·自动化运维