Why did plt.hline() show an extended long X axis than number of dates in the data? – matplotlib

Issue

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

Solution

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

Answered By – alvas

Answer Checked By – Candace Johnson (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.