几天来,我一直在努力解决这个问题,但由于我的理解有限,一直没有成功。任何洞察力都将是非常值得欣赏的。
基本上,我有一个循环。当我绘制结果时,它会相互重叠。
然而,我想要的是将它们线性地绘制出来。就像这样。
我当前的代码如下所示。提前感谢您的帮助。
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()
发布于 2018-06-03 01:03:30
固定系列
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()
https://stackoverflow.com/questions/50658809
复制相似问题