Python Forum
x-axis not displaying correctly
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
x-axis not displaying correctly
#1
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?

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')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sample labels from excel file in order to put them on x-axis and y-axis of a plot hobbyist 11 4,371 Sep-14-2021, 08:29 AM
Last Post: hobbyist
  Difference Between Figure Axis and Sub Plot Axis in MatplotLib JoeDainton123 2 2,472 Aug-21-2020, 10:17 PM
Last Post: JoeDainton123

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020