Data is in below form:
first_name nick_name activity duration
Harish Escorts MC GUARD ASSEMBLY WITH CABINATE BRACKET 226
Harish Escorts COOLANT TANK AND SIDE DOORS, OPP DOORS 225
Narasaraj Escorts MC GUARD ASSEMBLY WITH CABINATE AND BRACKET MO 225
Narasaraj Escorts COOLANT TANK AND SIDE DOORS, OPP DOORS ASSEMBLY 150
PurushothamEscorts PNEUMATIC AND LUBRICATION ASSEMBLY 55
Shivu Escorts CABLE CARRIER AND AXIS MOTOR ASSEMBLY 123
Using seaborn I am doing a barplot:
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(style="ticks", color_codes=True)
df = pd.read_excel('VMC & HMC data (sep&oct-15).xlsx', index = False)
df1 = df1[[ "first_name" , "nick_name", "activity" , "duration"]]
g = sns.catplot(x= 'first_name', y = 'duration', hue = 'activity' , data = df1, kind = 'bar', dodge=False, palette="deep", ci = None)
plt.ylim(0,300)
plt.gcf().autofmt_xdate()
for index, row in df1.iterrows():
g.text(row.name,row.first_name,row.duration, color='black', ha="center")
It throws me error as :
AttributeError: 'FacetGrid' object has no attribute 'text'
How to add values of bar on top of the bar??
I tried option A and got an AttributeError: 'numpy.ndarray' object has no attribute 'text'.
Here's how I solved it: You can add values for each bar by modifying the returned Facet grid.
g = sns.catplot(x='class', y='survival rate',
hue='sex', data=df, kind='bar')
ax = g.facet_axis(0,0)
for p in ax.patches:
ax.text(p.get_x() + 0.015,
p.get_height() * 1.02,
'{0:.2f}'.format(p.get_height()),
color='black', rotation='horizontal', size='large')
Here's what the example data looks like, if you want to recreate my plot -
class sex survival rate
0 first men 0.914680
1 second men 0.300120
2 third men 0.118990
3 first women 0.667971
4 second women 0.329380
5 third women 0.189747
6 first children 0.660562
7 second children 0.882608
8 third children 0.121259
catplot
returns a FacetGrid. This does not have a text
method.
Two options:
if catplot
produces multiple axes
g = sns.catplot(...)
g.axes[0].text(...)
if catplot
produces a single axes
g = sns.catplot(...)
g.axes.text(...)
ax = sns.barplot(...)
ax.text(...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With