Skip to content

Commit 2266234

Browse files
authored
Merge pull request matplotlib#27605 from dstansby/masked-boxplot
Ignore masked values in boxplot
2 parents 843f585 + f8b3b27 commit 2266234

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Boxplots now ignore masked data points
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
`~matplotlib.axes.Axes.boxplot` and `~matplotlib.cbook.boxplot_stats` now ignore
4+
any masked points in the input data.

lib/matplotlib/cbook.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1274,7 +1274,8 @@ def _compute_conf_interval(data, med, iqr, bootstrap):
12741274
continue
12751275

12761276
# up-convert to an array, just to be safe
1277-
x = np.asarray(x)
1277+
x = np.ma.asarray(x)
1278+
x = x.data[~x.mask].ravel()
12781279

12791280
# arithmetic mean
12801281
stats['mean'] = np.mean(x)

lib/matplotlib/tests/test_axes.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3388,6 +3388,20 @@ def test_boxplot():
33883388
ax.set_ylim((-30, 30))
33893389

33903390

3391+
@check_figures_equal(extensions=["png"])
3392+
def test_boxplot_masked(fig_test, fig_ref):
3393+
# Check that masked values are ignored when plotting a boxplot
3394+
x_orig = np.linspace(-1, 1, 200)
3395+
3396+
ax = fig_test.subplots()
3397+
x = x_orig[x_orig >= 0]
3398+
ax.boxplot(x)
3399+
3400+
x = np.ma.masked_less(x_orig, 0)
3401+
ax = fig_ref.subplots()
3402+
ax.boxplot(x)
3403+
3404+
33913405
@image_comparison(['boxplot_custom_capwidths.png'],
33923406
savefig_kwarg={'dpi': 40}, style='default')
33933407
def test_boxplot_custom_capwidths():

0 commit comments

Comments
 (0)