The following code produces the following output.
import seaborn as sns
penguins = sns.load_dataset("penguins")
sns.displot(data=penguins, x="flipper_length_mm", y="bill_length_mm", col="species", row="sex")
I'd like to know how many observations (N) were used in each panel and I'd like to show them inside each panel, if possible. Does somebody know how to accomplish this?
You can use g.map_dataframe(func)
where func
is user defined and receives two parameters: a color (to allow a consistent coloring) and data
, the subset of the dataframe used for this subplot. Each time func
is called, matplotlib's "current ax" will be set to a specific subplot. plt.gca()
gets access to that ax
.
Here is an example adding the length of the subset via "axes coordinates":
import matplotlib.pyplot as plt
import seaborn as sns
def show_counts(color, data):
ax = plt.gca()
ax.text(0.5, 0.95, f'Count: {len(data)}', ha='center', color=color, transform=ax.transAxes)
penguins = sns.load_dataset("penguins")
g = sns.displot(data=penguins, x="flipper_length_mm", y="bill_length_mm", col="species", row="sex")
g.map_dataframe(show_counts)
plt.show()