声明:
- 本文参考自matplotlib官方教程
- 介绍了3D曲线图和散点图的绘制,以后可能会添加更复杂图形的绘制方法。
开始
使用matplotlib绘制3D图形,需要通过创建一个新的axes对象——Axes3D来实现。
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
plt.show()
绘制曲线
通过参数projection='3d
来声明axes为axes3D,此时对axes3D对象调用plot()
函数绘制曲线图时,多了如下表所示的几个参数,其他的参数还跟原来一样。
Argument | Description |
---|---|
xs, ys | x,y轴上的值 |
zs | z轴上的值 |
zdir | z轴的方向,可取{‘x’, ‘y’, ‘z’} |
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.gca(projection='3d')
# Prepare arrays x, y, z
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') #这里传入x, y, z的值
ax.legend()
plt.show()
绘制散点图
相较于绘制曲线图,绘制直线图增加了三个参数:控制散点大小的s
、空值散点颜色的c
和空值散点阴影效果的depthshade
。
Argument | Description |
---|---|
xs, ys | 数据点的x,y坐标 |
zs | 数据点的z坐标,默认为0 |
zdir | z轴的方向 |
s | 散点的大小 |
c | 散点的颜色 |
depthshade | 内层的散点颜色会浅一点,默认为True |
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
# Fixing random state for reproducibility
np.random.seed(19680801)
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].
# 迭代两次,分别绘制两种不同的散点,一种是(红色,小圆圈,z坐标取值范围(-50, -25)),另一种是(蓝色,小三角,z坐标取值范围(-30, -5))。
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()
改变图像角度
首先定义3维点集:
将点集在3d空间描绘出来:
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection='3d')
for i in range(x.shape[0]):
ax.text(x[i, 0], x[i, 1], x[i, 2], str(1),
color='r',
fontdict={'weight': 'bold', 'size': 9})
plt.show()
改变一下图像的角度:
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection='3d')
for i in range(x.shape[0]):
ax.text(x[i, 0], x[i, 1], x[i, 2], str(1),
color='r',
fontdict={'weight': 'bold', 'size': 9})
ax.view_init(20, 0) # 只有这一行改变了
plt.show()
-
解释:
-
关键就是函数
ax.view_init()
函数的调用 - 该函数接受两个参数,第一个参数是竖直旋转,第二个参数是水平旋转,旋转单位是度°
-
默认的初始角度是
ax.view_init(30, -60)