Home > OS >  How to annotate grouped bars with the value and text from a different column
How to annotate grouped bars with the value and text from a different column

Time:08-26

I am trying to get a bar plot in seaborn with vertical text alignment. The name text is aligned vertically but it maintains some gap between each bars. I need to align the name text in top of the bar vertically and also the col3 values inside the bar. Please help me out to solve this.

enter image description here

The code that I had tried is,

df= pd.DataFrame({'name':['Name1','Name2','Nmae3','Name4','Name2','Name1'],'Col1':[1,1,1,2,2,2],'col2':['a','b','c','a','b','c'],'col3':[22,33,4,11,3,43]})

    name  Col1 col2  col3
0  Name1     1    a    22
1  Name2     1    b    33
2  Nmae3     1    c     4
3  Name4     2    a    11
4  Name2     2    b     3
5  Name1     2    c    43

tm = sns.barplot(x="Col1", y="col3", hue="col2", data=df)

for index in range(len(df)):
    tm.text(index,df.col3.values[index] 0.2, str(df.name.values[index]),fontdict=dict(color='black', rotation=90,fontsize=10),horizontalalignment='left')

CodePudding user response:

  • The main issue is getting the correct 'name' value from the dataframe.
  • Use enter image description here

  • Related