我试图在for循环中绘制一些水平条形图。对于我的每个栏,我想在顶部添加一个标签来显示计数(确切地说是x_labels是什么)
import matplotlib.pyplot as plt
for i in my_list:
sns.set_palette("husl")
ax = sns.barplot(x=data.sort_values(ascending = False), y=data.sort_values(ascending = False).index)
ax.set_xlabel('number of clicks')
# for each of the bars, try to get a label
rects = ax.patches
for rect in rects:
y_value = rect.get_x() + rect.get_width() / 2
x_value = rect.get_height()
label = "{:.1f}".format(x_value)
plt.annotate(
label,
(x_value, y_value))
plt.show()
我得到了所有的条形图,但标签不合适并且计算不正确:
我正在引用此stackoverflow帖子:Adding value labels on a matplotlib bar chart