我目前正在尝试使用 3D 数组绘制 3D 散点图。
我在网上找到的关于绘制 3D 散点图的内容看起来像
ax.scatter3D(x, y, z)
其中 x
、y
、z
都是一维数组。
但是,就我而言,我正在使用 numpy 的 (3, 3, 3)
生成一组形状histogramdd
。
In [61]: h, edges = histogramdd(array([[1,2,4],[4,2,8],[3,2,1],[2,1,2],[2,1,3],[2,1,1],[2,1,4]]),bins=3)
In [64]: h
Out[64]:
array([[[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 1., 0.]],
[[ 3., 1., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.]],
[[ 0., 0., 0.],
[ 0., 0., 0.],
[ 1., 0., 1.]]])
我的问题是,如何将这个 (3, 3, 3)
解包成与轴对应的一维数组,以便我可以绘制 3d 散点图?
答案 0 :(得分:1)
我想说您需要 4 个维度来绘制您创建的直方图。一个想法可能是使用 3D 散点图改变标记的大小,以对每个 3D 箱中包含的数据点数量的信息进行编码。
这就是我要做的。
import numpy as np
a = np.array([[1,2,4],[4,2,8],[3,2,1],[2,1,2],[2,1,3],[2,1,1],[2,1,4]])
h, edges = np.histogramdd(a, bins=3)
ex, ey, ez = edges
x, y, z = np.meshgrid(np.linspace(ex[0], ex[-1], 3),
np.linspace(ey[0], ey[-1], 3),
np.linspace(ez[0], ez[-1], 3))
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x.flatten(), y.flatten(), z.flatten(), s=h.flatten()*50)
这是情节结果: