mag*_*gu_ 5 python 3d matplotlib
我想制作一个 3D 散点图,每边都有 2D 投影。类似于以下内容:
散点图已创建为:
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?
使用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)
如果您只需要投影(而不是直方图),一个更简单的方法是添加带有展平数据的散点图:
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更明确)