-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
[Bug]: add_subfigure does not respect GridSpec parameters #28132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
There was some discussion at #25511 about if and when subfigures should respect the gridspec's white space settings. |
It is true that spacing/padding is ignored when computing the position in The code for this is here, where we recompute sizes of boxes based only on height/width ratios: matplotlib/lib/matplotlib/figure.py Lines 2284 to 2293 in eb62d69
Replacing this with: bbox = self._subplotspec.get_position(self)
self.bbox_relative.p0 = bbox.p0
self.bbox_relative.p1 = bbox.p1 Resolves to the expected behavior of this issue, though does cause ~6 test failures (though actually mostly relatively minor, to be honest... some slight tweaking to text/colorbar positions, though some more distinct differences as well). I suspect that the reason for ignoring the padding has to do with the idea that a subfigure has its own internal padding usually, and so not having the padding may in many cases make the subfigures look better by default (perhaps especially when juxtaposed with non-subfigure subplots?) That said, using I will tag in @jklymak and @anntzer to provide opinions here (as original author and the one who rewrote the code linked above, respectively) If we decide to change behavior, the technical solution is pretty easy, though may need to either replace test files or adjust test code to get the reproduce the old figures (e.g. by explicitly setting hspace/wspace to 0). Xrefs:
I do not see specific conversation in these threads as to whether to account for padding or not. |
The original subfigures was not meant to account for paddings in a GridSpec. #25960 added keywords for
Overall I think the current API is probably the correct one (ignore GridSpec, and control with keyword arguments). As stated before, users should not typically be creating gridspecs themselves. The above can be accomplished with
The only thing I'd add is I wouldn't objects if we wanted to add |
It certainly would be good to have options like import matplotlib.pyplot as plt
import numpy as np
dpi = 100
width = 600
height = 600
main = plt.figure(figsize=(width / dpi, height / dpi), dpi=100,
facecolor='#000000', frameon=False)
colors = np.array([
['red', 'black', 'green', 'blue'],
['brown', 'gray', 'white', 'gold']
])
sf = main.subfigures(2, 4)
for row in range(2):
for col in range(4):
sf[row, col].set_facecolor(colors[row, col])
sf[row, col].add_subplot(xticks=[], yticks=[])
plt.show() The idea is that I can highlight an |
Bug summary
According the documentation for
Figure.add_subfigure
, you should be able to useGridSpec
to create a set of subfigures to fill a certain space of a parent figure. However, no matter what settings are input intoGridSpec
, the parent figure is divided into equally sized subfigures. Doing this same process withFigure.add_subplot
produces the expected results with the axes bounded by the input extent and separated by the correcthspace
andwspace
.Code for reproduction
Actual outcome
The output is equally sized subfigures without any space between them and the full extent being used despite the input restrictions.

Expected outcome
What is expected is a properly spaced areas with the proper extent. This can be produced when using

add_subplot
instead ofadd_subfigure
.Additional information
No response
Operating system
No response
Matplotlib Version
3.8.3
Matplotlib Backend
No response
Python version
3.12.2
Jupyter version
No response
Installation
conda
The text was updated successfully, but these errors were encountered: