0

I have two plots, each with multiple subplots (panels), i.e. multiple rows and columns. Each row shows the same type of data as images with colorbars on the right side. The first plot has, say, 8 rows and 4 columns. The second one has, say, 3 rows and 4 columns. The two plots are inserted into a LaTeX pdf document on two consecutive pages, with the same width (\includegraphics[width=\hsize]{fig1.pdf}). For layout reasons, I want the panels to have exactly the same width and height when flipping between pages in a pdf reader. To guarantee this, I thus used the same subplot layout of 8 x 4 panels for the second plot and made the panels (axes) for the 5 rows where there are no data invisible.

Since the second plot has only 3 rows of data, I use fig.savefig(bbox_inches='tight') to clip the white space below those rows. Unfortunately, as the ticks on the colorbar on the right side of the last column have a different maximum number of digits (on the first and second plot, say 1 and 2 decimal digits), with bbox_inches='tight' the resulting figure width (after saving and thus also when included into the LaTeX pdf) becomes different between the two plots.

I would like to not have to use a different layout of 3 x 4 subplots for the second plot, where I have to manually adjust the figure height (and likely still don't get exactly the same panel sizes). I could adjust both the tick formatters and tick locators on the colorbars to the same maximum number of digits for the two plots, but I would prefer to let matplotlib use the default ScalarFormatter() and AutoLocator().

Optimally, I would like to clip the white space below the 3 rows for the y-direction using bbox_inches='tight', but leave the bbox untouched for the x-direction and manually adjust the right figure border via fig.subplot_params(fig_right) to the same value for the two plots.

Questions: Is it possible to use bbox_inches='tight' only for the y-direction of a figure, but use the default bbox_inches (rcParams['savefig.bbox'], defaults to None) for the x-direction? Should such a feature (accepted values 'tight_x' and 'tight_y' for bbox_inches) maybe be implemented to matplotlib (I assume (but don't know) that this would not be too difficult)? Do you have alternative ideas how I can obtain exactly the same panel sizes for the two plots?

bproxauf
  • 1,076
  • 12
  • 23
  • 1
    No it is not possible to use bbox_inches='tight' in one direction only. However, you can get the bbox, and then force the width to be the same as the first bbox. You would just get the union of all the individual bboxes of your axes and pass that instead of 'tight' – Jody Klymak Jun 13 '22 at 16:13

1 Answers1

0

Update from original poster: As noted by @Jody Klymak, it is not possible to use a tight bounding box for one direction (or one border) only, but custom bounding boxes may be used. In my case, I pass bbox_inches=None for the first plot, and bbox_inches=Bbox([[0,fig.get_tightbbox(fig.canvas.get_renderer())._bbox.y0/fig.dpi - 0.1],[fig.get_figwidth(),fig.get_figheight()]]) for the second plot. This solves the question.

bproxauf
  • 1,076
  • 12
  • 23