Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 04937b5

Browse files
committed
ENH: allow for custom patch artist and labels to the boxplots
1 parent b429dcf commit 04937b5

1 file changed

Lines changed: 40 additions & 8 deletions

File tree

lib/matplotlib/axes/_axes.py

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

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

29412965
if flierprops is None:
29422966
flierprops = dict(linestyle='none', marker='+',
@@ -3011,6 +3035,9 @@ def dopatch(xs, ys, **kwargs):
30113035
holdStatus = self._hold
30123036

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

30633090
# draw the whiskers
30643091
whiskers.extend(doplot(
3065-
whisker_x, whiskerlo_y, **boxprops
3092+
whisker_x, whiskerlo_y, **otherprops
30663093
))
30673094
whiskers.extend(doplot(
3068-
whisker_x, whiskerhi_y, **boxprops
3095+
whisker_x, whiskerhi_y, **otherprops
30693096
))
30703097

30713098
# maybe draw the caps:
30723099
if showcaps:
30733100
caps.extend(doplot(
3074-
cap_x, cap_lo, **boxprops
3101+
cap_x, cap_lo, **otherprops
30753102
))
30763103
caps.extend(doplot(
3077-
cap_x, cap_hi, **boxprops
3104+
cap_x, cap_hi, **otherprops
30783105
))
30793106

30803107
# draw the medians
@@ -3102,19 +3129,24 @@ def dopatch(xs, ys, **kwargs):
31023129

31033130
# fix our axes/ticks up a little
31043131
if vert:
3105-
setticks, setlim = self.set_xticks, self.set_xlim
3132+
setticks = self.set_xticks
3133+
setlim = self.set_xlim
3134+
setlabels = self.set_xticklabels
31063135
else:
3107-
setticks, setlim = self.set_yticks, self.set_ylim
3136+
setticks = self.set_yticks
3137+
setlim = self.set_ylim
3138+
setlabels = self.set_yticklabels
31083139

31093140
newlimits = min(positions) - 0.5, max(positions) + 0.5
31103141
setlim(newlimits)
31113142
setticks(positions)
3143+
setlabels(datalabels)
31123144

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

31163148
return dict(whiskers=whiskers, caps=caps, boxes=boxes,
3117-
medians=medians, fliers=fliers)
3149+
medians=medians, fliers=fliers, means=means)
31183150

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

0 commit comments

Comments
 (0)