Matplotlib三维绘图:如何去除过多的空白?

2024-04-25 19:29:31 发布

您现在位置:Python中文网/ 问答频道 /正文

如果我在Matplotlib中进行三维打印:

from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.gca(projection='3d')

x_labels = [10,20,30]
x = [1,2,3,4]
y = [3,1,5,1]
legend = False

for label in x_labels:
    x_3d = label*np.ones_like(x)
    ax.plot(x_3d, x, y, color='black', label='GMM')
    if legend == False:
        ax.legend()
        legend = True

ax.set_zlabel('test')

它将产生:

enter image description here

左侧有过多的空白。我想知道能不能把它处理掉?在


Tags: fromimportfalselabelsmatplotlibfigpltax
1条回答
网友
1楼 · 发布于 2024-04-25 19:29:31

可能已经太迟了,但我也遇到了类似的问题,这里是我删除空白的方法:使用fig.subplot_adjust()将左/右放置在正常区域之外。在您的例子中,我发现fig.subplot_adjust(left=-0.11)给出了一个合理的结果。在

完整代码如下:

from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()  
ax = fig.gca(projection='3d')

x_labels = [10,20,30]
x = [1,2,3,4]
y = [3,1,5,1]
legend = False

for label in x_labels:
    x_3d = label*np.ones_like(x)
    ax.plot(x_3d, x, y, color='black', label='GMM')
    if legend == False:
        ax.legend()
        legend = True

ax.set_zlabel('test')

fig.tight_layout()
fig.subplots_adjust(left=-0.11)  # plot outside the normal area

enter image description here

相关问题 更多 >