来自Matplotlib窗格边缘可见性的3D图形

时间:2017-11-16 16:32:03

标签: python matplotlib

我无法看到左窗格边缘。

%pylab inline
from mpl_toolkits.mplot3d import axes3d
rcdefaults()

x, y = meshgrid(linspace(-10, 10, 100), linspace(-10, 10, 100))
z = x**2+y**2

fig = figure(figsize=[16, 9])
ax3D = fig.add_subplot(111, projection='3d')
ax3D.plot_surface(x, y, z, rstride=8, cstride=8, alpha=0.3, cmap='hot')  
ax3D.contour(x, y, z, zdir='x', offset=ax3D.get_xlim()[0])
ax3D.contour(x, y, z, zdir='y', offset=ax3D.get_ylim()[1])
ax3D.contour(x, y, z, zdir='z', offset=ax3D.get_zlim()[0])
ax3D.xaxis.pane.set_edgecolor('#000000')
ax3D.yaxis.pane.set_edgecolor('#000000')
ax3D.zaxis.pane.set_edgecolor('#000000')
ax3D.xaxis.pane.set_alpha(1)
ax3D.yaxis.pane.set_alpha(1)
ax3D.zaxis.pane.set_alpha(1)
ax3D.xaxis.pane.fill = False
ax3D.yaxis.pane.fill = False
ax3D.zaxis.pane.fill = False
fig.savefig('test')

enter image description here

一个令人不满意的解决方法是通过反转x和y轴在这个特定的窗格边上放置一个带标签的斧头:

ax3D.invert_yaxis()
ax3D.invert_xaxis()

enter image description here

使用Poly3DCollection线补丁的更好的解决方法几乎可以做到。除了检索到的限制似乎不太合适。

from mpl_toolkits.mplot3d import art3d

i = array([ax3D.get_xlim3d()[0], ax3D.get_ylim3d()[0], ax3D.get_zlim3d()[0]])
f = array([ax3D.get_xlim3d()[0], ax3D.get_ylim3d()[0], ax3D.get_zlim3d()[1]])
p = art3d.Poly3DCollection(array([[i, f]]))
p.set_color('black')
ax3D.add_collection3d(p)

enter image description here

1 个答案:

答案 0 :(得分:0)

所以这是我有点hacky的答案或更好的解决这个问题的解决方法:

%pylab inline
%matplotlib notebook
%matplotlib notebook
from mpl_toolkits.mplot3d import axes3d, art3d
rcdefaults()

x, y = meshgrid(linspace(-10, 10, 100), linspace(-10, 10, 100))
z = x**2+y**2

fig = figure()
ax3D = fig.add_subplot(111, projection='3d')
ax3D.plot_surface(x, y, z, rstride=8, cstride=8, alpha=0.3, cmap='hot')

def lims(mplotlims):
    scale = 1.021
    offset = (mplotlims[1] - mplotlims[0])*scale
    return mplotlims[1] - offset, mplotlims[0] + offset
xlims, ylims, zlims = lims(ax3D.get_xlim()), lims(ax3D.get_ylim()), lims(ax3D.get_zlim())
ax3D.contour(x, y, z, zdir='x', offset=xlims[0])
ax3D.contour(x, y, z, zdir='y', offset=ylims[1])
ax3D.contour(x, y, z, zdir='z', offset=zlims[0])
i = array([xlims[0], ylims[0], zlims[0]])
f = array([xlims[0], ylims[0], zlims[1]])
p = art3d.Poly3DCollection(array([[i, f]]))
p.set_color('black')
ax3D.add_collection3d(p)

ax3D.xaxis.pane.set_edgecolor('#000000')
ax3D.yaxis.pane.set_edgecolor('#000000')
ax3D.zaxis.pane.set_edgecolor('#000000')
ax3D.xaxis.pane.set_alpha(1)
ax3D.yaxis.pane.set_alpha(1)
ax3D.zaxis.pane.set_alpha(1)
ax3D.xaxis.pane.fill = False
ax3D.yaxis.pane.fill = False
ax3D.zaxis.pane.fill = False
fig.tight_layout()
show()

enter image description here