[英]Add Text/Annotations to stacked barplot Python
I would like to add text in the middle of single category/bar by ('Rank') instead of legend.我想通过('Rank')而不是图例在单个类别/栏的中间添加文本。 Please advise if this is achieveable in python?请告知这是否可以在 python 中实现?
My code below:我的代码如下:
ax = sns.histplot(data=dfnew2, multiple='stack', x = 'Purpose', weights = 'Credit amount', hue='Rank',shrink=0.8, legend = True)
ax.set(ylabel='Credit Amount', xlabel='Purpose')
ax.tick_params(axis="x", labelrotation=90)
you can do it using:你可以这样做:
ax.text()
check this site example which use it with for loop, just like your data.检查使用 for 循环的这个站点示例,就像您的数据一样。
https://www.tutorialspoint.com/how-to-write-text-above-the-bars-on-a-bar-plot-python-matplotlib https://www.tutorialspoint.com/how-to-write-text-above-the-bars-on-a-bar-plot-python-matplotlib
Since you didn't provide a code sample try to adjust this with your variables it will work as you asked:由于您没有提供代码示例,请尝试使用您的变量进行调整,它将按照您的要求工作:
x = np.arange(len(years)) # the label locations
width = 0.35 # the width of the bars
fig, ax = plt.subplots()
ax.set_ylabel('Population(in million)')
ax.set_title('Years')
ax.set_xticks(x)
ax.set_xticklabels(years)
pps = ax.bar(x, population, width, label='population')
for p in pps:
height = p.get_height()
ax.text(x=p.get_x() + p.get_width() / 2, y=height/2.5,
s="{}".format(height),
ha='center',
rotation=90
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.