Skip to content

displot instantly close upon display when using seaborn 0.11.2 with matplotlib 3.5.2 #2914

@kailizcatman

Description

@kailizcatman

I 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"].

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions