I have a bunch of plots, one is a 3D projection. All the plots look good, except the 3D plot has a ton of empty space above and to the left (see the example). The bottom and right margins look fine, just not the top and left margins. Is there any way to remove this extra white space?
I have tried manually changing the margins but with no apparent effect (they were 0.5, 0.5, 0). The matplotlib rc file has savefig.bbox = tight
. I tried messing with the position bbox, but that hasn't gone well being a subplot. The problem seems to be related to a 3D plot in a subplot and not alone.
EDIT
Matplotlib 3d plot: how to get rid of the excessive white space? does not answer this question unfortunately, because the 3D plot is not in a sub plot. Changing the left in subplots_adjust
also damages the non 3D plot. Remove white spaces in Axes3d (matplotlib) also largely requires the same underlying function subplots_adjust
for the bulk of the adjustment.
Example:
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(2, 1, 1, projection='3d')
x_labels = [10,20,30]
x = [1,2,3,4]
y = [3,1,5,1]
for label in x_labels:
x_3d = label * np.ones_like(x)
ax.plot(x_3d, x, y, color='black')
#
ax.set_zlabel('test')
ax = fig.add_subplot(2, 1, 2)
time = np.arange(0, 10, 0.1)
ax.plot(time, np.sin(time))
fig.tight_layout()
fig.subplots_adjust(left=-0.11) # plot outside the normal area
canvas = FigureCanvas(fig)
canvas.print_figure(r'D:\test.png')