Annotate bars with values on Pandas (on Seaborn factorplot bar plot)

前端 未结 1 1072
自闭症患者
自闭症患者 2020-12-03 15:51

I wrote some code to try and solve this question: https://stackoverflow.com/questions/39477748/how-to-annotate-bars-with-values-on-pandas-on-seaborn-factorplot-bar-plot

相关标签:
1条回答
  • 2020-12-03 16:57
        #Seaborn --factorplot
    
        colors = ["windows blue", "orange red", "grey", "amber"]  
        myPalette = sns.xkcd_palette(colors) #envío "colors" a la función xkcd_palette
    
        sns.set(style="white") #fondo blanco
        g = sns.factorplot(x="Stages", y="Accuracy", hue="Dataset", data=df, saturation=5, size=4, aspect=3, kind="bar",
                  palette= myPalette, legend=False) #se suprime la leyenda
    
        g.set(ylim=(0, 140)) 
        g.despine(right=False) 
        g.set_xlabels("") 
        g.set_ylabels("")  
        g.set_yticklabels("") 
    
    
       #Matplotlib --legend creation
    
         myLegend=plt.legend(bbox_to_anchor=(0., 1.2, 1., .102), prop ={'size':10}, loc=10, ncol=4,  #left, bottom, width, height
                    title=r'TOTAL ACCURACY AND PER STAGE-RANDOM FOREST')                    
         myLegend.get_title().set_fontsize('24')
    
    
    
         #Matplotlib --anotación de barras
    
           ax=g.ax #annotate axis = seaborn axis
           def annotateBars(row, ax=ax): 
           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='gray', rotation=90, xytext=(0, 20),
                     textcoords='offset points')  verticales
    
    
         plot = df.apply(annotateBars, ax=ax, axis=1)
    

    0 讨论(0)
提交回复
热议问题