-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
[Bug]: wspace and hspace in subfigures not working #25511
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
Thanks for the report @maurosilber. The problem is clearer if we set a facecolor for each subfigure: import matplotlib.pyplot as plt
for space in [0, 0.2]:
figs = plt.figure().subfigures(2, 2, hspace=space, wspace=space)
for fig, color in zip(figs.flat, 'cmyw'):
fig.set_facecolor(color)
fig.subplots().plot([1, 2])
plt.show() |
Just to add something, it looks like it works when using 'constrained' layout. |
The relevant code is matplotlib/lib/matplotlib/figure.py Line 2237 in 0b4e615
This didn't break - it never worked. I imagine the position logic could be borrowed from Axes.... |
I am a first time contributer, and would like to attempt to work on this if possible! Will get a PR in the coming days |
I have been trying to understand the code associated with this and have run into some issues. matplotlib/lib/matplotlib/gridspec.py Line 145 in 0b4e615
|
There is a |
I'm not quite sure what you are asking here? Constrained layout adjusts the axes sizes to fit in the figure. If you don't do constrained layout the axes labels can definitely spill out of the figure if you just use default axes positioning. |
I've been digging into this. We have a test that adds a subplot and a subfigure using the same gridspec, and the subfigure is expected to ignore the wspace on the gridspec. matplotlib/lib/matplotlib/tests/test_figure.py Lines 1425 to 1439 in ffd3b12
The use-case envisioned in the test seems entirely reasonable to me, but I'm struggling to see how we can support that while also fixing this issue. |
Why do you say the subfigure is expected to ignore the wspace? I don't see that wspace is set in the test. |
Since no wspace is passed, I assume the gridspec will have the default from rcParams, which is 0.2. |
Sure, but I don't understand what wouldn't work in that example with |
Yes, I think in the test example, if both subfigure and subplot were respecting the 0.2 wspace then the left-hand subplots would be narrower and we’d have more whitespace in the middle. Currently in this example the total width of the two lefthand subplots looks about the same as the width of the righthand one, so overall the figure seems well-balanced. Another test here explicitly doesn’t expect any whitespace between subfigures, though in this case there are no subplots so you could just pass matplotlib/lib/matplotlib/tests/test_figure.py Lines 1368 to 1388 in 8293774
|
Can we just make the default wspace for subfigures be zero? |
|
In this case, as you point out, it is better if subplots and subfigures get different wspace values, even if they are the same grid spec level. I'm suggesting that subfigures ignores the grid spec wspace (as it currently does) and if we want a wspace for a set of subfigures that be a kwarg of the subfigure call. However, I never use wspace nor hspace, and given that all these things work so much better with constrained layout, I'm not sure what the goal of manually tweaking the spacing is. |
OK, how’s this? #25960 |
Bug summary
wspace
andhspace
inFigure.subfigures
do nothing.Code for reproduction
Actual outcome
Same figure independently of the values of hspace and wspace.
Expected outcome
matplotlib/lib/matplotlib/figure.py
Lines 1550 to 1554 in b3bd929
Additional information
No response
Operating system
OS/X
Matplotlib Version
3.7.1
Matplotlib Backend
MacOSX
Python version
Python 3.10.9
Jupyter version
No response
Installation
conda
The text was updated successfully, but these errors were encountered: