python matplotlib绘制三维图的示例
作者:catmelo 时间:2023-03-04 16:35:48
作者:catmelo 本文版权归作者所有
链接:https://www.cnblogs.com/catmelo/p/4162101.html
本文参考官方文档:http://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html
起步
新建一个matplotlib.figure.Figure对象,然后向其添加一个Axes3D类型的axes对象。
其中Axes3D对象的创建,类似其他axes对象,只不过使用projection='3d'
关键词。
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
3D曲线图
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
mpl.rcParams['legend.fontsize'] = 10
fig = plt.figure()
ax = fig.gca(projection='3d')
theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)
z = np.linspace(-2, 2, 100)
r = z**2 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)
ax.plot(x, y, z, label='parametric curve')
ax.legend()
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.show()
简化用法:
from pylab import *
from mpl_toolkits.mplot3d import Axes3D
plt.gca(projection='3d')
plt.plot([1,2,3],[3,4,1],[8,4,1],'--')
plt.xlabel('X')
plt.ylabel('Y')
#plt.zlabel('Z') #无法使用
3D散点图
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
def randrange(n, vmin, vmax):
return (vmax-vmin)*np.random.rand(n) + vmin
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
n = 100
for c, m, zl, zh in [('r', 'o', -50, -25), ('b', '^', -30, -5)]:
xs = randrange(n, 23, 32)
ys = randrange(n, 0, 100)
zs = randrange(n, zl, zh)
ax.scatter(xs, ys, zs, c=c, marker=m)
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.show()
来源:https://www.cnblogs.com/catmelo/p/4162101.html
标签:matplotlib,绘制,三维图,python
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
asp如何实现按照输入汉字提示拼音功能?
2010-05-18 18:37:00
Python使用回溯法子集树模板解决爬楼梯问题示例
2022-05-30 17:38:54
![](https://img.aspxhome.com/file/2023/6/104886_0s.jpg)
vue中简单弹框dialog的实现方法
2024-05-21 10:14:57
![](https://img.aspxhome.com/file/2023/8/125298_0s.jpg)
什么是gRPC
2023-01-16 15:23:53
![](https://img.aspxhome.com/file/2023/0/120550_0s.jpg)
python爬虫教程之bs4解析和xpath解析详解
2023-09-22 19:43:06
Python Flask-Login模块使用案例详解
2021-10-14 21:01:17
![](https://img.aspxhome.com/file/2023/0/90590_0s.png)
python 绘制场景热力图的示例
2022-01-22 07:03:21
![](https://img.aspxhome.com/file/2023/0/80560_0s.jpg)
Python input函数使用实例解析
2022-01-05 21:00:34
JavaScript实现年历效果
2023-09-10 10:53:26
![](https://img.aspxhome.com/file/2023/0/56280_0s.jpg)
PyCharm最新激活码PyCharm2020.2.3有效
2023-05-14 21:18:55
![](https://img.aspxhome.com/file/2023/2/93812_0s.png)
ASP生成html的新方法
2011-04-02 11:04:00
W3C 发布 XMLHttpRequest 工作草图
2008-04-28 13:00:00
Python+OCR实现文档解析的示例代码
2023-11-22 02:34:19
![](https://img.aspxhome.com/file/2023/0/82790_0s.jpg)
javascript中使用replaceAll()函数实现字符替换的方法
2024-04-10 16:18:47
JS实现控制表格内指定单元格内容对齐的方法
2024-04-17 09:42:28
Python类的动态修改的实例方法
2021-07-03 12:16:15
pandas把dataframe转成Series,改变列中值的类型方法
2023-07-04 11:41:58
Python 中 Shutil 模块详情
2022-12-20 15:25:58
![](https://img.aspxhome.com/file/2023/8/128618_0s.jpg)
vue实现Input输入框模糊查询方法
2024-04-28 09:20:46
![](https://img.aspxhome.com/file/2023/2/133112_0s.png)
Laravel实现登录跳转功能
2023-06-14 23:35:40
![](https://img.aspxhome.com/file/2023/9/55549_0s.png)