Skip to content

Commit 65a686c

Browse files
committed
ENH: allow for custom patch artist and labels to the boxplots
1 parent 91f88a2 commit 65a686c

File tree

1 file changed

+40
-8
lines changed

1 file changed

+40
-8
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2933,9 +2933,33 @@ def bxp(self, bxpstats, positions=None, widths=None, vert=True,
29332933
# lists of artists to be output
29342934
whiskers, caps, boxes, medians, means, fliers = [], [], [], [], [], []
29352935

2936+
# empty list of xticklabels
2937+
datalabels = []
2938+
2939+
# translates between line2D and patch linestyles
2940+
linestyle_map = {
2941+
'solid': '-',
2942+
'dashed': '--',
2943+
'dashdot': '-.',
2944+
'dotted': ':'
2945+
}
2946+
29362947
# plotting properties
29372948
if boxprops is None:
2938-
boxprops = dict(linestyle='-', color='black')
2949+
if patch_artist:
2950+
boxprops = dict(linestyle='solid', edgecolor='black',
2951+
facecolor='white')
2952+
else:
2953+
boxprops = dict(linestyle='-', color='black')
2954+
2955+
if patch_artist:
2956+
otherprops = dict(
2957+
linestyle=linestyle_map[boxprops['linestyle']],
2958+
color=boxprops['edgecolor']
2959+
)
2960+
else:
2961+
otherprops = dict(linestyle=boxprops['linestyle'],
2962+
color=boxprops['color'])
29392963

29402964
if flierprops is None:
29412965
flierprops = dict(linestyle='none', marker='+',
@@ -3010,6 +3034,9 @@ def dopatch(xs, ys, **kwargs):
30103034
holdStatus = self._hold
30113035

30123036
for pos, width, stats in zip(positions, widths, bxpstats):
3037+
# try to find a new label
3038+
datalabels.append(stats.get('label', pos))
3039+
30133040
# outliers coords
30143041
flier_x = np.ones(len(stats['outliers'])) * pos
30153042
flier_y = stats['outliers']
@@ -3061,19 +3088,19 @@ def dopatch(xs, ys, **kwargs):
30613088

30623089
# draw the whiskers
30633090
whiskers.extend(doplot(
3064-
whisker_x, whiskerlo_y, **boxprops
3091+
whisker_x, whiskerlo_y, **otherprops
30653092
))
30663093
whiskers.extend(doplot(
3067-
whisker_x, whiskerhi_y, **boxprops
3094+
whisker_x, whiskerhi_y, **otherprops
30683095
))
30693096

30703097
# maybe draw the caps:
30713098
if showcaps:
30723099
caps.extend(doplot(
3073-
cap_x, cap_lo, **boxprops
3100+
cap_x, cap_lo, **otherprops
30743101
))
30753102
caps.extend(doplot(
3076-
cap_x, cap_hi, **boxprops
3103+
cap_x, cap_hi, **otherprops
30773104
))
30783105

30793106
# draw the medians
@@ -3101,19 +3128,24 @@ def dopatch(xs, ys, **kwargs):
31013128

31023129
# fix our axes/ticks up a little
31033130
if vert:
3104-
setticks, setlim = self.set_xticks, self.set_xlim
3131+
setticks = self.set_xticks
3132+
setlim = self.set_xlim
3133+
setlabels = self.set_xticklabels
31053134
else:
3106-
setticks, setlim = self.set_yticks, self.set_ylim
3135+
setticks = self.set_yticks
3136+
setlim = self.set_ylim
3137+
setlabels = self.set_yticklabels
31073138

31083139
newlimits = min(positions) - 0.5, max(positions) + 0.5
31093140
setlim(newlimits)
31103141
setticks(positions)
3142+
setlabels(datalabels)
31113143

31123144
# reset hold status
31133145
self.hold(holdStatus)
31143146

31153147
return dict(whiskers=whiskers, caps=caps, boxes=boxes,
3116-
medians=medians, fliers=fliers)
3148+
medians=medians, fliers=fliers, means=means)
31173149

31183150
@docstring.dedent_interpd
31193151
def scatter(self, x, y, s=20, c='b', marker='o', cmap=None, norm=None,

0 commit comments

Comments
 (0)