diff --git a/doc/users/whats_new/boxplot_zorder_kwarg.rst b/doc/users/whats_new/boxplot_zorder_kwarg.rst new file mode 100644 index 000000000000..bbb44aa59ee8 --- /dev/null +++ b/doc/users/whats_new/boxplot_zorder_kwarg.rst @@ -0,0 +1,11 @@ +Boxplot Zorder Keyword Argument +------------------------------- + +The ``zorder`` parameter now exists for :func:`boxplot`. This allows the zorder +of a boxplot to be set in the plotting function call. + +Example +``````` +:: + + boxplot(np.arange(10), zorder=10) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 0d87c5566829..603f43ff581b 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -3095,7 +3095,7 @@ def boxplot(self, x, notch=None, sym=None, vert=None, whis=None, showbox=None, showfliers=None, boxprops=None, labels=None, flierprops=None, medianprops=None, meanprops=None, capprops=None, whiskerprops=None, - manage_xticks=True, autorange=False): + manage_xticks=True, autorange=False, zorder=None): """ Make a box and whisker plot. @@ -3108,7 +3108,7 @@ def boxplot(self, x, notch=None, sym=None, vert=None, whis=None, showbox=True, showfliers=True, boxprops=None, labels=None, flierprops=None, medianprops=None, meanprops=None, capprops=None, whiskerprops=None, - manage_xticks=True, autorange=False): + manage_xticks=True, autorange=False, zorder=None): Make a box and whisker plot for each column of ``x`` or each vector in sequence ``x``. The box extends from the lower to @@ -3220,6 +3220,9 @@ def boxplot(self, x, notch=None, sym=None, vert=None, whis=None, ``shownotches`` is also True. Otherwise, means will be shown as points. + zorder : scalar, optional (None) + Sets the zorder of the boxplot. + Other Parameters ---------------- The following boolean options toggle the drawing of individual @@ -3390,7 +3393,7 @@ def _update_dict(dictionary, rc_name, properties): medianprops=medianprops, meanprops=meanprops, meanline=meanline, showfliers=showfliers, capprops=capprops, whiskerprops=whiskerprops, - manage_xticks=manage_xticks) + manage_xticks=manage_xticks, zorder=zorder) return artists def bxp(self, bxpstats, positions=None, widths=None, vert=True, @@ -3398,7 +3401,7 @@ def bxp(self, bxpstats, positions=None, widths=None, vert=True, showcaps=True, showbox=True, showfliers=True, boxprops=None, whiskerprops=None, flierprops=None, medianprops=None, capprops=None, meanprops=None, - meanline=False, manage_xticks=True): + meanline=False, manage_xticks=True, zorder=None): """ Drawing function for box and whisker plots. @@ -3409,7 +3412,7 @@ def bxp(self, bxpstats, positions=None, widths=None, vert=True, showcaps=True, showbox=True, showfliers=True, boxprops=None, whiskerprops=None, flierprops=None, medianprops=None, capprops=None, meanprops=None, - meanline=False, manage_xticks=True): + meanline=False, manage_xticks=True, zorder=None): Make a box and whisker plot for each column of *x* or each vector in sequence *x*. The box extends from the lower to @@ -3513,6 +3516,9 @@ def bxp(self, bxpstats, positions=None, widths=None, vert=True, manage_xticks : bool, default = True If the function should adjust the xlim and xtick locations. + zorder : scalar, default = None + The zorder of the resulting boxplot + Returns ------- result : dict @@ -3555,7 +3561,10 @@ def bxp(self, bxpstats, positions=None, widths=None, vert=True, # empty list of xticklabels datalabels = [] - zorder = mlines.Line2D.zorder + # Use default zorder if none specified + if zorder is None: + zorder = mlines.Line2D.zorder + zdelta = 0.1 # box properties if patch_artist: diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index c08d4689c57f..8390952e4384 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -2012,6 +2012,14 @@ def test_boxplot_bad_ci_1(): conf_intervals=[[1, 2]]) +@cleanup +def test_boxplot_zorder(): + x = np.arange(10) + fix, ax = plt.subplots() + assert ax.boxplot(x)['boxes'][0].get_zorder() == 2 + assert ax.boxplot(x, zorder=10)['boxes'][0].get_zorder() == 10 + + @cleanup def test_boxplot_bad_ci_2(): x = np.linspace(-7, 7, 140)