Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1556c7b

Browse files
authored
Merge pull request #14064 from meeseeksmachine/auto-backport-of-pr-14043-on-v3.1.x
Backport PR #14043 on branch v3.1.x (Ensure errorbars are always drawn on top of bars in ax.bar)
2 parents d75d2b0 + adf5f22 commit 1556c7b

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
@@ -2292,6 +2292,14 @@ def bar(self, x, height, width=0.8, bottom=None, *, align="center",
22922292
xerr = kwargs.pop('xerr', None)
22932293
yerr = kwargs.pop('yerr', None)
22942294
error_kw = kwargs.pop('error_kw', {})
2295+
ezorder = error_kw.pop('zorder', None)
2296+
if ezorder is None:
2297+
ezorder = kwargs.get('zorder', None)
2298+
if ezorder is not None:
2299+
# If using the bar zorder, increment slightly to make sure
2300+
# errorbars are drawn on top of bars
2301+
ezorder += 0.01
2302+
error_kw.setdefault('zorder', ezorder)
22952303
ecolor = kwargs.pop('ecolor', 'k')
22962304
capsize = kwargs.pop('capsize', rcParams["errorbar.capsize"])
22972305
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
@@ -6374,3 +6374,18 @@ def test_hist_range_and_density():
63746374
range=(0, 1), density=True)
63756375
assert bins[0] == 0
63766376
assert bins[-1] == 1
6377+
6378+
6379+
def test_bar_errbar_zorder():
6380+
# Check that the zorder of errorbars is always greater than the bar they
6381+
# are plotted on
6382+
fig, ax = plt.subplots()
6383+
x = [1, 2, 3]
6384+
barcont = ax.bar(x=x, height=x, yerr=x, capsize=5, zorder=3)
6385+
6386+
data_line, caplines, barlinecols = barcont.errorbar.lines
6387+
for bar in barcont.patches:
6388+
for capline in caplines:
6389+
assert capline.zorder > bar.zorder
6390+
for barlinecol in barlinecols:
6391+
assert barlinecol.zorder > bar.zorder

0 commit comments

Comments
 (0)