Home > front end >  Highlighting Date Range in matplotlib
Highlighting Date Range in matplotlib

Time:12-13

I am trying to highlight a section of the plot using this code:

import pandas as pd
from pandas import DataFrame as df
import matplotlib
from pandas_datareader import data as web
import matplotlib.pyplot as plt
import datetime
import warnings
warnings.filterwarnings("ignore")

from matplotlib import dates as mdates

start = datetime.date(2020,1,1)
end = datetime.date.today()

stock =  'fb'

data = web.DataReader(stock, 'yahoo', start, end)
data.index = pd.to_datetime(data.index, format ='%Y-%m-%d')
data = data[~data.index.duplicated(keep='first')]
data['year'] = data.index.year
data['month'] = data.index.month
data['week'] = data.index.week
data['day'] = data.index.day
data.set_index('year', append=True, inplace =True)
data.set_index('month',append=True,inplace=True)
data.set_index('week',append=True,inplace=True)
data.set_index('day',append=True,inplace=True)

fig, ax = plt.subplots(dpi=300, figsize =(15,4))
plt.plot(data.index.get_level_values('Date'), data['Close'])
plt.axvspan((datetime(2020,3,12)), (datetime(2020,6,1)), 
            label="Labeled",color="green", alpha=0.3)

The format is should be matplotlib.pyplot.axvspan(xmin, xmax, ymin=0, ymax=1, **kwargs) as per the documentation enter image description here enter image description here

  • Related