如何在matplotlib中绘制混合的+ ve和-ve值的100%堆叠条? [英] How to draw 100% stacked bars with mixed +ve and -ve values in matplotlib?

查看:257
本文介绍了如何在matplotlib中绘制混合的+ ve和-ve值的100%堆叠条?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些数据混合的正值和负值,每个变量的绝对值的总和= 100%
这里是一些示例数据:

I have some data with mixed positive and negative values that the sum of the absolute values of each variable = 100% Here are some sample data:

Out01 = [79.069,-1.602,5.067,-4.241,-5.433,-4.590]
Out02 = [50.348,13.944,-15.373,6.554,5.541,8.240]
Out03 = [-8.053,0.819,-9.741,2.814,22.475,56.098]
Out04 = [-17.350,33.710,-18.510,-0.842,3.050,26.537]
Out05 = [-20.169,37.583,-20.785,-2.041,1.728,17.695]

我根据需要在Microsoft Excel中的100%堆叠列图表如下:

现在我想通过matplotlib库在python中绘制类似的图表。

I drew them as desired in Microsoft Excel as follows by the "100% stacked columns" chart: Now I want to draw similar chart in python through matplotlib library.

我该怎么做?

推荐答案

最后我得到了nswer,
当我按照 matplotlib页面中的示例,它包括 bottom 关键字,它指定了上一个样本数据的上升。

Finally, I got the answer, When I followed the example in matplotlib page, it includes the bottom keyword which specifies the rise of each sample data over the previous one.

p2 = plt.bar(ind, womenMeans, width, color='y', bottom=menMeans, yerr=womenStd)

例如,如果我们要按照示例绘制男女数据,我们从男人开始得分20(系列G1),然后绘制女性,他们开始绘制25的价值,底部值为20.
为了扩展这个,如果我们添加了另一个类别,说儿童,得分为15,那么它应该用底部 = 20 + 25 = 45.等等。

For example, if we want to plot men and women data as in the example, we starts with men with score 20 (in series G1), then to draw women, they start plotting the value of 25 with bottom value of 20. To expand this, if we added another category, say children, with score 15, then it should be plotted with bottom = 20 + 25 = 45. etc.

使用负值,我们有一个问题,他们在正面的方向相反的方向增长。所以它应该从bottom = 0开始,然后独立地为正值或负值之和的最大值。
要了解一个例子,如果我们要绘制如下的系列:(20,25,-15,30,-10,-5,17,3,-28)
每个值的底部应该如下(0,20,0,45,-15,-25,75,92,-30)为什么?

With negative values, we have a problem, that they grew in the opposite direction of the positives ones. so it should start with bottom = 0, then with maximum value of the sum of either positive or negative values independently. To understand that with an example, if we want to plot a series like the following: (20, 25, -15, 30, -10, -5, 17, 3, -28) The bottoms of each value should be as follows (0, 20, 0, 45, -15, -25, 75, 92, -30) Why?

对于20我们只是启动绘图,所以没有底层需要。
对于25,我们需要将它提高20.
对于-15,它是第一个负值,因此它必须在没有底部值的轴下绘制,因此底部= 0
对于30,应该提高20 + 25 = 45
对于-10,它应该从以前的负值开始低于-15
对于下一个-5,应该开始低于-10 + -15 = -25
等等...

For the 20, we just initiate drawing, so, no bottom required. For the 25, we need to rise it by 20. For the -15, it is the first negative value, so it must be drawn below axis with no bottom value, so the bottom = 0 For the 30, it should be raised by 20 + 25 = 45 For the -10, it should start below the previous negative value which is -15 For the next -5, it should start below -10 + -15 = -25 And so on...

def bottoms_matrix(matrix):
    positives = []
    negatives = []
    for i, row_mat in enumerate(matrix):
        tmp_p = []
        tmp_n = []
        for j, cell in enumerate(row_mat):
            if cell >0:
                tmp_p.append(cell)
                tmp_n.append(0.)
            else:
                tmp_p.append(0.)
                tmp_n.append(cell)
        positives.append(tmp_p)
        negatives.append(tmp_n)

    # get cumulative sums
    positives = positives[:-1]
    negatives = negatives[:-1]
    positives.insert(0, [0.] * len (matrix[0]))
    negatives.insert(0, [0.] * len(matrix[0]))
    tmp = swap_matrix(positives)
    tmp = [list(np.cumsum(t)) for t in tmp]
    positives = swap_matrix(tmp)

    tmp = swap_matrix(negatives)
    tmp = [list(np.cumsum(t)) for t in tmp]
    negatives = swap_matrix(tmp)

    final_matrix =[]
    for i, row_mat in enumerate(matrix):
        tmp =[]
        for j, cell in enumerate(row_mat):
            tmp.append(positives[i][j] if cell > 0 else negatives[i][j])
        final_matrix.append(tmp)
    return final_matrix

具有数据和所有辅助功能的完整示例已上传在我的Git页面

A complete example with data and all auxiliary functions is uploaded on my Git page.

这篇关于如何在matplotlib中绘制混合的+ ve和-ve值的100%堆叠条?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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