Python 三维散点图可视化随机数据
用 Python 生成三维散点图可视化随机数据
本文将使用 Python 生成 50 组随机数据,并利用 matplotlib 库绘制三维散点图进行可视化展示,直观地呈现数据的空间分布情况。
以下是使用 Python 生成的 50 组随机数据:
- (3, 8, 10)
- (9, 1, 6)
- (2, 5, 7)
- (8, 6, 1)
- (7, 9, 2)
- (5, 4, 7)
- (8, 5, 9)
- (2, 6, 4)
- (3, 10, 1)
- (4, 2, 9)
- (7, 3, 6)
- (1, 5, 4)
- (10, 2, 9)
- (4, 8, 3)
- (5, 7, 10)
- (6, 9, 2)
- (1, 8, 5)
- (2, 7, 6)
- (3, 9, 1)
- (10, 4, 8)
- (7, 1, 3)
- (8, 9, 6)
- (2, 10, 5)
- (4, 7, 3)
- (5, 1, 9)
- (6, 3, 10)
- (1, 4, 8)
- (2, 9, 7)
- (3, 6, 5)
- (10, 8, 1)
- (9, 1, 7)
- (6, 7, 3)
- (5, 8, 4)
- (1, 2, 6)
- (9, 3, 10)
- (7, 6, 8)
- (10, 5, 2)
- (8, 4, 1)
- (3, 2, 9)
- (4, 6, 7)
- (1, 10, 5)
- (2, 8, 3)
- (9, 7, 4)
- (6, 1, 2)
- (5, 3, 8)
- (10, 9, 1)
- (7, 4, 2)
- (8, 2, 5)
- (3, 1, 10)
- (4, 9, 6)
由于数据是三维的,我们可以使用三维散点图来表示。
下面是使用 matplotlib 库生成的三维散点图:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import random
# 生成随机数据
data = []
for i in range(50):
x = random.randint(1, 10)
y = random.randint(1, 10)
z = random.randint(1, 10)
data.append((x, y, z))
# 绘制散点图
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
for (x, y, z) in data:
ax.scatter(x, y, z, c='b', marker='o')
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.show()
运行代码后,会弹出一个窗口,显示生成的三维散点图,如下图所示:

原文地址: https://www.cveoy.top/t/topic/m5cC 著作权归作者所有。请勿转载和采集!