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

Skip to content

Commit 6f6afc5

Browse files
committed
Merge pull request #3571 from tacaswell/bxp_empty
BUG : deal with empty list passed to boxplot
2 parents 2645afb + 7602e3d commit 6f6afc5

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

lib/matplotlib/cbook.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1896,7 +1896,7 @@ def boxplot_stats(X, whis=1.5, bootstrap=None, labels=None):
18961896
======== ===================================
18971897
label tick label for the boxplot
18981898
mean arithemetic mean value
1899-
median 50th percentile
1899+
med 50th percentile
19001900
q1 first quartile (25th percentile)
19011901
q3 third quartile (75th percentile)
19021902
cilo lower notch around the median
@@ -1962,13 +1962,34 @@ def _compute_conf_interval(data, med, iqr, bootstrap):
19621962

19631963
input_whis = whis
19641964
for ii, (x, label) in enumerate(zip(X, labels), start=0):
1965+
19651966
# empty dict
19661967
stats = {}
19671968
stats['label'] = label
19681969

19691970
# restore whis to the input values in case it got changed in the loop
19701971
whis = input_whis
19711972

1973+
# note tricksyness, append up here and then mutate below
1974+
bxpstats.append(stats)
1975+
1976+
# if empty, bail
1977+
if len(x) == 0:
1978+
stats['fliers'] = np.array([])
1979+
stats['mean'] = np.nan
1980+
stats['med'] = np.nan
1981+
stats['q1'] = np.nan
1982+
stats['q3'] = np.nan
1983+
stats['cilo'] = np.nan
1984+
stats['ciho'] = np.nan
1985+
stats['whislo'] = np.nan
1986+
stats['whishi'] = np.nan
1987+
stats['med'] = np.nan
1988+
continue
1989+
1990+
# up-convert to an array, just to be safe
1991+
x = np.asarray(x)
1992+
19721993
# arithmetic mean
19731994
stats['mean'] = np.mean(x)
19741995

@@ -2021,9 +2042,9 @@ def _compute_conf_interval(data, med, iqr, bootstrap):
20212042
np.compress(x > stats['whishi'], x)
20222043
])
20232044

2024-
# add in teh remaining stats and append to final output
2045+
# add in the remaining stats
20252046
stats['q1'], stats['med'], stats['q3'] = q1, med, q3
2026-
bxpstats.append(stats)
2047+
20272048

20282049
return bxpstats
20292050

0 commit comments

Comments
 (0)