J Rock
J Rock

Reputation: 261

Matplotlib's axhline function with a datetime object

I have a plot that looks like this :

import pandas as pd
import pandas_datareader as web
import datetime as dt
from datetime import timedelta
import matplotlib.pyplot as plt

#get the data
start_date = pd.to_datetime('2019-11-1')
end_date = pd.datetime.today()
df = web.DataReader('^gspc', 'yahoo', start_date, end_date)
df = df['Adj Close']
#build the plot
fig, ax1 = plt.subplots()

ax1.plot(df)

#set the axhline
ax1.axhline(df.max(),xmin=0,xmax=1)

ax1.set_xlim(start_date,end_date + timedelta(30))
ax1.set_ylim(df.min() -200, df.max() +200)

I am trying to set the axhline so it starts on the day of the maximum value in the df. I am having issues because the index is a datetime object, and axhline needs an integer.

Here is what I've tried:

ax1.axhline(df.max(),xmin=df.idxmax(),xmax=1)

What is the most efficient way to set the xmin to the date of the max value in the df?

Thanks

Upvotes: 4

Views: 4019

Answers (1)

JohanC
JohanC

Reputation: 80279

axhline() uses a y position, and two x positions. Y is in data coordinates and x in axes coordinates (0 for the left margin, 1 for the right). But the desired starting position is only available in data coordinates. hlines() can work with these.

df.argmax() finds the position of the maximum. df.index[df.argmax()] or df.idxmax() gets the value of the index at that position.

import pandas as pd
import pandas_datareader as web
import datetime as dt
from datetime import timedelta
import matplotlib.pyplot as plt

start_date = pd.to_datetime('2019-11-1')
end_date = pd.datetime.today()
df = web.DataReader('^gspc', 'yahoo', start_date, end_date)
df = df['Adj Close']

fig, ax1 = plt.subplots()
ax1.plot(df)
ax1.hlines(df.max(), df.idxmax(), end_date + timedelta(30), color='crimson', ls=':')
ax1.set_xlim(start_date, end_date + timedelta(30))
ax1.set_ylim(df.min() - 200, df.max() + 200)
plt.show()

example plot

Upvotes: 5

Related Questions