1

I would like to add text in the middle of single category/bar by ('Rank') instead of legend. Please advise if this is achieveable in python ?

enter image description here

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)
Geron
  • 85
  • 1
  • 7
  • Check `ax.annotate` or `ax.text`. Please provide a minimal reproducible example if you need help in achieving it. – mozway Dec 28 '21 at 22:09

2 Answers2

1

you can do it using :

ax.text()

check this site example which use it with for loop , just like your data .

https://www.tutorialspoint.com/how-to-write-text-above-the-bars-on-a-bar-plot-python-matplotlib

Mr Dream
  • 134
  • 1
  • 10
0

enter image description here

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
            )
Mr Dream
  • 134
  • 1
  • 10