From 59ee039abe9d4adee072cbe5b4bd909bc9f2415a Mon Sep 17 00:00:00 2001 From: tiy Date: Mon, 12 Dec 2022 16:29:07 -0500 Subject: [PATCH] Boxplot: Median line is now bound within the box for all linewidths --- lib/matplotlib/axes/_axes.py | 3 +++ lib/matplotlib/tests/test_axes.py | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index ecb6f5e2098b..5390d0578e8f 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -4119,6 +4119,9 @@ def bxp(self, bxpstats, positions=None, widths=None, vert=True, zdelta = 0.1 + if medianprops is None or medianprops.get('solid_capstyle', None) is None: + medianprops['solid_capstyle'] = 'butt' + def merge_kw_rc(subkey, explicit, zdelta=0, usemarker=True): d = {k.split('.')[-1]: v for k, v in mpl.rcParams.items() if k.startswith(f'boxplot.{subkey}props')} diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 5b39be8bccdf..67aa0dba9248 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -3367,6 +3367,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'