Skip to content

[Bug]: Suptitle not visible with subfigures #24910

Closed

Description

@mwaskom

Bug summary

In a fairly basic example using subfigures, a suptitle added to the parent figure is not visible:

Code for reproduction

f = plt.figure(figsize=(4, 3))
sf1, sf2 = f.subfigures(1, 2)
sf1.subplots()
sf2.subplots()
f.suptitle("It's a bird, it's a plane, it's a suptitle")

Actual outcome

test

Expected outcome

Visible suptitle.

Additional information

It looks like this is happening because the subfigures have a solid facecolor that obscure the suptitle:

f = plt.figure(figsize=(4, 3))
sf1, sf2 = f.subfigures(1, 2)
sf1.subplots()
sf2.subplots()
sf1.set_facecolor((.8, 0, 0, .3))
f.suptitle("It's a bird, it's a plane, it's a suptitle")

test

Setting the zorder of the suptitle to a large number has no apparent effect.

Matplotlib Version

3.6.2

Matplotlib Backend

module://matplotlib_inline.backend_inline

Activity

  1. jklymak commented on Jan 8, 2023

    @jklymak
    Member

    I don't think we specifically set the background color of subfigures to anything different than a figure, which defaults to white. We could, by default, make subfigures transparent, but not 100% clear to me that is the right thing to do.

    FWIW, this problem goes away with layout='constrained' which makes the subfigure the correct size to allow the suptitle.

  2. mwaskom commented on Jan 9, 2023

    @mwaskom
    Author

    We could, by default, make subfigures transparent, but not 100% clear to me that is the right thing to do.

    I agree that this could have the potential to add unpredictable complexity, but in practice what do you think the failure mode would look like?

    If the default subfigure facecolor is transparent, it will appear as if the overlying subfigures have whatever facecolor is set for figures by default (i.e. in the rc params), so changing the figure default will still "work". And you could still have subfigures listen to the facecolor parameter passed when they are created, so users could make a subfigure within a larger figure that has a distinct background.

    The only potential confusing situation that comes to mind is if a user set the rc parameter to a specific value, set the figure facecolor to a different value, and then expects a subfigure made without an explicit facecolor to follow the rc parameter rather than inherit from the parent. While that strikes me as unusual (I think I would maybe expect inheritance? but not sure) it seems like there's an easy route to the solution ("ah, it's inheriting the parent figure color, so let me try setting the facecolor when I create the subfigure").

    In contrast, the root cause of the behavior here is not obvious; it just looks like the suptitle "isn't working" for some reason. Note how the seaborn issue was presented, it appeared to the user (who is sharp) that the suptitle was "being removed". It took me a while to determine that it's actually there, just "under" the subfigure (especially since zorder didn't work, which I tried first).

    And while it's good to know that constrained_layout will help with suptitle, I don't think tight_layout accounts for suptitles, and neither layout engine handles similar edge cases like manually added text artists representing captions or panel labels. (Right?)

    So on balance, I think that making subfigures have transparent facecolors by default will strictly reduce surprise, but maybe I'm not thinking of a circumstance where that's not true.

  3. thuiop commented on Jan 9, 2023

    @thuiop

    Ah, so that was what happened. I was really confused by the issue as I could still see that the Text object was correctly in the fig.texts ; I had also tried moving it around in the figure and it did not work, probably because it was still under the subfigures (although in my case I thought that I was using the constrained layout while seaborn actually deactivated it, which added to the confusion).
    I do agree that this is definitely an easy trap to fall in for which I would not expect a non-expert user to quickly find the solution (using constrained layout). There is probably a way to make the suptitle show above the subfigures anyway but it does not seem obvious to me ; I agree with the case for having transparent subfigures (but could be missing something as well).

  4. anntzer commented on Jan 9, 2023

    @anntzer
    Contributor

    Not that it really matters for the discussion here, but

    I don't think tight_layout accounts for suptitles

    has been fixed in #17219.

  5. jklymak commented on Jan 9, 2023

    @jklymak
    Member

    Just to clarify - suptitles now works for simple cases that tight_layout works on. However it does not work for subfigures or other nested layouts.

  6. added
    🌱 Good first issueOpen a pull request against these issues if there are no active ones!
    on Feb 8, 2023
  7. jklymak commented on Feb 8, 2023

    @jklymak
    Member

    neither layout engine handles similar edge cases like manually added text artists representing captions or panel labels. (Right?)

    I'm not sure what you mean by this - certainly if you attach text to an axes, constrained_layout will make all the axes smaller to make sure there is no overlap, just like it does for titles and ticklabels...

    For this particular issue, I think someone could easily put in a PR to make the default facecolor for subfigures "none" and see what people think. I agree it's probably fine for 99.9% of cases, and easy to change to something else if desired. I'll mark as a good first issue and I don't think its hard, though anyone interested in pursuing should be prepared to advocate for it.

  8. mwaskom commented on Feb 8, 2023

    @mwaskom
    Author

    I'm not sure what you mean by this

    I would typically specify panel labels or a caption by adding text to the figure object, not the axes:

    f, axs = plt.subplots(2, 2, constrained_layout=True)
    f.text(.99, .01, "Source: I made it up", size=15, va="bottom", ha="right")

  9. jklymak commented on Feb 8, 2023

    @jklymak
    Member

    Yes I see. Definitely the layout engines cannot avoid random figure objects, so space would have to be reserved for a "caption" like this.

  10. mwaskom commented on Feb 9, 2023

    @mwaskom
    Author

    Yeah — there's a separate issue about adding a Figure.caption concept that layout algorithms could then be smart about (#23873) but the point here is that there are other edge cases for this issue that are not solved even by non-default "best practices".

  11. robrighter commented on Feb 19, 2023

    @robrighter
    Contributor

    I created a pull request to satisfy the suggested solution above. This makes "none" the default facecolor for subfigures:

    #25255

  12. added this to the v3.8.0 milestone on Feb 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    🌱 Good first issueOpen a pull request against these issues if there are no active ones!

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions