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
[Bug]: get_backend() clears figures from Gcf.figs if they were created under rc_context #23298
Comments
My knee-jerk guess is that :
This is probably been a long standing latent bug, but was brought to the front when we made the backend resolution lazier. |
That is indeed the problem. I do not see an easy way to fix this that we can backport to 3.5.x, PR coming with a way to fix this for 3.6.... |
The motivating issue is that if the backend was the auto backend sentinel and the backend was first fully resolved in an `rc_context` block, then we would reset back to the sentinel an exit which would lead to strange behavior with figures being erroneously closed. Adding a general purpose exclusion list seems like a better option than special casing either the backend key or the backend value. Closes matplotlib#23298
The motivating issue is that if the backend was the auto backend sentinel and the backend was first fully resolved in an `rc_context` block, then we would reset back to the sentinel an exit which would lead to strange behavior with figures being erroneously closed. This special cases the 'backend' key to not be reset on `__exit__` Closes matplotlib#23298
I encountered this bug when using matplotlib (3.5.2) indirectly via seaborn. Before my environment is updated with matplotlib 3.6, what would you recommend as a workaround for this bug? The workaround should preferably have as little side effect as possible. |
Adding import matplotlib
matplotlib.get_backend()
del matplotilb should force the backend resolution to work around this issue and have no other side effects. This only needs to be done once per process. |
Thank you. |
But also seaborn need not use a context manager here? |
? |
I think seaborn can turn the layout off ( I guess philosophically I don't think of rcParams as something downstream libraries should be changing, but rather defaults that the user sets. However I appreciate that there is a counter current in the project of thinking of rcParams as a global state for the library that gets manipulated at will, and I can see how the context manager helps encourage that. However, I'm just not convinced it is a good idea given the number of weird side effects it can produce. |
The rcParams very much are "a global state that can be manipulated at will" so if doing so can produce "weird side effects", it seems important for matplotlib to clearly document them. |
Bug summary
calling
matplotlib.get_backend()
removes all figures fromGcf
if the first figure inGcf.figs
was created in anrc_context
.Code for reproduction
Actual outcome
Expected outcome
The figure should not be missing from
Gcf
. Consequences of this are, e.g,plt.close(fig2)
doesn't work becauseGcf.destroy_fig()
can't find it.Additional information
No response
Operating system
Xubuntu
Matplotlib Version
3.5.2
Matplotlib Backend
QtAgg
Python version
Python 3.10.4
Jupyter version
n/a
Installation
conda
The text was updated successfully, but these errors were encountered: