8000 Ensure errorbars are always drawn on top of bars in ax.bar by dstansby · Pull Request #14043 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Ensure errorbars are always drawn on top of bars in ax.bar #14043

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2291,6 +2291,14 @@ def bar(self, x, height, width=0.8, bottom=None, *, align="center",
xerr = kwargs.pop('xerr', None)
yerr = kwargs.pop('yerr', None)
error_kw = kwargs.pop('error_kw', {})
ezorder = error_kw.pop('zorder', None)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need this pop if we are going to use setdefault.

I think the question is do we want to .bar(..., error_kw={'zorder': None}) to mean "please do not adjust the zorder and fall through the current behavior" or do we want it to me "do the new default thing and adjust the zorder".

I think we want the second here, but see arguments either way.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 for the second.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think for the second option (for which I am also 👍 ) the pop needs to stay?

if ezorder is None:
ezorder = kwargs.get('zorder', None)
if ezorder is not None:
# If using the bar zorder, increment slightly to make sure
# errorbars are drawn on top of bars
ezorder += 0.01
error_kw.setdefault('zorder', ezorder)
ecolor = kwargs.pop('ecolor', 'k')
capsize = kwargs.pop('capsize', rcParams["errorbar.capsize"])
error_kw.setdefault('ecolor', ecolor)
Expand Down
15 changes: 15 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6314,3 +6314,18 @@ def test_hist_range_and_density():
range=(0, 1), density=True)
assert bins[0] == 0
assert bins[-1] == 1


def test_bar_errbar_zorder():
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've checked that this test fails on master.

# Check that the zorder of errorbars is always greater than the bar they
# are plotted on
fig, ax = plt.subplots()
x = [1, 2, 3]
barcont = ax.bar(x=x, height=x, yerr=x, capsize=5, zorder=3)

data_line, caplines, barlinecols = barcont.errorbar.lines
for bar in barcont.patches:
for capline in caplines:
assert capline.zorder > bar.zorder
for barlinecol in barlinecols:
assert barlinecol.zorder > bar.zorder
0