Issue
Why do I keep getting this warning whenever I try to use FacetGrid from seaborn?
UserWarning: The figure layout has changed to tight.
self._figure.tight_layout(*args, **kwargs)
I understand it's a warning and not an error and I also understand it is changing the layout to tight. My question is why does it appear in the first place? Am I missing something?
Example code:
import seaborn as sns
penguins = sns.load_dataset("penguins")
g = sns.FacetGrid(penguins, col="island")
g.map_dataframe(sns.histplot, x="bill_length_mm")
This code throws that warning.
What am I doing wrong?
I know I can hide them with warnings module but I don't wanna do that.
Solution
As mentioned in the comments, this is a matplotlib bug. It was fixed in version 3.7.3, so you can avoid it by upgrading Matplotlib.
One of the comments suggests calling plt.figure(..., layout='constrained')
, instead of tight_layout()
, and that matches a few comments I found in the docs, like the Constrained Layout Guide:
Constrained layout is similar to Tight layout, but is substantially more flexible.
I saw this warning, because I was calling subplots()
, then repeatedly plotting, calling tight_layout()
, saving the figure, and calling cla()
.
I fixed it by removing the call to tight_layout()
from the loop, and calling subplots(..., layout='tight')
.
Answered By - Don Kirkby
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.