0

Suppose I have the following Seaborn FacetGrid plot using sns.lmplot():

import seaborn as sns
import matplotlib.pyplot as plt
from palmerpenguins import load_penguins
df = load_penguins()

g = sns.lmplot(
    data=df, x="bill_length_mm", y="bill_depth_mm",
    col="species", row="sex", height=3,
)

plt.show()

lmplot example

How can I include a title and subtitle for the entire figure? Thanks to this question and this question I know I can add a title by including something like this:

g.fig.suptitle("Palmer's Penguins", x = 0.5, y = 1.1, fontsize = 16)

lmplot example with title

But neither of these questions address how to include a subtitle in addition to the title. If possible, I would prefer to stick with an object-oriented approach (for example, if there were something like g.fig.subtitle()) as opposed to a more generalized PyPlot approach (like something that might use plt.title()).

jglad
  • 120
  • 2
  • 2
  • 13
  • There is a y-value for height adjustment in the title settings already set up, so I think that would be the best choice. `g.fig.suptitle("Palmer's Penguins", x=0.5, y= 1.05, fontsize=16)` – r-beginners Feb 08 '23 at 04:58

1 Answers1

0

I found a solution using g.fig.text() to manually add a subtitle:

import seaborn as sns
import matplotlib.pyplot as plt
from palmerpenguins import load_penguins
df = load_penguins()

g = sns.lmplot(
    data=df, x="bill_length_mm", y="bill_depth_mm",
    col="species", row="sex", height=3,
)

g.fig.suptitle("Palmer's Penguins", x = 0.5, y = 1.1, fontsize = 16)
g.fig.text(x=0.435, y=1.025, s="This is a subtitle")

plt.show()

lmplot example with title and subtitle

It takes some adjusting to the x and y arguments to center it as desired, but this seems to be the best option available and it does the trick.

jglad
  • 120
  • 2
  • 2
  • 13