0

This question is specific to adding a title to a facet grid plot where only columns are defined, it is not answered by How to add a title to Seaborn Facet Plot.

I want to add a title to a FacetGrid where there is a single row of data, i.e. row=None. When I do this the subplots_adjust call doesn't seem to be applied correctly and the title clashes with the column headings.

Here I show that the method in use works correctly for a FacetGrid with col + row data:

tips = sns.load_dataset("tips")
g = sns.FacetGrid(tips, col="sex", row="smoker", margin_titles=True)
g.map(sns.scatterplot, "total_bill", "tip")
g.fig.subplots_adjust(top=.9)
g.fig.suptitle('col and row - needs to be long to show issue', size=14)
g.savefig('col_and_row.png')

shows plot avoids title then both col and row defined

The same but omitting the row:

tips = sns.load_dataset("tips")
g = sns.FacetGrid(tips, col="sex", margin_titles=True)
g.map(sns.scatterplot, "total_bill", "tip")
g.fig.subplots_adjust(top=.9)
g.fig.suptitle('col only - needs to be long to show issue', size=14)
g.savefig('col_only.png')

As you can see the title now clashes with the column headings:

shows title clash when row omitted

Is this a bug or have I missed something?

Keiran Raine
  • 163
  • 9
  • `g.fig.subplots_adjust(top=.8); g.fig.suptitle(..., y=0.95)` see [this post](https://stackoverflow.com/questions/12750355/python-matplotlib-figure-title-overlaps-axes-label-when-using-twiny) – JohanC May 14 '20 at 23:25

1 Answers1

1

The output is correct as per your code. You have given top=.9 and hence the subplots layout is adjusted according to '.9' value of 'top'. Try adjusting the 'top' value based on your requirments to get the desired output.

A slight modification to your code i.e. g.fig.subplots_adjust(top=.8) produced this output:

enter image description here

You can refer the parameters of subplots_adjust() at the following link: https://matplotlib.org/api/_as_gen/matplotlib.pyplot.subplots_adjust.html

Nitish
  • 546
  • 1
  • 4
  • 5