You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Recently I had a case of layouting subfigures where I could have benefitted from a subfigure_mosaic function, similar to subplot_mosaic.
Proposed solution
Provide subfigure_mosaic.
Bonus points if the implementation shares as much as possible with subplot_mosaic (I suspect we can just factor out everything and pass the "child-adding method" (fig.add_subplot or fig.add_subfigure) as a parameter to the internal implementation?).
I think this is a good first issue (limited API discussion as "match subplot_mosaic" is going to be the answer) and only affects a very limited part of the codebase.
Labeling this as medium difficluty because the subplot_mosaic code is a bit complex (it supports infinite nesting of layouts via recursion) and some of the internal variables are not super clearly named (I named them).
is the only Axes-specific thing and parameterizin _do_layout to take make_the_sub_thing as an input would work.
One maybe interesting question is that for nested layouts to we want to have sub-figures of sub-figures or flatten all of the sub-figures to be children of the Figure they are called on? I think the return has to be flat so I would lean towards the implementation also being flat (with the nesting in the spec being a details of the layout not an expression of actual nesting) and if the user does really want sub-figure of sub-figure they can call subfigure_moasic in a loop to build up exactly the tree they want.
Tasks:
understand how subplot_mosaic currently works
refactor the layout logic into a private method
pass the "make the thing" method through _do_layout
add new FigureBase method subfigure_mosaic
tests (lots of tests....should be able to adapt the subplot_mosaic tests)
whats new entry (suspect that this is something people will be excited about so we want to highlight it)
software estimate are notoriously hard, but I would guess 4-20 hours with a bulk of the time being in writing tests and docs.
One maybe interesting question is that for nested layouts to we want to have sub-figures of sub-figures or flatten all of the sub-figures to be children of the Figure they are called on? I think the return has to be flat
Hi @tacaswell, I have some questions about the tasks you laid out.
When you say "refactor the layout logic into a private method", do you want that to be a private method of FigureBase that both subplot_mosaic and subfigure_mosaic can use?
Also, I'm a bit confused by the make_the_sub_thing parameter. Would this be add_subplot for subplot_mosaic and maybe add_subfigure for subfigure_mosaic?
When you say "refactor the layout logic into a private method", do you want that to be a private method of FigureBase that both subplot_mosaic and subfigure_mosaic can use?
yes, or a class method, top level (private) function if it does not need any state. The main goal is to not have two copies of this somewhat long subtle code in the code base.
Would this be add_subplot for subplot_mosaic and maybe add_subfigure for subfigure_mosaic?
Yes, exactly that. The logic is "I need to add a sub-version of " which we can specify via dependency injection.
@brandall25 Hi! Not sure I'm qualified to answer you here, but I've been working on this since last week and did just what you described for both of your questions. It seems to work.
The only problem now is writing proper docs and tests, because just passing a string with no kwargs for subfigure_mosaic doesn't create anything pretty like the plot function does. In fact, it looks like nothing at all. That's probably intended (since it does work if you add kwargs), but still.
Edit: sorry, didn't see there was already an answer! @tacaswell should I make a PR for this? I didn't comment before because I wasn't sure I'd be able to do anything at all and then it was late.
Hi @mgamzec - as mentioned above there is still a PR open for this although it hasn't seen movement in a while. In the meantime, please try and pick another issue to work on. Thanks!
#26061 is still work in progress. Please do not open a concurrent pull request. If you want to contribute here, check the status of #26021 and try help clarifying the state / comment on what still needs to be done.
Given there is already a PR, and the length of its discussion I'm removing "good first issue".
On a side note, I don't think subfigure_mosaic is really necessary as the need for complex subfigure layouts should be very rare. I won't get involved here and leave it to the original proponents to decide the future of this topic.
Activity
tacaswell commentedon May 23, 2023
I think this is a good first issue (limited API discussion as "match subplot_mosaic" is going to be the answer) and only affects a very limited part of the codebase.
Labeling this as medium difficluty because the subplot_mosaic code is a bit complex (it supports infinite nesting of layouts via recursion) and some of the internal variables are not super clearly named (I named them).
The source is:
matplotlib/lib/matplotlib/figure.py
Lines 1821 to 2128 in 82cfed3
On a cursory skim, I think that
matplotlib/lib/matplotlib/figure.py
Lines 2081 to 2087 in 82cfed3
_do_layoutto takemake_the_sub_thingas an input would work.One maybe interesting question is that for nested layouts to we want to have sub-figures of sub-figures or flatten all of the sub-figures to be children of the Figure they are called on? I think the return has to be flat so I would lean towards the implementation also being flat (with the nesting in the spec being a details of the layout not an expression of actual nesting) and if the user does really want sub-figure of sub-figure they can call
subfigure_moasicin a loop to build up exactly the tree they want.Tasks:
subplot_mosaiccurrently works_do_layoutsubfigure_mosaicsoftware estimate are notoriously hard, but I would guess 4-20 hours with a bulk of the time being in writing tests and docs.
anntzer commentedon May 23, 2023
Flat would seem normal to me.
brandall25 commentedon May 30, 2023
Could I have a go at this?
melissawm commentedon May 30, 2023
Hi @brandall25 - go ahead! Make sure you check our contributor guide and let us know if you have questions. Thanks!
brandall25 commentedon Jun 2, 2023
Hi @tacaswell, I have some questions about the tasks you laid out.
When you say "refactor the layout logic into a private method", do you want that to be a private method of
FigureBasethat bothsubplot_mosaicandsubfigure_mosaiccan use?Also, I'm a bit confused by the
make_the_sub_thingparameter. Would this beadd_subplotforsubplot_mosaicand maybeadd_subfigureforsubfigure_mosaic?Thanks!
tacaswell commentedon Jun 2, 2023
yes, or a class method, top level (private) function if it does not need any state. The main goal is to not have two copies of this somewhat long subtle code in the code base.
Yes, exactly that. The logic is "I need to add a sub-version of " which we can specify via dependency injection.
turnipseason commentedon Jun 2, 2023
@brandall25 Hi! Not sure I'm qualified to answer you here, but I've been working on this since last week and did just what you described for both of your questions. It seems to work.
The only problem now is writing proper docs and tests, because just passing a string with no kwargs for
subfigure_mosaicdoesn't create anything pretty like the plot function does. In fact, it looks like nothing at all. That's probably intended (since it does work if you add kwargs), but still.Edit: sorry, didn't see there was already an answer! @tacaswell should I make a PR for this? I didn't comment before because I wasn't sure I'd be able to do anything at all and then it was late.
brandall25 commentedon Jun 2, 2023
@turnipseason Hello, thanks for responding! Sounds like you have it covered, I think I'll move on to another issue.
turnipseason commentedon Jun 2, 2023
@brandall25 Hi! Sorry, I really should've written a comment earlier. Good luck with the other issues!
tacaswell commentedon Jun 2, 2023
@turnipseason Yes please to the PR.
Adding to or mimicing https://github.com/matplotlib/matplotlib/blob/main/galleries/users_explain/axes/mosaic.py for sub-figures would probably make sense. You can use the same
fig.text(.5, .5, LABEL, ha='center', va='center')(in fact if you pass aFigureobject toidentify_axesI think it will "just work"... 🦆 typing is fun when it works :)Alternatively looking https://matplotlib.org/devdocs/gallery/subplots_axes_and_figures/subfigures.html#figure-subfigures and using the "change the patch color" as a way to label the space (or both!) may be a good starting point for documentation inspiration.
Similarly look at how
subplot_mosaicand SubFigure are tested, I suspect they can be adapted.tacaswell commentedon Jun 2, 2023
also don't worry about the first draft being perfect, we expect iteration on PRs.
6 remaining items
mgamzec commentedon Jun 9, 2025
Is the issue still open?
melissawm commentedon Jun 9, 2025
Hi @mgamzec - as mentioned above there is still a PR open for this although it hasn't seen movement in a while. In the meantime, please try and pick another issue to work on. Thanks!
ENH: Implement subfigure_mosaic for creating SubFigure layouts
JoshTVR commentedon Mar 30, 2026
Hi, I see this was already implemented by @turnipseason.
Should this issue be closed?
Thanks!
timhoffm commentedon Mar 30, 2026
No. The PR (#26061) is not merged.
p-sreesankar commentedon May 30, 2026
Hello, Can I have a go at this issue.
timhoffm commentedon May 31, 2026
#26061 is still work in progress. Please do not open a concurrent pull request. If you want to contribute here, check the status of #26021 and try help clarifying the state / comment on what still needs to be done.
Given there is already a PR, and the length of its discussion I'm removing "good first issue".
On a side note, I don't think
subfigure_mosaicis really necessary as the need for complex subfigure layouts should be very rare. I won't get involved here and leave it to the original proponents to decide the future of this topic.story645 commentedon May 31, 2026
So I've been really wanting it lately cause I use subfigures a lot for reusing panels across paper/poster/slide deck.