Jan-03-2020, 04:08 PM
Please forgive the mess. I am still learning.
I am downloading financial data from yahoo and trying to plot it on a graph with a linear regression line. I have had to do some fancy footwork with the dates column and here is where I am stuck.I have defined variable 'x' as the time value but when I print the chart, the x axis shows as 0.5,1,1.5,2....
Any ideas why that is?
I am downloading financial data from yahoo and trying to plot it on a graph with a linear regression line. I have had to do some fancy footwork with the dates column and here is where I am stuck.I have defined variable 'x' as the time value but when I print the chart, the x axis shows as 0.5,1,1.5,2....
Any ideas why that is?
import yfinance as yf import pandas as pd import matplotlib.pyplot as plt import numpy as np from sklearn.linear_model import LinearRegression from datetime import datetime Ins_Name = "EURUSD=X" # Get the data for the stock Apple by specifying the stock ticker, start date, and end date df = yf.download(Ins_Name,'2019-09-01','2019-12-01') df=df.sort_values(by=['Date']) df = df.iloc[:, : 4] #select 1st 4 columns df.index = df.index.strftime('%d%m%Y') #format the index column and change the time format df.index = pd.to_numeric(df.index) #I have to convert this date from object to numeric x_reshaped = df.index.to_numpy().reshape(-1, 1) #Reshape so that we can do linear regression y_reshaped = df.iloc[:, 3].values.reshape(-1, 1) x = df.index.values y = df.Close linear_regressor = LinearRegression() # create object for the linearregression class linear_regressor.fit(x_reshaped,y_reshaped) Y_pred = linear_regressor.predict(x_reshaped) fig, ax = plt.subplots(figsize=(20,15)) ax = plt.subplot("211") print(df.index.values) ax.scatter(x,y,color='purple') ax.plot(x_reshaped, Y_pred, color='red') ax.set_ylabel('Price', fontsize=25) ax.set_xlabel('Interval', fontsize=25) ax.set_title('Mean reversion of ' + Ins_Name + ' Close Prices',fontsize= 45,color='white') ax.xaxis.label.set_color('white') ax.yaxis.label.set_color('white') ax.tick_params(axis='x', colors='White',labelsize=20,rotation=90) ax.tick_params(axis='y', colors='White', labelsize=20) plt.style.use('seaborn-paper')