Reputation: 4482
I have the following dataframe
import pandas as pd
df = pd.DataFrame({'reg' : ['A', 'B', 'C'],
'file': ['1', '1', '2'],
'val' : [1, 2, 3]})
I would like to create a barplot
using seaborn
, with faceting
by file
, color by reg
and also add the val
on top of each bar
I have tried this
import seaborn as sns
g = sns.FacetGrid(df, col='file', hue='reg', col_wrap=2)
g.map(sns.barplot, 'reg', 'val').add_legend()
g.savefig('test_so.png')
But, this doesn't do the coloring right
Any ideas?
Upvotes: 2
Views: 2716
Reputation: 153460
You need order
parameter, add unique
per @jdehesa mentions below:
g = sns.FacetGrid(df, col='file', hue='reg', col_wrap=2)
g.map(sns.barplot, 'reg', 'val', order=df.reg.unique()).add_legend()
Output:
g = sns.FacetGrid(df, col='file', hue='reg', col_wrap=2)
g.map(sns.barplot, 'reg', 'val', order=df.reg.unique()).add_legend()
for ax in g.axes:
for p in ax.patches:
ax.annotate("%.2f" % p.get_height(), (p.get_x() + p.get_width() / 2., p.get_height()),
ha='center', va='center', fontsize=11, color='black', xytext=(0, 5),
textcoords='offset points')
Upvotes: 3