Matplotlib 3d plot: how to get rid of the excessiv

2019-02-21 00:30发布

If I make a 3d plot in 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')

It will produce:

enter image description here

The left side have excessive white space. I want to know if it is possible to get rid of it?

1条回答
小情绪 Triste *
2楼-- · 2019-02-21 01:09

It's probably too late, but I came across similar problems and here is what I did to remove the white space: use fig.subplot_adjust() to put left/right outside the normal region. In your case I found fig.subplot_adjust(left=-0.11) gives a reasonable result.

Full code below:

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

查看更多
登录 后发表回答