From 951e64c425e28b3e698da4cedba8949a3d765468 Mon Sep 17 00:00:00 2001 From: Werner Mandla Date: Thu, 23 Jun 2022 21:17:39 +0200 Subject: [PATCH] Added a default capstyle for the boxplot median line and changed the zdelta --- lib/matplotlib/axes/_axes.py | 8 +++++++- lib/matplotlib/tests/test_axes.py | 9 +++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 5fba8a054a18..0b45c2930450 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -3991,7 +3991,13 @@ def bxp(self, bxpstats, positions=None, widths=None, vert=True, if zorder is None: zorder = mlines.Line2D.zorder - zdelta = 0.1 + # Use 'butt' as the default capstyle to avoid a visual overlap of + # the median line and the boxplot + if medianprops.get('solid_capstyle') is None: + medianprops['solid_capstyle'] = 'butt' + + # put the mean and median line below the box for visual improvement + zdelta = -0.1 def merge_kw_rc(subkey, explicit, zdelta=0, usemarker=True): d = {k.split('.')[-1]: v for k, v in rcParams.items() diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 0796e2474fef..4041de403835 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -3223,6 +3223,15 @@ def test_boxplot_zorder(): assert ax.boxplot(x, zorder=10)['boxes'][0].get_zorder() == 10 +@check_figures_equal() +def test_boxplot_median_bound_by_box(fig_test, fig_ref): + data = np.arange(3) + medianprops_test = {"linewidth": 12} + medianprops_ref = {**medianprops_test, "solid_capstyle": "butt"} + fig_test.subplots().boxplot(data, medianprops=medianprops_test) + fig_ref.subplots().boxplot(data, medianprops=medianprops_ref) + + def test_boxplot_marker_behavior(): plt.rcParams['lines.marker'] = 's' plt.rcParams['boxplot.flierprops.marker'] = 'o'