首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >线性图的循环累加结果

线性图的循环累加结果
EN

Stack Overflow用户
提问于 2018-06-03 00:12:57
回答 1查看 29关注 0票数 0

几天来,我一直在努力解决这个问题,但由于我的理解有限,一直没有成功。任何洞察力都将是非常值得欣赏的。

基本上,我有一个循环。当我绘制结果时,它会相互重叠。

然而,我想要的是将它们线性地绘制出来。就像这样。

我当前的代码如下所示。提前感谢您的帮助。

代码语言:javascript
复制
import numpy as np
rate           = 0.07
saving         = 2000
year           = [1, 2, 4, 6]

for x in year:
    month          = np.arange(1,12*x+1,1)
    compound       = (1+rate/12)**month
    monthly_saving = saving*np.ones(np.size(month))
    monthly_growth = compound*monthly_saving
    total_growth = np.cumsum(monthly_growth)

    import itertools
    list2d = [total_growth]
    merged = list(itertools.chain(*list2d))

    import matplotlib.pyplot as plt
    plt.plot(np.arange(1,np.size(merged)+1)/12, merged)

plt.xlabel("year")
plt.ylabel("asset")
plt.show()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-03 01:03:30

固定系列

代码语言:javascript
复制
import numpy as np
#  import itertools
import matplotlib.pyplot as plt


rate           = 0.07
saving         = 2000
year           = [1, 2, 4, 6]

merged = []                                      # init merge
for x in year:
    month          = np.arange(1,12*x+1,1)
    compound       = (1+rate/12)**month
    monthly_saving = saving*np.ones(np.size(month))
    monthly_growth = compound*monthly_saving
    total_growth = np.cumsum(monthly_growth)

    list2d = list(total_growth)                  # convert tolist
    merged.extend(list2d)                        # extend list

plt.plot(np.arange(1,len(merged)+1)/12, merged)  # plot once

plt.xlabel("year")
plt.ylabel("asset")
plt.show()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50658809

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档