在Python中绘图?

时间:2016-05-18 23:45:42

标签: python graph

我在Python中编写了一个程序,在给定某些参数的情况下计算数组的随机游走。

我认为"随机"功能正常,因为我得到输出" (我正在使用iPython)。但是,我无法在图形上绘制函数,因为float()参数必须是字符串或数字,而不是函数。然而,考虑到所讨论变量的递归性质,需要将其定义为程序运行的函数。

我该如何绘制图表?

1 个答案:

答案 0 :(得分:1)

这个怎么样?

randomnumbers = np.random.normal((mu*(price/10)), (v*(price/10)), days) #This variable generates a series of random numbers based on the normal distribution and in accordance with the return and volatility specified.
randomwalk    = [simAssetPrice[i-1] + randomnumbers[i] + k for i in range(1,days)]

plt.plot(randomwalk)
plt.ylabel('Price')
plt.show()

我必须使用你指定的一个随机数。

我已使用列表理解来代替您的randomwalk()功能。

对变量和函数使用相同的名称必然会搞砸。另外,既然你写了:

randomwalk
plt.plot(randomwalk)

你指的是函数的“指针”。您需要编写randomwalk(simAssetPrice, randomnumbers)才能调用该功能。

您可能想要查看函数在Python中的工作方式。