0

I would like to add labels to my sns histogram. I've used plt.text but it's colliding with the edges of the frame. How can I prevent it from doing that? Or - is there a better way to add labels?

Thanks.


plt.figure(figsize=(7, 4))
hist = sns.histplot(x='y', data=data, hue='y', palette=palette, legend=False)
for p in hist.patches:
    x = p.get_x() + p.get_width() / 2
    if p.get_height() != 0:
        txt = plt.text(x=x, y=p.get_height() + 1000, s=p.get_height(), ha='center')

enter image description here

Netanel
  • 103
  • 3
  • 1
    E.g. `hist.margins(y=0.2)` to increase the whitespace at the top. `sns.despine(hist)` to remove the upper and right spine. By the way, using `ax` in the name of the subplot returned by `sns.histplot` makes it easier to see how examples in tutorials and on SO can be used in your own situation. – JohanC Dec 27 '22 at 14:50

0 Answers0