matplotlib.pyplot.3dscatter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
"""
demo06_3dscatter.py 3D图
"""
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d

n = 300
x = np.random.normal(0, 1, n)
y = np.random.normal(0, 1, n)
z = np.random.normal(0, 1, n)
# 绘制三维散点图
plt.figure('3D Scatter', facecolor='lightgray')
ax3d = plt.gca(projection='3d') # 创建三维坐标系
plt.title('3D Scatter', fontsize=20)
ax3d.set_xlabel('x', fontsize=14)
ax3d.set_ylabel('y', fontsize=14)
ax3d.set_zlabel('z', fontsize=14)
plt.xticks([-3, -2, -1, 0, 1, 2, 3])
plt.yticks([-3, -2, -1, 0, 1, 2, 3])
plt.tick_params(labelsize=9)
d = np.sqrt(x**2 + y**2 + z**2)
ax3d.scatter(x, y, z, marker='o', s=70, c=d,
alpha=.5, cmap='jet')
# make unused spines invisible to fix error of tight_layout
for spine in ax3d.spines.values():
spine.set_visible(False)
plt.tight_layout()
plt.savefig('3d-scatter.png')
plt.show()

3d scatter