2

I'm trying to replicate a plot example but ran into an issue with the x axis and date range. When the plt.hlines() is included, the range goes back to 1970. When removed, the date range is correct. What could be causing the issue?

import yfinance as yf
import matplotlib.pyplot as plt

AAPL = yf.download('AAPL', start = '2020-4-5', end = '2021-6-5',)

data = AAPL['Close']
mean = AAPL['Close'].mean()
std = AAPL['Close'].std()
min_value = min(data)
max_value = max(data)

plt.title("AAPL")
plt.ylim(min_value -20, max_value + 20)
plt.scatter(x=AAPL.index, y=AAPL['Close'])
plt.hlines(y=mean, xmin=0, xmax=len(data))  # If this line is Removed, the X axis works with Date Range.
plt.show()

Example of Issue, plt.hline included

Correct Date Range, but no hline

alvas
  • 115,346
  • 109
  • 446
  • 738
Data
  • 57
  • 1
  • 5
  • This [answer](https://stackoverflow.com/a/59040003/7758804) in the duplicate shows how to add a horizontal line on a datetime axis. – Trenton McKinney Jul 06 '22 at 04:49
  • The xaxis is a datetime axis, and 0 in min=0 corresponds to 1970. xmin and xmax in hlines, must be dates. So you can use xmin=AAPL.index.min() and xmax=AAPL.index.max(). – Trenton McKinney Jul 06 '22 at 04:57
  • The dataframe can be plotted directly with `AAPL.plot(y='Close', marker='.', figsize=(9, 6))`, and this type of continuous timeseries data should typically be a line plot, but you can add `marker=` if really needed. See [code and plot](https://i.stack.imgur.com/vne7b.png) – Trenton McKinney Jul 06 '22 at 05:35

1 Answers1

0

The issue is with:

plt.hlines(y=mean, xmin=0, xmax=len(data))  # If this line is Removed, the X axis works with Date Range.

Your data points has data between start = '2020-4-5', end = '2021-6-5'

But when you use the hlines (horizontal line), the xmin and xmax functions are the arguments not what you were assuming them to be.

https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.hlines.html

  • xmin, xmax refers to the respective beginning and end of each line. If scalars are provided, all lines will have same length.

When you set the xmax=len(data), you are asking for len(data) "units" to be shown on the x-axis.

When you remove that plt.hlines in your code snippet, you are essentially asking matplotlib to automatically determine the x-axis range, that is why it worked.

Perhaps what you are looking for is to specify the date-range, e.g.

plt.xlim([datetime.date(2020, 4, 5), datetime.date(2021, 6, 5)])

Full example:

import datetime
import yfinance as yf
import matplotlib.pyplot as plt

AAPL = yf.download('AAPL', start = '2020-4-5', end = '2021-6-5',)

data = AAPL['Close']
mean = AAPL['Close'].mean()
std = AAPL['Close'].std()
min_value = min(data)
max_value = max(data)

plt.title("AAPL")
plt.ylim(min_value -20, max_value + 20)
plt.xlim([datetime.date(2020, 4, 5), datetime.date(2021, 6, 5)])
plt.scatter(x=AAPL.index, y=AAPL['Close'])
plt.show()

enter image description here

alvas
  • 115,346
  • 109
  • 446
  • 738