简体   繁体   English

如何在matplotlib上具体分配Python中的X和Y轴?

[英]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.天数 (numDays) 内的股票价格被捕获在列表 stock_price_list 中。

I've created an array, monte_list, to store a bunch of different stock_price_lists.我创建了一个数组 monte_list 来存储一堆不同的 stock_price_list。 I want to graph all those stock_price_lists on the same graph.我想在同一张图上绘制所有这些 stock_price_lists。 So I've created the variable numSimulations, which is supposed to create numSimulations number of rows in monte_list.所以我创建了变量 numSimulations,它应该在 monte_list 中创建 numSimulations 行数。

As far as I can tell, monte_list works.据我所知,monte_list 有效。 It's an array with one column and numSimulations numbers of rows.它是一个包含一列和 numSimulations 行数的数组。 These rows are populated with stock_price_lists, which are themselves lists of stock price data.这些行填充有 stock_price_lists,它们本身就是股票价格数据列表。

stock_price_list works; stock_price_list 有效; I've graphed it multiple times.我已经多次绘制它。

I think that monte_list works too;我认为 monte_list 也有效; 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. X 轴表示 numSimulations。

The Y axis is graphing stock price. Y 轴绘制股票价格。

I WANT the X axis to graph numDays, NOT numSimulations, but I can't figure out how to change that.我想要 X 轴绘制 numDays,而不是 numSimulations,但我不知道如何改变它。

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.) (请注意,我希望使 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()

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.我为 plot monte_list 的各种元素创建了一个 for 循环。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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