声明:
使用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维点集:
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)