-2

I have created a facetgrid where columns are my years, x axis are months and Y axis is consumption levels. I have put an horizontal line showing the mean consumption level for every subplot. I also would like to state with an annotate what that mean number is for every column, here yearly data. Any ideas how to do it?

My facetgrid

tdy
  • 36,675
  • 19
  • 86
  • 83

1 Answers1

1

Since you did not present any data, I applied your code using the data in the reference. I created a function to annotate the string and add a horizontal line since the function drawing the horizontal line is unknown.

import matplotlib.pyplot as plt
import seaborn as sns

colors = sns.color_palette()

# load seaborn sample dataset
flights = sns.load_dataset('flights')

def annotate(data, **kws):
    n = data.passengers.mean()
    ax = plt.gca()
    ax.text(.1, .8, "Mean = {:.1f}".format(n), transform=ax.transAxes)
    ax.axhline(y=n, color='red')
    
g2 = sns.catplot(flights, x='month', y='passengers', kind='bar', col='year', col_wrap=4, color=colors[0])
g2.map_dataframe(annotate)

enter image description here

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
r-beginners
  • 31,170
  • 3
  • 14
  • 32