箱线图绘制多个掩码数组

Boxplotting multiple masked arrays

我正在尝试绘制几个数组的箱线图,这些数组屏蔽了我数据集中的所有 -9999 值(设备故障值)。似乎 np.boxplot 忽略了值的掩码并无论如何绘制它们。如何让 numpy 排除每个数据集中的屏蔽值?

import numpy as np
import matplotlib.pyplot as plt

a = np.ma.array([229.5,374.0,536.5,-9999,-9999,-9999,-9999,-9999,182.0,42.5,49.0])
b = np.ma.array([363.0,118.5,159.0,-9999,311.0,516.0,380.0,338.5,223.0,211.5,128.5])
c = np.ma.array([205.5,277.5,141.5,278.0,302.0,251.0,299.0,250.0,315.5,92.1,211.9])

a = np.ma.masked_less( a, -9000 )
b = np.ma.masked_less( b, -9000 )
c = np.ma.masked_less( c, -9000 )

data_to_plot = [ a , b, c ]
labels = [ 'a', 'c', 'c' ]

fig = plt.figure( 3, figsize = ( 8, 10 ) )
ax = fig.add_subplot( 111, frameon = False )
bp = ax.boxplot( data_to_plot, patch_artist = True, widths = .85, 
                labels = labels, vert = True)
plt.setp( bp[ 'boxes' ], color = 'black', alpha = .8, linewidth = 3 )
plt.setp( bp[ 'whiskers' ], color = 'black', linewidth = 3 )
plt.setp( bp[ 'fliers' ], markeredgecolor = 'black', alpha = 1, markersize = 30, marker = '.' )
plt.setp( bp[ 'medians' ], color = 'orange', linewidth = 3 )
plt.setp( bp[ 'means' ], color = 'black' )
plt.setp( bp[ 'caps' ], color = 'black', linewidth = 4 )

colors = ['r', 'b', 'g', ]
for b in ( bp ):
    for patch, color in zip( bp[ 'boxes' ], colors ):
        patch.set_facecolor( color )
plt.show()

您的代码会产生错误,因此很难判断这是否是您正在使用的代码。我不确定 matplotlib 如何处理屏蔽数组,因为我通常会创建一个只包含我需要的数据的新数组。您可以替换:

 a = np.ma.masked_less( a, -9000 )
 b = np.ma.masked_less( b, -9000 )
 c = np.ma.masked_less( c, -9000 )

 a = a[a>-9000]
 b = b[b>-9000]
 c = c[c>-9000]

https://github.com/matplotlib/matplotlib/issues/13533

解决 matplotlib.boxplots 无法识别屏蔽值的问题。