使用Python中的numpy和scipy在matplotlib中制作binned boxplot

3 python plot numpy matplotlib scipy

我有一个包含数值对的二维数组,我想通过x值的不同区间制作一个y值的箱线图.即如果数组是:

my_array = array([[1, 40.5], [4.5, 60], ...]])
Run Code Online (Sandbox Code Playgroud)

然后我想将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)
Run Code Online (Sandbox Code Playgroud)

但是,这给了我在elts_to_bins中的值,范围从1到3.我认为我应该为这些箱子获得基于0的索引,而我只想要3个箱子.我假设这是由于在linspace和数字化中如何表示箱子的一些棘手问题.

实现这一目标的最简单方法是什么?我想要num_bins-许多等距离的箱子,第一个箱子包含数据的下半部分,上部箱子包含上半部分...即,我希望每个数据点落入一些箱子,这样我就可以制作一个箱形图.

谢谢.

Joe*_*ton 7

你得到了数组中最大值的第3个bin(我假设你有一个拼写错误,而max_x应该是"max(my_array [:,0])"而不是"max(my_array [:,1] ])").您可以通过向最后一个bin添加1(或任何正数)来避免这种情况.

另外,如果我正确理解你,你想要将另一个变量装入另一个变量,所以下面的例子显示了这一点.如果您正在使用重新排列(速度慢得多),matplotlib.mlab中还有几个函数(例如mlab.rec_groupby等)可以执行此类操作.

无论如何,最后,你可能会有类似这样的东西(用x中的值来区分x,假设x和y的长度相同)

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
Run Code Online (Sandbox Code Playgroud)

作为一个简单的例子:

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 ])]
Run Code Online (Sandbox Code Playgroud)

希望那有所帮助!


Eri*_*got 4

Numpy 有一个专用函数,可以按照您需要的方式创建直方图:

histogram(a, bins=10, range=None, normed=False, weights=None, new=None)
Run Code Online (Sandbox Code Playgroud)

你可以这样使用:

(hist_data, bin_edges) = histogram(my_array[:,0], weights=my_array[:,1])
Run Code Online (Sandbox Code Playgroud)

这里的关键点是使用参数weights:每个值a[i]都会weights[i]对直方图做出贡献。例子:

a = [0, 1]
weights = [10, 2]
Run Code Online (Sandbox Code Playgroud)

描述 x = 0 处的 10 个点和 x = 1 处的 2 个点。

您可以使用参数设置 bin 数量或 bin 限制bins(请参阅官方文档(有关更多详细信息,

然后可以使用以下内容绘制直方图:

bar(bin_edges[:-1], hist_data)
Run Code Online (Sandbox Code Playgroud)

如果只需要做直方图绘图,类似的hist()函数可以直接绘制直方图:

hist(my_array[:,0], weights=my_array[:,1])
Run Code Online (Sandbox Code Playgroud)