我试图对一些数组进行箱线绘图,这些数组掩盖了我的数据集中的所有-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()
答案 0 :(得分:0)
您的代码产生错误,因此很难确定这是否是您正在使用的代码。我不确定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]
答案 1 :(得分:0)
https://github.com/matplotlib/matplotlib/issues/13533
解决matplotlib.boxplots无法识别掩码值的解决方案。