displot instantly close upon display when using seaborn 0.11.2 with matplotlib 3.5.2
See original GitHub issueI encountered a case of figure window closing instantly when trying to display a displot.
To reproduce, try the following with seaborn version 0.11.2 and matplotlib version 3.5.2
import seaborn
import matplotlib.pyplot as plt
sns.displot([0,0,1,1,2,3,3,1,0])
plt.show()
In seaborn version 0.11.2, a figure window will flash on the screen and instantly disappear, whereas in seaborn version 0.11.1, a histogram bar plot is displayed in a new window that persists until I closed it.
After some digging, I suspect that this is due to the handling of the backend
entry of matplotlib.rcParams
. rcParams["backend"]
by default is initialized with a sentinel object matplotlib.rcsetup._auto_backend_sentinel
. The first time someone tries to access rcParams["backend"]
, matplotlib will call matplotlib.pyplot.switch_backend
to swap in the actual backend object. The function matplotlib.pyplot.switch_backend
, when called, will first close all currently open figures.
In seaborn version 0.11.2, displot
will try to create a FacetGrid
object in which to place the plot. In the constructor of FacetGrid
, at line 408, it creates a Figure object within a context manager. As a result, the rcParams["backend"]
gets reverted to the sentinel object upon exit from the with block. The created Figure would get automatically closed the next time the code tries to access rcParams["backend"]
.
Issue Analytics
- State:
- Created a year ago
- Comments:10 (4 by maintainers)
Top GitHub Comments
I tested the branch in #2925 in my environment with matplotlib 3.5.2. It worked as expected. Thank you for working on this.
There is probably a question of philosophy and intention when it comes to using
rc_context
. It seems that some matplotlib developers believe rcParams and related mechanisms should only be used by end users in a somewhat interactive context.Let me comment on the issue to see what the matplotlib people thinks. My naive thinking is that any workaround for this issue is bound to have some side-effect (i.e.
rcParams["backend"]
become updated).