Python堆积条形图不像Excel图表那样显示

时间:2018-03-27 02:51:24

标签: python matplotlib-widget

我正在尝试在Python中创建堆积条形图以尝试匹配Excel生成的堆积条形图,但我似乎无法使其与Excel的输出相匹配。 Python图表没有显示A数组中的值。excel chart python chart

import matplotlib.pyplot as plt

def graph2Peak(self):
    A = [-1478, -16411, 5986, 0]
    B = [14933, 29866, 7466, 14933]

    X = range(4)
    plt2 = plt

    plt.bar(X, A, color = 'b')
    plt.bar(X, B, color = 'r', bottom = A)

    plt.show()

1 个答案:

答案 0 :(得分:0)

您需要确保第二个栏不是从负值开始。enter image description here

import matplotlib.pyplot as plt


def graph2Peak():
    A = [-1478, -16411, 5986, 0]
    B = [14933, 29866, 7466, 14933]

    X = range(4)
    plt2 = plt

    plt.bar(X, A, color = 'b')
    plt.bar(X, B, color = 'r', bottom = [0 if l<0 else l for l in A])

    plt.show()

graph2Peak()