如何在Python中具体指定matplotlib的X轴和Y轴?

rjjhvcjd  于 2023-01-31  发布在  Python
关注(0)|答案(1)|浏览(142)

我正在尝试创建一个蒙特卡罗模拟来模拟股票的价格。
股票的价格每天都在变化,变化由一个随机变量决定,股票价格在天数(numDays)内被记录在一个列表stock_price_list中。
我创建了一个数组monte_list,用来存储一组不同的stock_price_lists,我想把所有的stock_price_lists绘制在同一个图上,所以我创建了变量numSimulations,它用来创建monte_list中的numSimulations行数。
据我所知,monte_list是一个只有一列和numSimulations的数组,这些行用stock_price_lists填充,它们本身就是股票价格数据的列表。
股票价格表工程;我画了很多次。
我认为monte_list也有效;至少,当我打印数组时,它返回的信息看起来是正确的。
我的问题是坐标轴画错了变量。
X轴表示数字模拟。
Y轴表示股票价格。
我想用X轴来绘制numDays,而不是numSimulations,但我不知道如何更改它。
我真的很想得到任何建议(注意,我希望numDays和numSimulations大得多,但希望使用较小的数字来掌握窍门)。

daily_mean = .06/250
daily_stdev = .2/(250**.5)
start_stock_price = 100

numDays = 7
numSimulations = 5
monte_arr = pd.DataFrame({'FirstCol': numSimulations}, index=[0])
monte_list = [None] * numSimulations #this is a test: I'm trying to createa list of numPrices Nones,\
    #then fill them all with stock_price_lists in the for loop


for j in range(0, numSimulations):
    stock_price_list = [start_stock_price]
    daily_stock_price = start_stock_price
        #add a col of stock price data
    for i in range (0,numDays):
        daily_ret = np.random.normal(daily_mean, daily_stdev, 1) # generates a random return
        daily_stock_price = daily_stock_price * (1+daily_ret)
        stock_price_list.append(float(daily_stock_price))
        np.array(stock_price_list)
        #arr = np.array(stock_price_list)
        #arr[j] = stock_price_list
    monte_list[j] = stock_price_list # somehow stock_price_list is over-writing cols
    
#I think monte_list generates numSimulations of stock_price_list entries.
#Problem: the axes are wrong. X axis should have numDays on it. Y should have prices
    # y axis is currently graphing highest stock price, but I want X to be graphing highest stock price
    # I want X axis to be numDays
plt.figure(figsize = (14,5))
plt.plot(monte_list)
plt.title("monte list")
plt.show()

块引号

kknvjkwl

kknvjkwl1#

事实上,在一个朋友的帮助下,我想出了如何编写这个代码。
我创建了一个for循环来绘制monte_list的各种元素。

import numpy as np
import pandas as pd
from pandas_datareader import data as wb
from scipy.stats import norm
import matplotlib.pyplot as plt
import statsmodels as sm
import math

daily_mean = .06/250
daily_stdev = .2/(250**.5)
start_stock_price = 100

#stock_price_list = [start_stock_price]
#daily_stock_price = start_stock_price
numDays = 250
numSimulations = 100
monte_arr = pd.DataFrame({'FirstCol': numSimulations}, index=[0])
monte_list = [None] * numSimulations #this is a test: I'm trying to createa list of numPrices Nones,\
    #then fill them all with stock_price_lists in the for loop


for j in range(0, numSimulations):
    stock_price_list = [start_stock_price]
    daily_stock_price = start_stock_price
        #add a col of stock price data
    for i in range (0,numDays):
        daily_ret = np.random.normal(daily_mean, daily_stdev, 1) # generates a random return
        daily_stock_price = daily_stock_price * (1+daily_ret)
        stock_price_list.append(float(daily_stock_price))
        np.array(stock_price_list)
    monte_list[j] = stock_price_list 
plt.figure(figsize = (14,5))
plt.title("Monte List")
plt.xlabel("Number of Days")
plt.ylabel("Stock price")
plt.legend()
for i in range(0, numDays):
    plt.plot(monte_list[i])
plt.show()

相关问题