Writing numbers on the bars on a seaborn FacetGrid figure

Join the discussion
Registration is free. Start your own thread to ask a follow-up.
1 reply · 3K views
EngWiPy
Messages
1,361
Reaction score
61
Hello,

I want to write on the bars inside the subplots of a seaborn.FacetGrid a number that represents their height on the y-scale. How can I do that?

Suppose the code for the FacetGrid is

Python:
g = sns.FacetGrid(df, row = 'feat1', size = 5, aspect = 2)
g = (g.map(sns.countplot, 'feat2', hue = 'feat3', data = df, palette="Set1")).add_legend()

where all feat1 ... feat3 are categorical variables/features.

Thanks in advance
 
Physics news on Phys.org
Thanks. I found the answer. It is as follows for those who are interested:

Python:
g = sns.FacetGrid(...)
g = (g.map(sns.countplot, ...)).add_legend()
for ax in g.axes.ravel():#this will loop over the different figures in the grid
    for p in ax.patches:#this will loop over the different bars in each figure
        ax.annotate("%d" % p.get_height(), (p.get_x() + p.get_width() / 2., p.get_height()),
             ha='center', va='center', xytext=(0, 8), textcoords='offset points')

Fill the dots with your data, functions, and attributes.