Skip to content

Commit 2ca9461

Browse files
authored
Merge pull request #14043 from dstansby/bar-errbar-zorder
Ensure errorbars are always drawn on top of bars in ax.bar
2 parents 2b8b189 + 22e6a92 commit 2ca9461

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2291,6 +2291,14 @@ def bar(self, x, height, width=0.8, bottom=None, *, align="center",
22912291
xerr = kwargs.pop('xerr', None)
22922292
yerr = kwargs.pop('yerr', None)
22932293
error_kw = kwargs.pop('error_kw', {})
2294+
ezorder = error_kw.pop('zorder', None)
2295+
if ezorder is None:
2296+
ezorder = kwargs.get('zorder', None)
2297+
if ezorder is not None:
2298+
# If using the bar zorder, increment slightly to make sure
2299+
# errorbars are drawn on top of bars
2300+
ezorder += 0.01
2301+
error_kw.setdefault('zorder', ezorder)
22942302
ecolor = kwargs.pop('ecolor', 'k')
22952303
capsize = kwargs.pop('capsize', rcParams["errorbar.capsize"])
22962304
error_kw.setdefault('ecolor', ecolor)

lib/matplotlib/tests/test_axes.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6314,3 +6314,18 @@ def test_hist_range_and_density():
63146314
range=(0, 1), density=True)
63156315
assert bins[0] == 0
63166316
assert bins[-1] == 1
6317+
6318+
6319+
def test_bar_errbar_zorder():
6320+
# Check that the zorder of errorbars is always greater than the bar they
6321+
# are plotted on
6322+
fig, ax = plt.subplots()
6323+
x = [1, 2, 3]
6324+
barcont = ax.bar(x=x, height=x, yerr=x, capsize=5, zorder=3)
6325+
6326+
data_line, caplines, barlinecols = barcont.errorbar.lines
6327+
for bar in barcont.patches:
6328+
for capline in caplines:
6329+
assert capline.zorder > bar.zorder
6330+
for barlinecol in barlinecols:
6331+
assert barlinecol.zorder > bar.zorder

0 commit comments

Comments
 (0)