Annotate Bars With Values On Pandas (on Seaborn Factorplot Bar Plot)
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 I used part
Solution 1:
#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 axisdefannotateBars(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)
Solution 2:
This can now be plotted much more concisely
Axes.bar_label
automatically labels barsfor container in ax.containers: ax.bar_label(container)
Axes.legend
includesfontsize
andtitle_fontsize
paramsax.legend(fontsize=10, title='ACCURACY', title_fontsize=24)
Also note that
seaborn.factorplot
has been renamed toseaborn.catplot
Updated seaborn.catplot
colors = ['xkcd:windows blue', 'xkcd:orange red', 'xkcd:grey', 'xkcd:amber']
g = sns.catplot(x='Stages', y='Accuracy', hue='Dataset', data=df,
kind='bar', height=4, aspect=3, palette=colors, legend=False)
# auto-label barsfor container in g.ax.containers:
g.ax.bar_label(container, fmt='%.2f', padding=2, rotation=90)
# add legend with custom font sizes
ax.legend(bbox_to_anchor=(0, 1.2, 1, 0.102), loc=10, ncol=4, fontsize=10,
title='TOTAL ACCURACY AND PER STAGE-RANDOM FOREST', title_fontsize=24)
# redecorate
g.despine(right=False)
g.set_xlabels('')
g.set_ylabels('')
g.ax.set_yticklabels([])
Updated DataFrame.plot.bar
ax = df.pivot('Stages', 'Dataset', 'Accuracy').plot.bar(legend=False)
# auto-label barsfor container in ax.containers:
ax.bar_label(container, fmt='%.2f', padding=3, rotation=90, size='small')
# add legend with custom font sizes
ax.legend(bbox_to_anchor=(0, 1.1, 1, 0.102), loc=10, ncol=4, fontsize='small',
title='TOTAL ACCURACY AND PER STAGE-RANDOM FOREST', title_fontsize='xx-large')
# redecorate
sns.despine(right=False)
ax.set_yticklabels([])
plt.xticks(rotation=0)
Post a Comment for "Annotate Bars With Values On Pandas (on Seaborn Factorplot Bar Plot)"