【问题标题】:Is there a function to automatically generate cumulative area plot using matplotlib?是否有使用 matplotlib 自动生成累积面积图的功能?
【发布时间】:2020-10-18 06:11:21
【问题描述】:

以下使用 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] 

谢谢。

【问题讨论】:

  • 不,只需调用 numpy.cumsum

标签: python matplotlib area


【解决方案1】:

您可以使用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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-22
    • 2019-12-29
    • 1970-01-01
    • 1970-01-01
    • 2018-05-05
    • 2018-10-25
    • 2013-05-28
    相关资源
    最近更新 更多