Search code examples
pythonpandasseabornfacet-grid

How can I add a title to a relplot in seaborn?


This should be easy, but I've tried many things (based on seaborn relplot: how to control the location of the legend and add title, How to add a title to Seaborn Facet Plot and also the documentation), but nothing worked.

Things I've tried:

g = sns.relplot(x="col1", y="col2", data=df, height=6, aspect=2);
g.add_legend(title="Col1 x Col2");

Also:

g = sns.relplot(x="col1", y="col2", data=df, height=6, aspect=2).set_title('Col1 x Col2')

And:

plt.title('Col1 x Col2')
g = sns.relplot(x="col1", y="col2", data=df, height=6, aspect=2);

Well, nothing worked. I want the title on the top of the graph, just like what plt.title() usually do.


Solution

  • As you failed to include the source DataFrame, I used another one:

    df = sns.load_dataset("tips")
    

    Then I created the relplot the following way:

    g = sns.relplot(x="total_bill", y="tip", hue="day", data=df)
    g.fig.suptitle('Col1 x Col2', fontsize=16)
    g.fig.subplots_adjust(top=0.9);
    

    The result I got was:

    enter image description here

    Of course, the content of the image is different, but at least you have a title here, with the image adjusted down, to leave some space below the title.