代码之家  ›  专栏  ›  技术社区  ›  Kane Chew

是否有使用matplotlib自动生成累积面积图的功能?

  •  1
  • Kane Chew  · 技术社区  · 5 年前

    下面使用matplotlib库的代码为我们提供了一个简单的面积图

    import matplotlib.pyplot as plt
    
    x = range(1,6)
    y = [1,2,3,4,3] 
    plt.plot(x,y, labels=['A'])
    

    是否有一个函数可以生成累积面积图,而无需将y转换为:

    y_cumulative = [1,3,6,10,13] 
    

    谢谢

    1 回复  |  直到 5 年前
        1
  •  0
  •   Reblochon Masque    5 年前

    你可以使用 numpy.cumsum ,或者编写自己的普通python函数。

    def cumsum(seq):
        """returns a list with the cumulative sum of seq
        """
        result = []
        current = 0
        for elt in seq:
            current += elt
            result.append(current)
        return result