比如我有x,y,z三个维度,怎么用matplotlib画一个三维的散点图?
2个回答
# -*- coding: utf-8 -*-
from matplotlib import pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
data = np.random.randint(0, 255, size=[40, 40, 40])
x, y, z = data[0], data[1], data[2]
ax = plt.subplot(111, projection='3d') # 创建一个三维的绘图工程
# 将数据点分成三部分画,在颜色上有区分度
ax.scatter(x[:10], y[:10], z[:10], c='y') # 绘制数据点
ax.scatter(x[10:20], y[10:20], z[10:20], c='r')
ax.scatter(x[30:40], y[30:40], z[30:40], c='g')
ax.set_zlabel('Z') # 坐标轴
ax.set_ylabel('Y')
ax.set_xlabel('X')
plt.show()
谢谢!
-
吴昊通
2018-05-11 13:38
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# 生成数据
data = np.random.randint(0,255,size=[40,40,40])
x,y,z= data[0],data[1],data[2]
# 创建三维对象
fig= plt.figure()
ax = Axes3D(fig)
# 绘制散点图
ax.scatter(x[:10],y[:10],z[:10],c='y')
ax.scatter(x[10:20],y[10:20],z[10:20],c='r')
ax.scatter(x[30:40],y[30:40],z[30:40],c='g')
# 显示图像
plt.show()