How To Annotate A Seaborn Barplot With The Aggregated Value
How can the following code be modified to show the mean as well as the different error bars on each bar of the bar plot? import numpy as np import pandas as pd import matplotlib.py
Solution 1:
- Given the example data, for a
seaborn.barplot
with capped error bars,data_df
must be converted from a wide format, to a tidy (long) format, which can be accomplished withpandas.DataFrame.stack
orpandas.DataFrame.melt
- It is also important to keep in mind that a bar plot shows only the mean (or other estimator) value
Sample Data and DataFrame
.iloc[:, 1:]
is used to skip the'stages'
column at column index 0.
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
# given data_df from the OP, select the columns except stage and reshape to long format
df = data_df.iloc[:, 1:].melt(var_name='set', value_name='val')
# display(df.head())set val
0 S1 43.3404401 S1 43.7198982 S1 46.0159583 S1 54.3405974 S2 61.609735
Updated as of matplotlib v3.4.2
- Use
matplotlib.pyplot.bar_label
- See the matplotlib: Bar Label Demo page for additional formatting options.
- Some formatting can be done with the
fmt
parameter, but more sophisticated formatting should be done with thelabels
parameter, as show in the bottom Demo example, and in How to add multiple annotations to a barplot.
- Some formatting can be done with the
- Tested with
seaborn v0.11.1
, which is usingmatplotlib
as the plot engine.
fig, ax = plt.subplots(figsize=(8, 6))
# add the plot
sns.barplot(x='set', y='val', data=df, capsize=0.2, ax=ax)
# add the annotation
ax.bar_label(ax.containers[-1], fmt='Mean:\n%.2f', label_type='center')
ax.set(ylabel='Mean Time')
plt.show()
Annotation Resources - from matplotlib v3.4.2
- Adding value labels on a matplotlib bar chart
- How to annotate each segment of a stacked bar chart
- Stacked Bar Chart with Centered Labels
- How to plot and annotate multiple data columns in a seaborn barplot
- stack bar plot in matplotlib and add label to each section
- How to add multiple annotations to a barplot
- How to plot and annotate a grouped bar chart
plot with seaborn.barplot
- Using
matplotlib
before version 3.4.2 - The default for the
estimator
parameter ismean
, so the height of the bar is the mean of the group. - The bar height is extracted from
p
with.get_height
, which can be used to annotate the bar.
fig, ax = plt.subplots(figsize=(8, 6))
sns.barplot(x='set', y='val', data=df, capsize=0.2, ax=ax)
# show the meanfor p in ax.patches:
h, w, x = p.get_height(), p.get_width(), p.get_x()
xy = (x + w / 2., h / 2)
text = f'Mean:\n{h:0.2f}'
ax.annotate(text=text, xy=xy, ha='center', va='center')
ax.set(xlabel='Delay', ylabel='Time')
plt.show()
Post a Comment for "How To Annotate A Seaborn Barplot With The Aggregated Value"