盒图遮罩阵列 [英] Boxplotting Masked Arrays

查看:109
本文介绍了盒图遮罩阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何只用箱图绘制MaskedArray的非掩码值?我相信这将由boxplot(ma)自动发生,但这似乎可以对非掩码数组进行箱线绘图.

How can i boxplot only the non-masked values of a MaskedArray ? I tought this would happen automatically by boxplot(ma) but this seems to boxplot the non-masked array.

推荐答案

我认为您是正确的-如果发送已屏蔽的数组,plt.boxplot会忽略该屏蔽. 因此,您似乎不得不通过仅向boxplot发送未屏蔽的值来提供一些额外的帮助.由于数组的每一行可能具有不同数量的未屏蔽值,因此您将无法使用numpy数组.您必须形成Python的向量序列:

I think you are right -- plt.boxplot ignores the mask if sent a masked array. So it looks like you'll have to give boxplot some extra help by sending it only the values which are not masked. Since each row of the array may have a different number of unmasked values, you won't be able to use a numpy array. You'll have to form a Python sequence of vectors:

z = [[y for y in row if y] for row in x.T]

例如:

import matplotlib.pyplot as plt
import numpy as np

fig=plt.figure()

N=20
M=10

x = np.random.random((M,N))
mask=np.random.random_integers(0,1,N*M).reshape((M,N))
x = np.ma.array(x,mask=mask)
ax1=fig.add_subplot(2,1,1)
ax1.boxplot(x)

z = [[y for y in row if y] for row in x.T]
ax2=fig.add_subplot(2,1,2)
ax2.boxplot(z)
plt.show()

上面,第一个子图显示了x中所有数据的箱线图(忽略掩码),第二个子图仅显示了那些未被屏蔽的值的箱线图.

Above, the first subplot shows a boxplot of all the data in x (ignoring the mask), and the second subplot shows a boxplot of only those values which are not masked.

这篇关于盒图遮罩阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆