Displaying of values on barchart

本秂侑毒 提交于 2019-12-18 07:07:02

问题


I've found a couple of similar postings to this topic. But they wasn't helpful for me.

I'm relatively new to Python and Seaborn.

This is my Code:

import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline

x_axis = ["A", "B","C","D","E","F"]
y_axis = [78.5, 79.6, 81.6, 75.4, 78.3, 79.6]

plt.ylabel('Accuracy')
plt.title('Accuracy of Classifier')

g=sns.barplot(x_axis, y_axis, color="red")

I'm just trying to display the values from the y_axis on top of every bar.


回答1:


Loop through the patches and annotate the bars.

import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline

x_axis = ["A", "B","C","D","E","F"]
y_axis = [78.5, 79.6, 81.6, 75.4, 78.3, 79.6]

plt.ylabel('Accuracy')
plt.title('Accuracy of Classifier')

g=sns.barplot(x_axis, y_axis, color="red")
ax=g
#annotate axis = seaborn axis
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', xytext=(0, 20),
                 textcoords='offset points')
_ = g.set_ylim(0,120) #To make space for the annotations

Output:



来源:https://stackoverflow.com/questions/45946970/displaying-of-values-on-barchart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!