python 3d绘图模块_Python绘制3D图形
3D图形在数据分析、数据建模、图形和图像处理等领域中都有着广泛的应用,下面将给大家介绍一下如何使用python进行3D图形的绘制,包括3D散点、3D表面、3D轮廓、3D直线(曲线)以及3D文字等的绘制。
准备工作:
python中绘制3D图形,依旧使用常用的绘图模块matplotlib,但需要安装mpl_toolkits工具包,安装方法如下:windows命令行进入到python安装目录下的Scripts文件夹下,执行: pip install --upgrade matplotlib即可;linux环境下直接执行该命令。
安装好这个模块后,即可调用mpl_tookits下的mplot3d类进行3D图形的绘制。
下面以实例进行说明。
1、3D表面形状的绘制
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Make data
u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, np.pi, 100)
x = 10 * np.outer(np.cos(u), np.sin(v))
y = 10 * np.outer(np.sin(u), np.sin(v))
z = 10 * np.outer(np.ones(np.size(u)), np.cos(v))
# Plot the surface
ax.plot_surface(x, y, z, color='b')
plt.show()
球表面,结果如下:
2、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()
plt.show()
这段代码用于绘制一个螺旋状3D曲线,结果如下:
3、绘制3D轮廓
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
from matplotlib import cm
fig = plt.figure()
ax = fig.gca(projection='3d')
X, Y, Z = axes3d.get_test_data(0.05)
cset = ax.contour(X, Y, Z, zdir='z', offset=-100, cmap=cm.coolwarm)
cset = ax.contour(X, Y, Z, zdir='x', offset=-40, cmap=cm.coolwarm)
cset = ax.contour(X, Y, Z, zdir='y', offset=40, cmap=cm.coolwarm)
ax.set_xlabel('X')
ax.set_xlim(-40, 40)
ax.set_ylabel('Y')
ax.set_ylim(-40, 40)
ax.set_zlabel('Z')
ax.set_zlim(-100, 100)
plt.show()
绘制结果如下:
4、绘制3D直方图
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x, y = np.random.rand(2, 100) * 4
hist, xedges, yedges = np.histogram2d(x, y, bins=4, range=[[0, 4], [0, 4]])
# Construct arrays for the anchor positions of the 16 bars.
# Note: np.meshgrid gives arrays in (ny, nx) so we use 'F' to flatten xpos,
# ypos in column-major order. For numpy >= 1.7, we could instead call meshgrid
# with indexing='ij'.
xpos, ypos = np.meshgrid(xedges[:-1] + 0.25, yedges[:-1] + 0.25)
xpos = xpos.flatten('F')
ypos = ypos.flatten('F')
zpos = np.zeros_like(xpos)
# Construct arrays with the dimensions for the 16 bars.
dx = 0.5 * np.ones_like(zpos)
dy = dx.copy()
dz = hist.flatten()
ax.bar3d(xpos, ypos, zpos, dx, dy, dz, color='b', zsort='average')
plt.show()
绘制结果如下:
5、绘制3D网状线
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Grab some test data.
X, Y, Z = axes3d.get_test_data(0.05)
# Plot a basic wireframe.
ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10)
plt.show()
绘制结果如下:
6、绘制3D三角面片图
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
n_radii = 8
n_angles = 36
# Make radii and angles spaces (radius r=0 omitted to eliminate duplication).
radii = np.linspace(0.125, 1.0, n_radii)
angles = np.linspace(0, 2*np.pi, n_angles, endpoint=False)
# Repeat all angles for each radius.
angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)
# Convert polar (radii, angles) coords to cartesian (x, y) coords.
# (0, 0) is manually added at this stage, so there will be no duplicate
# points in the (x, y) plane.
x = np.append(0, (radii*np.cos(angles)).flatten())
y = np.append(0, (radii*np.sin(angles)).flatten())
# Compute z to make the pringle surface.
z = np.sin(-x*y)
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_trisurf(x, y, z, linewidth=0.2, antialiased=True)
plt.show(
绘制结果如下:
7、绘制3D散点图
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
def randrange(n, vmin, vmax):
'''''
Helper function to make an array of random numbers having shape (n, )
with each number distributed Uniform(vmin, vmax).
'''
return (vmax - vmin)*np.random.rand(n) + vmin
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
n = 100
# For each set of style and range settings, plot n random points in the box
# defined by x in [23, 32], y in [0, 100], z in [zlow, zhigh].
for c, m, zlow, zhigh in [('r', 'o', -50, -25), ('b', '^', -30, -5)]:
xs = randrange(n, 23, 32)
ys = randrange(n, 0, 100)
zs = randrange(n, zlow, zhigh)
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()
绘制结果如下:
8、绘制3D文字
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.gca(projection='3d')
# Demo 1: zdir
zdirs = (None, 'x', 'y', 'z', (1, 1, 0), (1, 1, 1))
xs = (1, 4, 4, 9, 4, 1)
ys = (2, 5, 8, 10, 1, 2)
zs = (10, 3, 8, 9, 1, 8)
for zdir, x, y, z in zip(zdirs, xs, ys, zs):
label = '(%d, %d, %d), dir=%s' % (x, y, z, zdir)
ax.text(x, y, z, label, zdir)
# Demo 2: color
ax.text(9, 0, 0, "red", color='red')
# Demo 3: text2D
# Placement 0, 0 would be the bottom left, 1, 1 would be the top right.
ax.text2D(0.05, 0.95, "2D Text", transform=ax.transAxes)
# Tweaking display region and labels
ax.set_xlim(0, 10)
ax.set_ylim(0, 10)
ax.set_zlim(0, 10)
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')
plt.show(
绘制结果如下:
9、3D条状图
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
for c, z in zip(['r', 'g', 'b', 'y'], [30, 20, 10, 0]):
xs = np.arange(20)
ys = np.random.rand(20)
# You can provide either a single color or an array. To demonstrate this,
# the first bar of each set will be colored cyan.
cs = [c] * len(xs)
cs[0] = 'c'
ax.bar(xs, ys, zs=z, zdir='y', color=cs, alpha=0.8)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()
绘制结果如下:
以上所述是小编给大家介绍的python绘制3D图形,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持
本文标题: Python绘制3D图形
本文地址: http://www.cppcns.com/jiaoben/python/226927.html
python 3d绘图模块_Python绘制3D图形相关推荐
- python 3d绘图立方体_python绘制3D立方体
我想绘制一个平行六面体.其实我从python脚本开始画立方体为:python绘制3D立方体 import numpy as np from mpl_toolkits.mplot3d import Ax ...
- python绘制3d动态模型_Python绘制3D图形
3D图形在数据分析.数据建模.图形和图像处理等领域中都有着广泛的应用,下面将给大家介绍一下如何使用python进行3D图形的绘制,包括3D散点.3D表面.3D轮廓.3D直线(曲线)以及3D文字等的绘制 ...
- python 3d绘图 范围_python – 在3D绘图中绘制所有三个轴上的分布轮廓
我在三维空间中有一堆点,并估计了这些点上的一些分布(也在3D空间中;使用 kernel density estimation虽然这与这个问题无关).我想将该分布的投影绘制为所有三个轴(x,y和z)上的 ...
- 3D绘图 WebGl引擎----ThreeJS 3D渲染引擎
3D绘图 WebGl引擎---->ThreeJS 3D渲染引擎 是将OpenGl ES2.0d原来C语言的API和配置项迁移到了JavaScript中 壹.基本图形语法 1.ThreeJs将原本 ...
- python 3d绘图平面_python 用 matplotlib 在 3D 空间中绘制平面 实例详解
#创建画布 fig = plt.figure(figsize=(12, 8), facecolor='lightyellow')#创建 3D 坐标系 ax = fig.gca(fc='whitesmo ...
- python绘制四边螺旋线代_Python绘制3d螺旋曲线图实例代码
Line plots Axes3D.plot(xs, ys, *args, **kwargs) 绘制2D或3D数据 参数 描述 xs, ys X轴,Y轴坐标定点 zs Z值,每一个点的值都是1 zdi ...
- matlab三维绘图poly,matplotlib绘制三维图形mplot3d(包含Mayavi.mlab模块)
http://blog.csdn.net/pipisorry/article/details/40008005 Matplotlib mplot3d 工具包简介 The mplot3d toolkit ...
- Python数据分析10——使用Matplotlib绘制3D图
目录 3D立体图形 3D绘图 3D散点图 3D曲线图 3D平面图 3D立体图形 绘制三维图像主要通过 mplot3d 模块实现. from matplotlib import pyplot as pl ...
- python有多少个模块_python绘图模块有哪些
匿名用户 1级 2018-03-22 回答 urtle库是Python语言中一个很流行的绘制图像的函数库,想象一个小乌龟,在一个横轴为x.纵轴为y的坐标系原点,(0,0)位置开始,它根据一组函数指令的 ...
最新文章
- 硬件工程师必备秘籍,模拟电子经典200问!
- mysql的别名可以动态么_mysql别名的使用
- 脏读、不可重复读 共享锁、悲观锁 和 事务五种隔离级别
- 20155335 俞昆 第十周作业
- asp.net中的窗体身份验证
- 谈谈你的GC调优思路?
- Seata 单机环境搭建_01
- c语言实现各种排序,c语言实现各种排序算法
- 百度网盘vep文件如何转换mp4_用这个软件,聊聊如何将MOV文件转换为MP4
- 山大网络教育线上作业计算机,山大网络教育《计算机基础》模拟参考答案.doc...
- 【文献阅读】Stacked What-Where Auto-encoders -ICLR-2016
- 使用npm-check-updates模块升级插件
- [PHP]如何使用Mobile_Detect来判断访问网站的设备:安卓,平板,电脑
- javascript之函数的定义传参
- linux的lilo,深入Linux的LILO
- 黑客利用2012伦敦奥运诈骗个人资料
- sdiv和srem问题解决
- html 自动 生成 日期,自己生成Select列表日期时间
- CodeWarrior V5.1破解license+教程
- 计算机音乐制作专业美国研究生,美国音乐制作专业研究生六大首选音乐学院
热门文章
- 用计算机画对称图形,人教小学美术五下《第17课电脑美术 对称图形》word教案...
- 1334177-87-5,Cbz-N-amido-PEG8-acid含有Cbz保护的氨基和末端羧酸的PEG连接物
- 解决:The ‘Access-Control-Allow-Origin‘ header contains___Nginx跨域设置
- LSD(Line Segment Detector)直线提取算法
- Hive学习使用一周感悟
- H5 iframe标签的用法
- 前端第二章:1.HTML简介、Linux 命令行打开 .html 文件、常用标签(一)
- 20191027(32)RT-Thread SPI 设备挂载——ADS1256 后续提供具体实现源码(stm32f407)
- [汇编] 汇编语言实现简易文本编辑器(光标移动、上卷和退格删除)
- Causal Reasoning from Meta-reinforcement Learning(自用笔记)