3

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?? No values at the top of the bar

surya rahul
  • 833
  • 2
  • 14
  • 27

2 Answers2

3

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')

catplot example

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
aamir23
  • 1,143
  • 15
  • 23
1

catplot returns a FacetGrid. This does not have a text method.

Two options:

A. Select an axes from the grid

  • if catplot produces multiple axes

    g = sns.catplot(...)
    g.axes[0].text(...)
    
  • if catplot produces a single axes

    g = sns.catplot(...)
    g.axes.text(...)
    

B. Use a barplot

ax = sns.barplot(...)
ax.text(...)
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • Does `g.axes[0].text(...)` will be called under a `enumerate` loop ?? – surya rahul Oct 28 '18 at 10:54
  • I tried `for index, row in df1.iterrows(): g.axes[0].text(row.name,row.first_name, round(row.duration,2), color='black', ha="center")` but this give a error `TypeError: 'AxesSubplot' object does not support indexing` – surya rahul Oct 28 '18 at 11:09
  • I tried option A and got an AttributeError: 'numpy.ndarray' object has no attribute 'text'. I have posted the solution that worked for me – aamir23 Nov 17 '20 at 18:19