8000 Draw unfilled hist()s with the zorder of lines. · matplotlib/matplotlib@abd3a25 · GitHub
[go: up one dir, main page]

Skip to content

Commit abd3a25

Browse files
committed
Draw unfilled hist()s with the zorder of lines.
Unfilled hist()s (`histtype="step"`) are technically drawn as Patches with `fill=False`, but they are effectively lines and should therefore be drawn above gridlines (like Line2D), not below (like Patch). Change their default zorder accordingly.
1 parent 3232f58 commit abd3a25

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

doc/api/api_changes_3.3/behaviour.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,8 @@ wx Timer interval
181181
~~~~~~~~~~~~~~~~~
182182
Setting the timer interval on a not-yet-started ``TimerWx`` won't start it
183183
anymore.
184+
185+
"step"-type histograms default to the zorder of `.Line2D`
186+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
187+
This ensures that they go above gridlines by default. The old ``zorder`` can
188+
be kept by passing it as a keyword argument to `.Axes.hist`.

lib/matplotlib/axes/_axes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6769,7 +6769,8 @@ def hist(self, x, bins=None, range=None, density=False, weights=None,
67696769
closed=True if fill else None,
67706770
facecolor=c,
67716771
edgecolor=None if fill else c,
6772-
fill=fill if fill else None))
6772+
fill=fill if fill else None,
6773+
zorder=None if fill else mlines.Line2D.zorder))
67736774
for patch_list in patches:
67746775
for patch in patch_list:
67756776
if orientation == 'vertical':

lib/matplotlib/tests/test_axes.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1903,6 +1903,18 @@ def test_hist_with_empty_input(data, expected_number_of_hists):
19031903
assert hists.shape[0] == expected_number_of_hists
19041904

19051905

1906+
@pytest.mark.parametrize("histtype, zorder",
1907+
[("bar", mpl.patches.Patch.zorder),
1908+
("step", mpl.lines.Line2D.zorder),
1909+
("stepfilled", mpl.patches.Patch.zorder)])
1910+
def test_hist_zorder(histtype, zorder):
1911+
ax = plt.figure().add_subplot()
1912+
ax.hist([1, 2], histtype=histtype)
1913+
assert ax.patches
1914+
for patch in ax.patches:
1915+
assert patch.get_zorder() == zorder
1916+
1917+
19061918
def contour_dat():
19071919
x = np.linspace(-3, 5, 150)
19081920
y = np.linspace(-3, 5, 120)

0 commit comments

Comments
 (0)
0