Too much white space around graphs in 3d projection
When using 3d projection:
fig = plt.figure(figsize=(20,15)) ax = fig.gca(projection='3d')
and save the figure into png file, there is way too much white space from left right top and bottom, so when I want to add this graph into latex and use it in publication, I have to do post-processing of the image and remove unnecessary amount of blank space.
Hi @pepe78 that sounds annoying indeed. However, I think that this can likely be resolved with the existing tools in matplotlib. Can you please post about this (with a picture of the output and a description of what you'd expect) on https://discourse.matplotlib.org/ which is the best place for getting help with matplotlib.
Done (reported at https://discourse.matplotlib.org/). As far as "resolved with the existing tools in matplotlib", it would be nice to have by default less amount of blank space as it is done when not doing 3d projections (2d graphs). Thanks!
I'm not sure why there is such a large margin around axes3d plots. I have a sneaking suspicion that the margin was developed outside the subplot paradigm, which also have margins, and now we have two sets of margins. Definitely when you add an axes directly via Axes3d(fig) (which we want to deprecate), the margins are smaller.
This was great advice I got on discourse:
fig = plt.figure(figsize=(20,15), constrained_layout=True) plt.savefig('1.png', bbox_inches='tight')
Left and right margins were minimal when saved like that (and top and bottom seem also smaller).
I'll actually re-open, because to me, at least, there is still a mystery about why the margins are so much larger than other axes.
That makes sense - as I said before, it would be nice to have less margins on sides by default. When doing 2d plots, this actually really works nicely by default.
Yes, the original Axes3D didn't work within the subplot() framework. And subplots leave extra room for the static axes labels, meanwhile, Axes3D needed to account for extra space for its axes labels within the axes plotting area. Originally, this was fine because you didn't make Axes3D objects using subplots, so you didn't get allocated extra margins for axes labels. When it became possible to specify projection='3d' to a subplot call, this had the inadvertent effect of getting extra margin space that wasn't really needed.
I never did figure out a good solution for this that wouldn't act counter-intuitively. Perhaps Axes3D needs to be reworked such that it can interpret and express the margin parameters itself rather than letting it be handled by 2D-based assumptions?
I stumbled over this issue as well while trying to plot a representation of the Riemann zeta function.
Zooming in just cuts off left and right of the plot. :-/

Just a note that you can control the margin sizes to some extent using gridspec_kws.
fig, ax = plt.subplots(subplot_kw={'projection': '3d'},
gridspec_kw=dict(top=1.07, bottom=0.02, left=0, right=1))
fig.savefig('axes3d.png', dpi=600, bbox_inches='tight')
