【问题标题】:How to fix the max value of a stack bar chart's using matplotlib?如何使用 matplotlib 修复堆积条形图的最大值?
【发布时间】:2022-01-17 08:31:38
【问题描述】:

我正在尝试从 2 个列表创建堆栈条形图:

counts_start = [tensor(0.),
 tensor(0.),
 tensor(0.0050),
 tensor(0.0100),
 tensor(0.2833),
 tensor(0.8250),
 tensor(0.8917),
 tensor(1.),
 tensor(1.),
 tensor(1.)]

counts_end = [tensor(0.1350),
 tensor(0.1467),
 tensor(0.1517),
 tensor(0.2233),
 tensor(0.2350),
 tensor(0.2417),
 tensor(0.4100),
 tensor(0.4200),
 tensor(0.4483),
 tensor(0.4600)]

this 之后,我了解到我需要每个列表的bottom 是之前所有列表的总和,所以我得到了这个代码:

counts_start = [torch.tensor(0.),
    torch.tensor(0.),
    torch.tensor(0.0050),
    torch.tensor(0.0100),
    torch.tensor(0.2833),
    torch.tensor(0.8250),
    torch.tensor(0.8917),
    torch.tensor(1.),
    torch.tensor(1.),
    torch.tensor(1.)]
plt.bar(indices_end,counts_end, color='r')

counts_end = [torch.tensor(0.1350),
    torch.tensor(0.1467),
    torch.tensor(0.1517),
    torch.tensor(0.2233),
    torch.tensor(0.2350),
    torch.tensor(0.2417),
    torch.tensor(0.4100),
    torch.tensor(0.4200),
    torch.tensor(0.4483),
     torch.tensor(0.4600)]

indices_start = range(len(counts_start))
plt.bar(indices_start,counts_start, bottom=counts_end)
plt.show()

哪些输出

每个列表都有一个概率值,因此最大值为 1,最小值为 0,这是我需要条形图开始和结束但无法正常工作的地方。另请注意,第三列和第四列的顶部是蓝色,这使得蓝色看起来具有更高的价值(实际上并非如此)。

我还尝试用输出的plt.bar(indices_start,[a_i - b_i for a_i, b_i in zip(counts_start, counts_end)], bottom=counts_end) 替换第二个plt.bar

这几乎是正确的(最大值为 1),除了前几列的颜色错误(我认为这是因为负值)。

为了澄清,我希望 2 个列表之间的每一列的最大值是该列的最大值。如果counts_start 列表中有较高的值,则蓝色应在其值的顶部(例如 0.8917),而较小的值应为其值的最大值(例如 0.4100)。所以它看起来像一列,红色为 0-0.4100,蓝色为 0.4100-0.8917。但是,如果counts_end 列表中有更高的值(例如 0.1517),则该列的顶部应为红色,该列的顶部为 0.1517,蓝色应为对应的值(例如 0.0050) .它看起来像一列,蓝色为 0-0.0050,红色为 0.0050-0.1517。

【问题讨论】:

    标签: python matplotlib bar-chart


    【解决方案1】:

    如果我理解正确,您希望标准化您的列表值,以便 counts_start[i] + counts_end[i] == 1

    这可以通过以下方式完成:

    start = [0, 0, 0.005, 0.01, 0.2833, 0.825, 0.8917, 1, 1, 1]
    end = [0.135, 0.1467, 0.1517, 0.2233, 0.235, 0.2417, 0.41, 0.42, 0.4483, 0.46]
    
    new_start, new_end = [], []
    for x, y in zip(start, end):
        t = x + y
        new_start.append(x / t)
        new_end.append(y / t)
    
    n = len(start)
    
    plt.bar(range(n), new_end, color="r")
    plt.bar(range(n), new_start, bottom=new_end)
    plt.show()
    

    这会产生以下情节:

    编辑:好的,我相信您需要跟踪 4 个列表,如下所示:

    start = [0, 0, 0.005, 0.01, 0.2833, 0.825, 0.8917, 1, 1, 1]
    end = [0.135, 0.1467, 0.1517, 0.2233, 0.235, 0.2417, 0.41, 0.42, 0.4483, 0.46]
    
    lo_start, hi_start, lo_end, hi_end = [], [], [], []
    for x, y in zip(start, end):
        if x > y:
            a, b, c, d = 0, x - y, y, 0
        else:
            a, b, c, d = x, 0, 0, y - x
    
        lo_start.append(a)
        hi_start.append(b)
        lo_end.append(c)
        hi_end.append(d)
    
    n = len(start)
    offset = [max(x, y) for x, y in zip(lo_start, lo_end)]
    
    plt.bar(range(n), lo_start, color="tab:blue")
    plt.bar(range(n), lo_end, color="tab:red")
    plt.bar(range(n), hi_start, bottom=offset, color="tab:blue")
    plt.bar(range(n), hi_end, bottom=offset, color="tab:red")
    plt.show()
    

    产生:

    【讨论】:

    • 我澄清了我的问题。请查看我的编辑
    • 我编辑了我的答案。让我知道这是否是您正在寻找的。​​span>
    • 哦,是的,这正是我所需要的!谢谢
    猜你喜欢
    • 2017-11-02
    • 2018-01-17
    • 2019-11-28
    • 2021-11-17
    • 1970-01-01
    • 2021-02-27
    • 2018-12-17
    相关资源
    最近更新 更多