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

Skip to content

Commit 614736d

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 1002913 commit 614736d

2 files changed

Lines changed: 47 additions & 9 deletions

File tree

lib/matplotlib/cbook.py

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

18341834

1835-
def boxplot_stats(X, whis=1.5, bootstrap=None):
1835+
def boxplot_stats(X, whis=1.5, bootstrap=None, labels=None):
18361836
'''
18371837
Returns list of dictionaries of staticists to be use to draw a series of
18381838
box and whisker plots. See the `Returns` section below to the required
@@ -1848,7 +1848,7 @@ def boxplot_stats(X, whis=1.5, bootstrap=None):
18481848
18491849
whis : float (default = 1.5)
18501850
Determines the reach of the whiskers past the first and third
1851-
quartiles (e.g., Q3 + whis*IQR). Beyone the whiskers, data are
1851+
quartiles (e.g., Q3 + whis*IQR). Beyond the whiskers, data are
18521852
considers outliers and are plotted as individual points. Set
18531853
this to an unreasonably high value to force the whiskers to
18541854
show the min and max data. (IQR = interquartile range, Q3-Q1)
@@ -1857,6 +1857,10 @@ def boxplot_stats(X, whis=1.5, bootstrap=None):
18571857
Number of times the confidence intervals around the median should
18581858
be bootstrapped (percentile method).
18591859
1860+
labels : sequence
1861+
Labels for each dataset. Length must be compatible with dimensions
1862+
of `X`
1863+
18601864
Returns
18611865
-------
18621866
bxpstats : A list of dictionaries containing the results for each column
@@ -1926,9 +1930,21 @@ def _compute_conf_interval(data, med, iqr, bootstrap):
19261930
X = [X]
19271931

19281932
ncols = len(X)
1929-
for ii, x in enumerate(X, start=0):
1933+
if labels is None:
1934+
labels = [None] * ncols
1935+
elif len(labels) != ncols:
1936+
raise ValueError("Dimensions of labels and X must be compatible")
1937+
1938+
for ii, (x, label) in enumerate(zip(X, labels), start=0):
1939+
# empty dict
19301940
stats = {}
19311941

1942+
# set the label
1943+
if label is not None:
1944+
stats['label'] = label
1945+
else:
1946+
stats['label'] = ii
1947+
19321948
# arithmetic mean
19331949
stats['mean'] = np.mean(x)
19341950

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)