侧面带有 2D 直方图的 3D 散点图

mag*_*gu_ 5 python 3d matplotlib

我想制作一个 3D 散点图,每边都有 2D 投影。类似于以下内容:

3D 散射

散点图已创建为:

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x, y, z, c='r', marker='.')
Run Code Online (Sandbox Code Playgroud)

和预测:

 h = np.np.histogram2d(x, y)
 plt.imshow(h, cmap='cubehelix_r', interpolation='none')
Run Code Online (Sandbox Code Playgroud)

并且它们已经与inkscape结合在一起。我将如何完全做到这一点matplotlib

Lac*_*ack 5

使用plot_surface()

ax.scatter(x,y,z, marker='.', c='r')

h, yedges, zedges = np.histogram2d(y, z, bins=50)
h = h.transpose()
normalized_map = plt.cm.Blues(h/h.max())

yy, zz = np.meshgrid(yedges, zedges)
xpos = min(x)-2 # Plane of histogram
xflat = np.full_like(yy, xpos) 

p = ax.plot_surface(xflat, yy, zz, facecolors=normalized_map, rstride=1, cstride=1, shade=False)
Run Code Online (Sandbox Code Playgroud)

对其他 2 个直方图重复此操作。 用直方图绘制

如果您只需要投影(而不是直方图),一个更简单的方法是添加带有展平数据的散点图:

ax.scatter(x, y, z, c='r', marker='.')

xflat = np.full_like(x, min(ax.get_xlim()))
yflat = np.full_like(y, max(ax.get_ylim()))
zflat = np.full_like(z, min(ax.get_zlim()))

ax.scatter(xflat, y, z)
ax.scatter(x, yflat, z)
ax.scatter(x, y, zflat)
Run Code Online (Sandbox Code Playgroud)

(通常我只是输入x*0 + n一个匹配的数组来填充常量值,但np.full_like更明确)

带投影的绘图