Home > Back-end >  Plotting percentile values as errorbars on seaborn barplot
Plotting percentile values as errorbars on seaborn barplot

Time:03-04

I want to plot a bar chart on seaborn and include custom errorbars. My MWE is

import pandas as pd
import seaborn as sns

w = pd.DataFrame(data={'length': [40,35,34,40,38,39,38,44,40,39,35,46],
                       'species': ['A','A','A','A','B','B','B','B','C','C','C','C'],
                       'type': ['today','avg','10pc','90pc','today','avg','10pc','90pc','today','avg','10pc','90pc']
                      },
                )
w['Date'] = pd.to_datetime('2021-09-20')
w.set_index('Date',inplace=True)

w0 = w.loc[(w.type=='today') | (w.type=='avg')] # average length and today's length
w1 = w.loc[(w.type=='10pc') | (w.type=='90pc')] # 10th and 90th percentile

fig, ax = plt.subplots(figsize=(8,5))
y = sns.barplot(x=w0['species'], y=w0['length'], hue=w0['type'], yerr=w1['10pc','90pc'], capsize=.2) 
y.set_title(w0.index[0].strftime('%d %b %Y'), fontsize=16)
y.set_xlabel('species', fontsize=14)
y.set_ylabel('length (cm)', fontsize=14)
y.grid(axis='y', lw=0.5)

plt.show()

where today is today's length measurement, avg is the mean measurement, and 10pc and 90pc are the 10th and 90th percentile values. I tried setting yerr in the barplot command, but this doesn't work. I'm not sure how to configure seaborn to accept the percentile values.

I want to plot 10pc and 90pc for the avg bars for each species. This is what I'm aiming for (black bars drawn in myself):

enter image description here

CodePudding user response:

This is my take, using pandas' plotting functionality, based on enter image description here

  • Related