8000 boxplot default style dicts now updated with user input and better sh… · matplotlib/matplotlib@eb56c3b · GitHub
[go: up one dir, main page]

Skip to content

Commit eb56c3b

Browse files
committed
boxplot default style dicts now updated with user input and better shape checking for medians
1 parent d5edad9 commit eb56c3b

File tree

2 files changed

+56
-59
lines changed

2 files changed

+56
-59
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 48 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2937,7 +2937,7 @@ def boxplot(self, x, notch=False, sym='b+', vert=True, whis=1.5,
29372937

29382938
# replace medians if necessary:
29392939
if usermedians is not None:
2940-
if len(usermedians) != len(bxpstats):
2940+
if len(np.ravel(usermedians)) != len(bxpstats):
29412941
medmsg = 'usermedians length not compatible with x'
29422942
raise ValueError(medmsg)
29432943
else:
@@ -3100,38 +3100,47 @@ def bxp(self, bxpstats, positions=None, widths=None, vert=True,
31003100
'dotted': ':'
31013101
}
31023102

3103-
# plotting properties
3104-
if boxprops is None:
3105-
if patch_artist:
3106-
boxprops = dict(linestyle='solid', edgecolor='black',
3107-
facecolor='white', linewidth=1)
3108-
else:
3109-
boxprops = dict(linestyle='-', color='black', linewidth=1)
3103+
# box properties
3104+
if patch_artist:
3105+
final_boxprops = dict(linestyle='solid', edgecolor='black',
3106+
facecolor='white', linewidth=1)
3107+
else:
3108+
final_boxprops = dict(linestyle='-', color='black', linewidth=1)
3109+
3110+
if boxprops is not None:
3111+
final_boxprops.update(boxprops)
31103112

3113+
# other (cap, whisker) properties
31113114
if patch_artist:
31123115
otherprops = dict(
3113-
linestyle=linestyle_map[boxprops['linestyle']],
3114-
color=boxprops['edgecolor'],
3115-
linewidth=boxprops.get('linewidth', 1)
3116+
linestyle=linestyle_map[final_boxprops['linestyle']],
3117+
color=final_boxprops['edgecolor'],
3118+
linewidth=final_boxprops.get('linewidth', 1)
31163119
)
31173120
else:
3118-
otherprops = dict(linestyle=boxprops['linestyle'],
3119-
color=boxprops['color'],
3120-
linewidth=boxprops.get('linewidth', 1))
3121-
3122-
if flierprops is None:
3123-
flierprops = dict(linestyle='none', marker='+',
3124-
markeredgecolor='blue')
3125-
3126-
if medianprops is None:
3127-
medianprops = dict(linestyle='-', color='blue')
3128-
3129-
if meanprops is None:
3130-
if meanline:
3131-
meanprops = dict(linestyle='--', color='red')
3132-
else:
3133-
meanprops = dict(linestyle='none', markerfacecolor='red',
3134-
marker='s')
3121+
otherprops = dict(linestyle=final_boxprops['linestyle'],
3122+
color=final_boxprops['color'],
3123+
linewidth=final_boxprops.get('linewidth', 1))
3124+
3125+
# flier (outlier) properties
3126+
final_flierprops = dict(linestyle='none', marker='+',
3127+
markeredgecolor='blue')
3128+
if flierprops is not None:
3129+
final_flierprops.update(flierprops)
3130+
3131+
# median line properties
3132+
final_medianprops = dict(linestyle='-', color='blue')
3133+
if medianprops is not None:
3134+
final_medianprops.update(medianprops)
3135+
3136+
# mean (line or point) properties
3137+
if meanline:
3138+
final_meanprops = dict(linestyle='--', color='red')
3139+
else:
3140+
final_meanprops = dict(linestyle='none', markerfacecolor='red',
3141+
marker='s')
3142+
if final_meanprops is not None:
3143+
final_meanprops.update(meanprops)
31353144

31363145
def to_vc(xs, ys):
31373146
# convert arguments to verts and codes
@@ -3239,54 +3248,37 @@ def dopatch(xs, ys, **kwargs):
32393248
# maybe draw the box:
32403249
if showbox:
32413250
if patch_artist:
3242-
boxes.extend(dopatch(
3243-
box_x, box_y, **boxprops
3244-
))
3251+
boxes.extend(dopatch(box_x, box_y, **final_boxprops))
32453252
else:
3246-
boxes.extend(doplot(
3247-
box_x, box_y, **boxprops
3248-
))
3253+
boxes.extend(doplot(box_x, box_y, **final_boxprops))
32493254

32503255
# draw the whiskers
3251-
whiskers.extend(doplot(
3252-
whisker_x, whiskerlo_y, **otherprops
3253-
))
3254-
whiskers.extend(doplot(
3255-
whisker_x, whiskerhi_y, **otherprops
3256-
))
3256+
whiskers.extend(doplot(whisker_x, whiskerlo_y, **otherprops))
3257+
whiskers.extend(doplot(whisker_x, whiskerhi_y, **otherprops))
32573258

32583259
# maybe draw the caps:
32593260
if showcaps:
3260-
caps.extend(doplot(
3261-
cap_x, cap_lo, **otherprops
3262-
))
3263-
caps.extend(doplot(
3264-
cap_x, cap_hi, **otherprops
3265-
))
3261+
caps.extend(doplot(cap_x, cap_lo, **otherprops))
3262+
caps.extend(doplot(cap_x, cap_hi, **otherprops))
32663263

32673264
# draw the medians
3268-
medians.extend(doplot(
3269-
med_x, med_y, **medianprops
3270-
))
3265+
medians.extend(doplot(med_x, med_y, **final_medianprops))
32713266

32723267
# maybe draw the means
32733268
if showmeans:
32743269
if meanline:
32753270
means.extend(doplot(
3276-
[box_left, box_right],
3277-
[stats['mean'], stats['mean']],
3278-
**meanprops
3271+
[box_left, box_right], [stats['mean'], stats['mean']],
3272+
**final_meanprops
32793273
))
32803274
else:
32813275
means.extend(doplot(
3282-
[pos], [stats['mean']], **meanprops
3276+
[pos], [stats['mean']], **final_meanprops
32833277
))
32843278

32853279
# maybe draw the fliers
32863280
if showfliers:
3287-
fliers.extend(doplot(
3288-
flier_x, flier_y, **flierprops
3289-
))
3281+
fliers.extend(doplot(flier_x CB62 , flier_y, **final_flierprops))
32903282

32913283
# fix our axes/ticks up a little
32923284
if vert:

lib/matplotlib/tests/test_axes.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,12 +1372,18 @@ def test_boxplot_no_weird_whisker():
13721372
ax1.yaxis.grid(False, which='minor')
13731373
ax1.xaxis.grid(False)
13741374

1375-
def test_boxplot_bad_medians():
1375+
def test_boxplot_bad_medians_1():
13761376
x = np.linspace(-7, 7, 140)
13771377
x = np.hstack([-25, x, 25])
13781378
fig, ax = plt.subplots()
13791379
assert_raises(ValueError, ax.boxplot, x, usermedians=[1, 2])
13801380

1381+
def test_boxplot_bad_medians_1():
1382+
x = np.linspace(-7, 7, 140)
1383+
x = np.hstack([-25, x, 25])
1384+
fig, ax = plt.subplots()
1385+
assert_raises(ValueError, ax.boxplot, [x, x], usermedians=[[1, 2],[1, 2]])
1386+
13811387
def test_boxplot_bad_ci_1():
13821388
x = np.linspace(-7, 7, 140)
13831389
x = np.hstack([-25, x, 25])
@@ -1392,8 +1398,7 @@ def test_boxplot_bad_ci_2():
13921398
assert_raises(ValueError, ax.boxplot, [x, x],
13931399
conf_intervals=[[1, 2], [1]])
13941400

1395-
@image_comparison(baseline_images=['errorbar_basic',
1396-
'errorbar_mixed'])
1401+
@image_comparison(baseline_images=['errorbar_basic', 'errorbar_mixed'])
13971402
def test_errorbar():
13981403
x = np.arange(0.1, 4, 0.5)
13991404
y = np.exp(-x)

0 commit comments

Comments
 (0)
0