在Python中使用numpy和scipy在matplotlib中制作分箱箱形图
我有一个二维数组,里面包含了一对对的数值,我想根据不同的x值区间来制作y值的箱线图。也就是说,如果数组是:
my_array = array([[1, 40.5], [4.5, 60], ...]])
那么我想把my_array[:, 0]进行分组,然后对于每个分组,制作一个对应的my_array[:, 1]值的箱线图。最后,我希望这个图能包含多个箱线图,数量和分组数一样。
我试过以下方法:
min_x = min(my_array[:, 0])
max_x = max(my_array[:, 1])
num_bins = 3
bins = linspace(min_x, max_x, num_bins)
elts_to_bins = digitize(my_array[:, 0], bins)
但是,这样得到的elts_to_bins的值范围是从1到3。我本以为应该得到从0开始的索引,而且我只想要3个分组。我猜这可能是因为linspace和digitize在表示分组时有些复杂。
有什么简单的方法可以实现这个吗?我想要num_bins个等间距的分组,第一个分组包含数据的下半部分,最后一个分组包含上半部分……也就是说,我希望每个数据点都能落入某个分组,这样我才能制作箱线图。
谢谢。
2 个回答
你在找数组中最大值的第三个区间(我猜你这里有个笔误,max_x 应该是 "max(my_array[:,0])",而不是 "max(my_array[:,1])")。你可以通过在最后一个区间加1(或者任何正数)来避免这个问题。
另外,如果我理解得没错,你是想用一个变量来分组另一个变量,所以我下面的例子就是这样做的。如果你在使用 recarrays(这会慢很多),在 matplotlib.mlab 中也有几个函数可以做到这一点,比如 mlab.rec_groupby 等等。
总之,最后你可能会得到这样的结果(假设 x 和 y 的长度是一样的,用 y 的值来分组 x)
def bin_by(x, y, nbins=30):
"""
Bin x by y.
Returns the binned "x" values and the left edges of the bins
"""
bins = np.linspace(y.min(), y.max(), nbins+1)
# To avoid extra bin for the max value
bins[-1] += 1
indicies = np.digitize(y, bins)
output = []
for i in xrange(1, len(bins)):
output.append(x[indicies==i])
# Just return the left edges of the bins
bins = bins[:-1]
return output, bins
这里有个简单的例子:
In [3]: x = np.random.random((100, 2))
In [4]: binned_values, bins = bin_by(x[:,0], x[:,1], 2)
In [5]: binned_values
Out[5]:
[array([ 0.59649575, 0.07082605, 0.7191498 , 0.4026375 , 0.06611863,
0.01473529, 0.45487203, 0.39942696, 0.02342408, 0.04669615,
0.58294003, 0.59510434, 0.76255006, 0.76685052, 0.26108928,
0.7640156 , 0.01771553, 0.38212975, 0.74417014, 0.38217517,
0.73909022, 0.21068663, 0.9103707 , 0.83556636, 0.34277006,
0.38007865, 0.18697416, 0.64370535, 0.68292336, 0.26142583,
0.50457354, 0.63071319, 0.87525221, 0.86509534, 0.96382375,
0.57556343, 0.55860405, 0.36392931, 0.93638048, 0.66889756,
0.46140831, 0.01675165, 0.15401495, 0.10813141, 0.03876953,
0.65967335, 0.86803192, 0.94835281, 0.44950182]),
array([ 0.9249993 , 0.02682873, 0.89439141, 0.26415792, 0.42771144,
0.12292614, 0.44790357, 0.64692616, 0.14871052, 0.55611472,
0.72340179, 0.55335053, 0.07967047, 0.95725514, 0.49737279,
0.99213794, 0.7604765 , 0.56719713, 0.77828727, 0.77046566,
0.15060196, 0.39199123, 0.78904624, 0.59974575, 0.6965413 ,
0.52664095, 0.28629324, 0.21838664, 0.47305751, 0.3544522 ,
0.57704906, 0.1023201 , 0.76861237, 0.88862359, 0.29310836,
0.22079126, 0.84966201, 0.9376939 , 0.95449215, 0.10856864,
0.86655289, 0.57835533, 0.32831162, 0.1673871 , 0.55742108,
0.02436965, 0.45261232, 0.31552715, 0.56666458, 0.24757898,
0.8674747 ])]
希望这能帮到你一点!
Numpy 有一个专门的函数,可以帮你按照需要创建直方图:
histogram(a, bins=10, range=None, normed=False, weights=None, new=None)
你可以这样使用它:
(hist_data, bin_edges) = histogram(my_array[:,0], weights=my_array[:,1])
这里的关键是使用 weights
参数:每个值 a[i]
会对直方图贡献 weights[i]
的值。举个例子:
a = [0, 1]
weights = [10, 2]
这表示在 x = 0 的地方有 10 个点,而在 x = 1 的地方有 2 个点。
你可以通过 bins
参数来设置箱子的数量或箱子的范围(想了解更多,可以查看官方文档)。
然后,你可以用类似下面的方式来绘制直方图:
bar(bin_edges[:-1], hist_data)
如果你只需要绘制一个直方图,类似的hist() 函数可以直接绘制直方图:
hist(my_array[:,0], weights=my_array[:,1])