三维散点图,侧面有二维直方图

时间:2016-07-06 17:25:55

标签: python matplotlib 3d

我想制作一个3D散点图,每边都有2D投影。如下所示:

3D scatter

散点图已使用:

创建
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x, y, z, c='r', marker='.')

预测:

 h = np.np.histogram2d(x, y)
 plt.imshow(h, cmap='cubehelix_r', interpolation='none')

他们和inkscape一起被带到了一起。如何使用matplotlib完全完成此操作?

1 个答案:

答案 0 :(得分:2)

使用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)

重复其他2个直方图。 Plot with histograms

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

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)

(通常我只需输入x*0 + n即可使匹配的数组填充常量值,但np.full_like更明确)

Plot with projections

相关问题