欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

python matplotlib绘制三维图的示例

时间:2021-04-30 10:12:12|栏目:Python代码|点击:

作者: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()

上一篇:如何在python中实现随机选择

栏    目:Python代码

下一篇:python操作openpyxl导出Excel 设置单元格格式及合并处理代码实例

本文标题:python matplotlib绘制三维图的示例

本文地址:http://www.codeinn.net/misctech/111808.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有