Boxplotting多个蒙版数组

问题描述 投票:0回答:2

我试图在我的数据集中绘制几个掩盖了所有-9999值(设备故障值)的数组。似乎np.boxplot忽略了值的掩码并且无论如何都要绘制它们。如何在每个数据集中排除掩码值?

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()

boxplot of masked -9999 data

python numpy matplotlib boxplot
2个回答
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]

0
投票

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

解决matplotlib.boxpl解决方案无法识别屏蔽值。

© www.soinside.com 2019 - 2024. All rights reserved.