2

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?

Serenity
  • 35,289
  • 20
  • 120
  • 115
ZK Zhao
  • 19,885
  • 47
  • 132
  • 206
  • Do you refer to the plot inside the axes or to the axes inside the figure? – Aguy Jul 27 '16 at 07:33
  • 1
    If you click on the plot and move your mouse around the plot also moves. If you move it around on the x axis by 90 degrees the z label and the z ticks will be on the other side. I assume the excessive whitespace is to accommodate this. – DavidG Jul 27 '16 at 07:58
  • @Aguy the left-most number `10`, and the white space at the left side of it and between the black background. – ZK Zhao Jul 27 '16 at 10:47
  • How do you export the figure? In case you write it to a file, try the `bbox_inches`-argument `fig.savefig('myfigure.png', bbox_inches='tight')` – wsj Jul 29 '16 at 11:56
  • @wsj I display it in Jupyter Notebook, and then drag the image out, and then save it as png. – ZK Zhao Jul 29 '16 at 14:13
  • @wsj just tried it, still the same. – ZK Zhao Jul 29 '16 at 14:14
  • Related: https://stackoverflow.com/questions/41225293/remove-white-spaces-in-axes3d-matplotlib – Ciro Santilli OurBigBook.com Nov 15 '20 at 11:16

1 Answers1

7

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

Phyinmi
  • 450
  • 7
  • 15