简体   繁体   中英

How to specifically assign X and Y Axis on matplotlib in Python?

I'm trying to create a Monte Carlo simulation to simulate the price of a stock.

Every day, the price of the stock changes. The change is determined by a random variable. The stock prices over the number of days (numDays) is captured in a list, stock_price_list.

I've created an array, monte_list, to store a bunch of different stock_price_lists. I want to graph all those stock_price_lists on the same graph. So I've created the variable numSimulations, which is supposed to create numSimulations number of rows in monte_list.

As far as I can tell, monte_list works. It's an array with one column and numSimulations numbers of rows. These rows are populated with stock_price_lists, which are themselves lists of stock price data.

stock_price_list works; I've graphed it multiple times.

I think that monte_list works too; at least, when I print the array, it returns information that looks correct.

My problem is that the axes are graphing the wrong variables.

The X axis is graphing numSimulations.

The Y axis is graphing stock price.

I WANT the X axis to graph numDays, NOT numSimulations, but I can't figure out how to change that.

I'd really love any advice. (Note that I hope to make numDays and numSimulations much bigger, but wanted to use smaller numbers to get the hang of things.)

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()

Blockquote

So, it actually turns out that I figured out how to code this with some help from a friend.

I created a for loop to plot various elements of 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM