Skip to content

figure.clf() and subplots_adjust #11059

Closed

Description

@fredrik-1

This might not be a bug but I doubt that it is an intended behavior. figure.clf() don't change the subplot parameters to any default values so it is not enough to do figure.clf() when testing to plot with or without for example tight_layout . This is easy to fix by adding for example:

keys = ('left', 'right', 'bottom','top','wspace','hspace')
subplots_default = {key : rcParams['figure.subplot.' + key] for key in keys}
self.subplotpars.update(**subplots_default)

to figure.clf()

When I looked into this I also found that the docstrings for subplots_adjust and SubplotParams are kind of confusing and not really correct. The default is, except in SuplotParams.__ini__ (that most people probably newer use directly), not the rc parameters that it seems but the actual attributes in the SuplotParams object. So it is not trivial to start all over with the default rc variables after a change in the subplotparameters by for example tight_layout.

  • Would it be good to add an option in subplot_adjust to adjust to the default parameters?
  • Would it be good to add a get function for the subplotparameters?

The documentation should probably be changed anyway. Are there any method to avoid writing almost the same things several times? figure.subplots_adjust and plt.subplots_adjust should have more or less the exact same docstring and the SuplotParams should also have a similar one.

Activity

  1. changed the title [-]fig.clf() and subplots_adjust[/-] [+]figure.clf() and subplots_adjust[/+] on Apr 16, 2018
  2. jklymak commented on Apr 16, 2018

    @jklymak
    Member

    I'm not sure if this is intended behaviour or not. If understand what you are describing, it seems like clf should indeed reset those values. Not sure about the utility of getters and setters etc.

    There is a tension in the docs between reproducibility and simplicity. See the legend doc (in Master) for one way to make the docstrings consistent.

  3. ImportanceOfBeingErnest commented on Apr 16, 2018

    @ImportanceOfBeingErnest
    Member

    A crazy way to reset the subplotpars to the rcParams' values would be

    fig.subplotpars = type(fig.subplotpars)()
    fig.subplots_adjust()
    

    ...probably not the recommended way.
    Similarly you may get all subplot parameters by just using the SuplotParams' __dict__

    print(fig.subplotpars.__dict__)
    

    So given that those are really suboptimal "solutions", I think it would be good to

    • introduce a reset_to_rc function into SubplotParams.
    • let clf call this function.
    • introduce either a figure method fig.get_subplotparams() or a subplot param method fig.subplotparams.get_params() returning a dictionary of the 6 subplotparams.
  4. added
    API: consistencyConsistency of the matplotlib API, including naming, behavior, defaults, …
    on Jul 2, 2018
  5. efiring commented on Jul 2, 2018

    @efiring
    Member

    I added two labels because

    • Documentation needs improvement in any case.
    • We might want to modify the API.
  6. added this to the v3.3.0 milestone on Sep 6, 2019
  7. modified the milestones: v3.3.0, v3.4.0 on May 7, 2020
  8. modified the milestones: v3.4.0, v3.5.0 on Jan 27, 2021
  9. modified the milestones: v3.5.0, v3.6.0 on Sep 25, 2021
  10. modified the milestones: v3.6.0, v3.7.0 on Jul 8, 2022
  11. modified the milestones: v3.7.0, v3.7.1 on Feb 14, 2023
  12. modified the milestones: v3.7.1, v3.7-doc on Mar 4, 2023
  13. modified the milestones: v3.7-doc, v3.8-doc on Sep 9, 2023
  14. github-actions commented on Sep 9, 2024

    @github-actions

    This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

  15. added
    keepItems to be ignored by the “Stale” Github Action
    and removed
    status: inactiveMarked by the “Stale” Github Action
    on Sep 9, 2024
  16. timhoffm commented on Sep 9, 2024

    @timhoffm
    Member

    The first and most important thing to do here is define what "clear" means for a figure. To do that, we should identify the use case ("Why clear and reuse a figure instead of just creating a new one?"). I see the main application when the figure is embedded in an application and you want to update the plot. Therefore:

    clear() should (and currently does) remove:

    • all content (subfigs, Axes, Legends, suptitle etc.)

    clear() should (and currently does) keep:

    • all "wiring", canvas, toolbar, embedding in parent figs (for subfigs), etc.
    • figure size, dpi

    I'm undecided on (and believe they are currently not reset):

    • patch properties (facecolor, frame, ...)
    • layout engine
    • subplotpars

    Whether they should be reset or not depends on how similar / different the new plot is. One can argue that these are similar fundamental visual properties like figure size and thus should not be reset. OTOH if the new should look substantially different, it's cumbersome to reset these manually.

  17. modified the milestones: v3.8-doc, v3.11.0 on Apr 23, 2025
  18. removed
    keepItems to be ignored by the “Stale” Github Action
    on May 7, 2026
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

    API: consistencyConsistency of the matplotlib API, including naming, behavior, defaults, …Documentation

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions