mwaskom / seaborn

Statistical data visualization in Python
https://seaborn.pydata.org
BSD 3-Clause "New" or "Revised" License
12.53k stars 1.92k forks source link

Adding axis bar_label() to sns.barplot BarContainer with error bars doesn't put labels above error bars #2753

Closed hermidalc closed 2 years ago

hermidalc commented 2 years ago

In matplotlib when you call ax.bar_label(bar, labels=[...]) on a BarContainer which has error bars it will put the labels correctly above the error bars. But in seaborn it's putting it above the bars but not the error bars.

mwaskom commented 2 years ago

ax.bar_label is a matplotlib convenience function that makes certain assumptions about how plots are drawn. While it would be nice if it worked with seaborn plots, I don't consider it a bug in seaborn for it not to work — I can't control anything about the assumptions that matplotlib convenience functions make.

FWIW I find it strange to label a bar plot with text above the error bar because it looks like you are labeling the top of the error bar, but the text is conveying something else.

hermidalc commented 2 years ago

ax.bar_label is a matplotlib convenience function that makes certain assumptions about how plots are drawn. While it would be nice if it worked with seaborn plots, I don't consider it a bug in seaborn for it not to work — I can't control anything about the assumptions that matplotlib convenience functions make.

FWIW I find it strange to label a bar plot with text above the error bar because it looks like you are labeling the top of the error bar, but the text is conveying something else.

Why aren't you drawing the error bars in seaborn the same way that matplotlib is doing it? So that the BarContainer will have the error bar info the way matplotlib expects it and then the bar_label() function will just work.

In my field (life sciences) you commonly want to put a label above the error bar (e.g. a statistical significance), and the matplotlib devs designed it as such too so must be that many communities also expect it like this.

Do you know of a workaround still using seaborn? Like a way to move the labels up from where they are since I know the height of the error bars. I need to use the advanced bar plot functionality of seaborn because I want to also add a swarm plot on top and that is hard to do with matplotlib alone.

hermidalc commented 2 years ago

@mwaskom see examples here https://matplotlib.org/stable/gallery/lines_bars_and_markers/bar_label_demo.html

mwaskom commented 2 years ago

If you search on stackoverflow you will find approximately one thousand questions asking how to put labels over a bar plot.

hermidalc commented 2 years ago

If you search on stackoverflow you will find approximately one thousand questions asking how to put labels over a bar plot.

Thank you, for others who come here with the same problem, I found good examples below, where they used seaborn barplot and matplotlib annotate. Works great and you can put the labels as high or low as you want.

https://www.geeksforgeeks.org/how-to-annotate-bars-in-barplot-with-matplotlib-in-python/

jingxuanlim commented 8 months ago

Hi @hermidalc. I'm coming here with the same issue. Looks like seaborn doesn't consider this a bug or a feature worth implementing, which is fine, but I'm still hoping to find a workaround.

If you search on stackoverflow you will find approximately one thousand questions asking how to put labels over a bar plot.

Thank you, for others who come here with the same problem, I found good examples below, where they used seaborn barplot and matplotlib annotate. Works great and you can put the labels as high or low as you want.

https://www.geeksforgeeks.org/how-to-annotate-bars-in-barplot-with-matplotlib-in-python/

I don't see any of the examples in the link you provided having errorbars.

How did you do it in the end?