seaborn icon indicating copy to clipboard operation
seaborn copied to clipboard

Error bars in barplot do not respect clip_on

Open mwshinn opened this issue 5 years ago • 6 comments

Hello! It seems that error bars do not have clearly defined clipping behavior within barplots.

Expected behavior: Setting clip_on=True should result in neither bars or error bars being clipped.

Actual behavior: When setting clip_on to be True, bars in the bar plots are (correctly) not clipped at plot borders. However, errorbars are clipped.

Platform: Seaborn: 0.9.0 (though also appears to be the case in 0.10.1), Linux (*buntu), Python: 3.6.8, Matplotlib: 3.0.2

Here is a MWE:

import pandas
import seaborn as sns
import matplotlib.pyplot as plt

mdf = pandas.DataFrame({"a": [5, 8, 9, 10, 1, 2, 3, 4], "b": [0, 0, 0, 0, 1, 1, 1, 1]})

sns.barplot(data=mdf, x="b", y="a", clip_on=False)
plt.gca().set_ylim(0, 7.5)
plt.tight_layout(rect=[0, 0, 1, .5])
plt.show()

Thanks!

mwshinn avatar Jun 18 '20 12:06 mwshinn

Expected behavior: Setting clip_on=True should result in neither bars or error bars being clipped.

It's reasonable to have this expectation, but it's not correct. The kwargs (as the docs say) are passed through to ax.bar only. But you could do e.g.

ax = sns.barplot(data=mdf, x="b", y="a", clip_on=False)
ax.set_ylim(0, 7.5)
plt.setp(ax.lines, clip_on=False)
plt.tight_layout(rect=[0, 0, 1, .5])

mwaskom avatar Jun 18 '20 12:06 mwaskom

Expected behavior: Setting clip_on=True should result in neither bars or error bars being clipped.

It's reasonable to have this expectation, but it's not correct. The kwargs (as the docs say) are passed through to ax.bar only. But you could do e.g.

ax = sns.barplot(data=mdf, x="b", y="a", clip_on=False)
ax.set_ylim(0, 7.5)
plt.setp(ax.lines, clip_on=False)
plt.tight_layout(rect=[0, 0, 1, .5])

Sorry not understanding, as clip_on is a kwarg for matplotlib.axes.Axes.bar() and you docs seem to explicitly imply that it would be passed to it.

hermidalc avatar Mar 02 '22 00:03 hermidalc

Perhaps you could quote the relevant part of the docs? Otherwise it’s impossible to fix…

mwaskom avatar Mar 02 '22 01:03 mwaskom

Perhaps you could quote the relevant part of the docs? Otherwise it’s impossible to fix…

kwargs: key, value mappings

Other keyword arguments are passed through to matplotlib.axes.Axes.bar().

hermidalc avatar Mar 02 '22 01:03 hermidalc

Sorry forgot to put link https://seaborn.pydata.org/generated/seaborn.barplot.html

hermidalc avatar Mar 02 '22 01:03 hermidalc

Yes, it is true that other kwargs are passed through to ax.bar. If the bar itself extended past the axis limits, it would be visible. But the error bars are drawn using ax.plot.

mwaskom avatar Mar 02 '22 01:03 mwaskom

In v0.13+, barplot has an err_kws that can be used here:

ax = sns.barplot(tips, x="day", y="total_bill", errorbar="sd", err_kws=dict(clip_on=False))
ax.set(ylim=(0, 25))

image

mwaskom avatar Aug 27 '23 12:08 mwaskom