我想制作一个3D散点图,每边都有2D投影。如下所示:
散点图已使用:
创建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
完全完成此操作?
答案 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)
如果您只想要投影(但不是直方图),更简单的方法是添加带有展平数据的散点图:
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
更明确)