8000 figure.clf() and subplots_adjust · Issue #11059 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

figure.clf() and subplots_adjust #11059

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

Closed
fredrik-1 opened this issue Apr 16, 2018 · 5 comments · Fixed by #27183
Closed

figure.clf() and subplots_adjust #11059

fredrik-1 opened this issue Apr 16, 2018 · 5 comments · Fixed by #27183
Labels
API: consistency Documentation keep Items to be ignored by the “Stale” Github Action
Milestone

Comments

@fredrik-1
Copy link
Contributor

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.

@fredrik-1 fredrik-1 changed the title fig.clf() and subplots_adjust figure.clf() and subplots_adjust Apr 16, 2018
@jklymak
Copy link
Member
jklymak commented Apr 16, 2018

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.

@ImportanceOfBeingErnest
Copy link
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.

@efiring
Copy link
Member
efiring commented Jul 2, 2018

I added two labels because

  • Documentation needs improvement in any case.
  • We might want to modify the API.

Copy link
github-actions bot commented Sep 9, 2024

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!

@github-actions github-actions bot added the status: inactive Marked by the “Stale” Github Action label Sep 9, 2024
@timhoffm timhoffm added keep Items to be ignored by the “Stale” Github Action and removed status: inactive Marked by the “Stale” Github Action labels Sep 9, 2024
@timhoffm
Copy link
Member
timhoffm commented Sep 9, 2024

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.

@QuLogic QuLogic modified the milestones: v3.8-doc, v3.11.0 Apr 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API: consistency Documentation keep Items to be ignored by the “Stale” Github Action
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants
0