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?
Asked
Active
Viewed 270 times
-2
-
You should be using `sns.catplot` with `kind='bar'`. As per the documentation, it is not recommended to directly use [`FacetGrid`](https://seaborn.pydata.org/generated/seaborn.FacetGrid.html), – Trenton McKinney Oct 12 '22 at 15:21
-
Asking because I don't know why I should not be using Facetgrid? – Oğuz Underwood Oct 12 '22 at 15:36
-
It says right in the FacetGrid documentation in the **Warning** section – Trenton McKinney Oct 12 '22 at 15:48
1 Answers
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)

Trenton McKinney
- 56,955
- 33
- 144
- 158

r-beginners
- 31,170
- 3
- 14
- 32
-
-
Your question was unfortunately closed, but I have made a correction to resolve the error and change the color of the bar graph to one color. – r-beginners Oct 13 '22 at 01:45
-
1