Skip to content

Commit 9911795

Browse files
committed
add 'auto' state for boxplot patch_artist
1 parent b3d29fb commit 9911795

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

lib/matplotlib/axes/_axes.py

+15-9
Original file line numberDiff line numberDiff line change
@@ -3821,7 +3821,7 @@ def apply_mask(arrays, mask):
38213821
@_api.rename_parameter("3.9", "labels", "tick_labels")
38223822
def boxplot(self, x, notch=None, sym=None, vert=None,
38233823
orientation='vertical', whis=None, positions=None,
3824-
widths=None, patch_artist=None, bootstrap=None,
3824+
widths=None, patch_artist='auto', bootstrap=None,
38253825
usermedians=None, conf_intervals=None,
38263826
meanline=None, showmeans=None, showcaps=None,
38273827
showbox=None, showfliers=None, boxprops=None,
@@ -3947,9 +3947,12 @@ def boxplot(self, x, notch=None, sym=None, vert=None,
39473947
The widths of the boxes. The default is 0.5, or ``0.15*(distance
39483948
between extreme positions)``, if that is smaller.
39493949
3950-
patch_artist : bool, default: :rc:`boxplot.patchartist`
3951-
If `False` produces boxes with the Line2D artist. Otherwise,
3952-
boxes are drawn with Patch artists.
3950+
patch_artist : bool, default: 'auto'
3951+
If 'auto', boxes are drawn with the Patch artist if a parameter
3952+
that needs the Patch artist is passed, otherwise they are drawn
3953+
with the Line2D artist.
3954+
If `True` produces boxes with the Patch artist.
3955+
If `False` produces boxes with the Line2D artist.
39533956
39543957
tick_labels : list of str, optional
39553958
The tick labels of each boxplot.
@@ -4060,8 +4063,6 @@ def boxplot(self, x, notch=None, sym=None, vert=None,
40604063
labels=tick_labels, autorange=autorange)
40614064
if notch is None:
40624065
notch = mpl.rcParams['boxplot.notch']
4063-
if patch_artist is None:
4064-
patch_artist = mpl.rcParams['boxplot.patchartist']
40654066
if meanline is None:
40664067
meanline = mpl.rcParams['boxplot.meanline']
40674068
if showmeans is None:
@@ -4086,7 +4087,12 @@ def boxplot(self, x, notch=None, sym=None, vert=None,
40864087
if flierprops is None:
40874088
flierprops = {}
40884089

4089-
if patch_artist:
4090+
if patch_artist == 'auto':
4091+
patch_artist = mpl.rcParams['boxplot.patchartist']
4092+
require_patch = {'edgecolor', 'facecolor'}
4093+
if require_patch.intersection(boxprops):
4094+
patch_artist = True
4095+
if patch_artist is True:
40904096
boxprops['linestyle'] = 'solid' # Not consistent with bxp.
40914097
if 'color' in boxprops:
40924098
boxprops['edgecolor'] = boxprops.pop('color')
@@ -4347,7 +4353,7 @@ def merge_kw_rc(subkey, explicit, zdelta=0, usemarker=True):
43474353
else mpl.rcParams['patch.facecolor']),
43484354
'zorder': zorder,
43494355
**cbook.normalize_kwargs(boxprops, mpatches.PathPatch)
4350-
} if patch_artist else merge_kw_rc('box', boxprops, usemarker=False)
4356+
} if patch_artist is True else merge_kw_rc('box', boxprops, usemarker=False)
43514357
whisker_kw = merge_kw_rc('whisker', whiskerprops, usemarker=False)
43524358
cap_kw = merge_kw_rc('cap', capprops, usemarker=False)
43534359
flier_kw = merge_kw_rc('flier', flierprops)
@@ -4462,7 +4468,7 @@ def do_patch(xs, ys, **kwargs):
44624468

44634469
# maybe draw the box
44644470
if showbox:
4465-
do_box = do_patch if patch_artist else do_plot
4471+
do_box = do_patch if patch_artist is True else do_plot
44664472
boxes.append(do_box(box_x, box_y, **box_kw))
44674473
median_kw.setdefault('label', '_nolegend_')
44684474
# draw the whiskers

lib/matplotlib/tests/test_axes.py

+18
Original file line numberDiff line numberDiff line change
@@ -9153,3 +9153,21 @@ def test_boxplot_orientation(fig_test, fig_ref):
91539153

91549154
ax_test = fig_test.subplots()
91559155
ax_test.boxplot(all_data, orientation='horizontal')
9156+
9157+
9158+
def test_patch_artist_auto():
9159+
# Test patch_artist = 'auto'.
9160+
fig, axs = plt.subplots(nrows=1, ncols=3)
9161+
np.random.seed(19680801)
9162+
all_data = [np.random.normal(0, std, 100) for std in range(7, 10)]
9163+
# Default 'auto' state, should use Line2D artist.
9164+
bp0 = axs[0].boxplot(all_data)
9165+
assert all(isinstance(i, mpl.lines.Line2D) for i in bp0['boxes'])
9166+
# 'facecolor' in boxprops should turn on patch_artist.
9167+
bp1 = axs[1].boxplot(all_data, boxprops={'facecolor': 'r'})
9168+
assert all(i.get_facecolor() == (1.0, 0.0, 0.0, 1) for i in bp1['boxes'])
9169+
assert all(isinstance(i, mpl.patches.PathPatch) for i in bp1['boxes'])
9170+
# 'edgecolor' in boxprops should also turn on patch_artist.
9171+
bp2 = axs[2].boxplot(all_data, boxprops={'edgecolor': 'g'})
9172+
assert all(i.get_edgecolor() == (0.0, 0.5, 0.0, 1) for i in bp2['boxes'])
9173+
assert all(isinstance(i, mpl.patches.PathPatch) for i in bp2['boxes'])

0 commit comments

Comments
 (0)