8000 REF/TST: update boxplot_stats test with outliers changed to fliers · matplotlib/matplotlib@5cc858f · GitHub
[go: up one dir, main page]

Skip to content 8000

Commit 5cc858f

Browse files
committed
REF/TST: update boxplot_stats test with outliers changed to fliers
ENH/TST: boxplots stats now takes a labels kwarg MNT: minor PEP8 whitespace fix
1 parent f8e7215 commit 5cc858f

File tree

2 files changed

+47
-9
lines changed

2 files changed

+47
-9
lines changed

lib/matplotlib/cbook.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1846,7 +1846,7 @@ def delete_masked_points(*args):
18461846
return margs
18471847

18481848

1849-
def boxplot_stats(X, whis=1.5, bootstrap=None):
1849+
def boxplot_stats(X, whis=1.5, bootstrap=None, labels=None):
18501850
'''
18511851
Returns list of dictionaries of staticists to be use to draw a series of
18521852
box and whisker plots. See the `Returns` section below to the required
@@ -1862,7 +1862,7 @@ def boxplot_stats(X, whis=1.5, bootstrap=None):
18621862
18631863
whis : float (default = 1.5)
18641864
Determines the reach of the whiskers past the first and third
1865-
quartiles (e.g., Q3 + whis*IQR). Beyone the whiskers, data are
1865+
quartiles (e.g., Q3 + whis*IQR). Beyond the whiskers, data are
18661866
considers outliers and are plotted as individual points. Set
18671867
this to an unreasonably high value to force the whiskers to
18681868
show the min and max data. (IQR = interquartile range, Q3-Q1)
@@ -1871,6 +1871,10 @@ def boxplot_stats(X, whis=1.5, bootstrap=None):
18711871
Number of times the confidence intervals around the median should
18721872
be bootstrapped (percentile method).
18731873
1874+
labels : sequence
1875+
Labels for each dataset. Length must be compatible with dimensions
1876+
of `X`
1877+
18741878
Returns
18751879
-------
18761880
bxpstats : A list of dictionaries containing the results for each column
@@ -1940,9 +1944,21 @@ def _compute_conf_interval(data, med, iqr, bootstrap):
19401944
X = [X]
19411945

19421946
ncols = len(X)
1943-
for ii, x in enumerate(X, start=0):
1947+
if labels is None:
1948+
labels = [None] * ncols
1949+
elif len(labels) != ncols:
1950+
raise ValueError("Dimensions of labels and X must be compatible")
1951+
1952+
for ii, (x, label) in enumerate(zip(X, labels), start=0):
1953+
# empty dict
19441954
stats = {}
19451955

1956+
# set the label
1957+
if label is not None:
1958+
stats['label'] = label
1959+
else:
1960+
stats['label'] = ii
1961+
19461962
# arithmetic mean
19471963
stats['mean'] = np.mean(x)
19481964

lib/matplotlib/tests/test_cbook.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def setup(self):
105105
self.known_keys = sorted([
106106
'mean', 'med', 'q1', 'q3', 'iqr',
107107
'cilo', 'cihi', 'whislo', 'whishi',
108-
'outliers'
108+
'fliers', 'label'
109109
])
110110
self.std_results = cbook.boxplot_stats(self.data)
111111

@@ -115,13 +115,14 @@ def setup(self):
115115
'iqr': 13.492709959447094,
116116
'mean': 13.00447442387868,
117117
'med': 3.3335733967038079,
118-
'outliers': np.array([
118+
'fliers': np.array([
119119
92.55467075, 87.03819018, 42.23204914, 39.29390996
120120
]),
121121
'q1': 1.3597529879465153,
122122
'q3': 14.85246294739361,
123123
'whishi': 27.899688243699629,
124-
'whislo': 0.042143774965502923
124+
'whislo': 0.042143774965502923,
125+
'label': 0
125126
}
126127

127128
self.known_bootstrapped_ci = {
@@ -132,7 +133,11 @@ def setup(self):
132133
self.known_whis3_res = {
133134
'whishi': 42.232049135969874,
134135
'whislo': 0.042143774965502923,
135-
'outliers': np.array([92.55467075, 87.03819018]),
136+
'fliers': np.array([92.55467075, 87.03819018]),
137+
}
138+
139+
self.known_res_with_labels = {
140+
'label': 'Test1'
136141
}
137142

138143
def test_form_main_list(self):
@@ -151,7 +156,7 @@ def test_form_dict_keys(self):
151156
def test_results_baseline(self):
152157
res = self.std_results[0]
153158
for key in list(self.known_nonbootstrapped_res.keys()):
154-
if key != 'outliers':
159+
if key != 'fliers':
155160
assert_statement = assert_approx_equal
156161
else:
157162
assert_statement = assert_array_almost_equal
@@ -174,7 +179,7 @@ def test_results_whiskers(self):
174179
results = cbook.boxplot_stats(self.data, whis=3)
175180
res = results[0]
176181
for key in list(self.known_whis3_res.keys()):
177-
if key != 'outliers':
182+
if key != 'fliers':
178183
assert_statement = assert_approx_equal
179184
else:
180185
assert_statement = assert_array_almost_equal
@@ -183,3 +188,20 @@ def test_results_whiskers(self):
183188
res[key],
184189
self.known_whis3_res[key]
185190
)
191+
192+
def test_results_withlabels(self):
193+
labels = ['Test1', 2, 3, 4]
194+
results = cbook.boxplot_stats(self.data, labels=labels)
195+
res = results[0]
196+
for key in list(self.known_res_with_labels.keys()):
197+
assert_equal(res[key], self.known_res_with_labels[key])
198+
199+
@raises(ValueError)
200+
def test_label_error(self):
201+
labels = [1, 2]
202+
results = cbook.boxplot_stats(self.data, labels=labels)
203+
204+
@raises(ValueError)
205+
def test_bad_dims(self):
206+
data = np.random.normal(size=(34, 34, 34))
207+
results = cbook.boxplot_stats(data)

0 commit comments

Comments
 (0)
0