Let's go through a few key points of creatng nice time visualizations!
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
# Optional for interactive
# %matplotlib notebook (watch video for full details)
mcdon = pd.read_csv('mcdonalds.csv',index_col='Date',parse_dates=True)
mcdon.head()
Not a good practice to plot all columns in one chart! The Y-axis "Adj Close" and "Adj Volume" are on totally different scale
mcdon.plot()
Instead you should plot them individually
mcdon['Adj. Close'].plot()
mcdon['Adj. Volume'].plot()
mcdon['Adj. Close'].plot(figsize=(12,8))
mcdon['Adj. Close'].plot(figsize=(12,8))
plt.ylabel('Close Price')
plt.xlabel('Overwrite Date Index')
plt.title('Mcdonalds')
mcdon['Adj. Close'].plot(figsize=(12,8),title='Pandas Title')
Use xlim
to specify a period for a closer look. Note the ylim
won't automatically adjust
mcdon['Adj. Close'].plot(xlim=['2007-01-01','2009-01-01'])
You need to specify ylim
as well
mcdon['Adj. Close'].plot(xlim=['2007-01-01','2009-01-01'],ylim=[0,50])
Use ls
and c
to change the line style and color respectively
mcdon['Adj. Close'].plot(xlim=['2007-01-01','2007-05-01'],ylim=[0,40],ls='--',c='r')
This is where you will need the power of matplotlib to do heavy lifting if you want some serious customization!
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as dates
mcdon['Adj. Close'].plot(xlim=['2007-01-01','2007-05-01'],ylim=[0,40])
To Grab the whole index
idx = mcdon.index
To Grab a portion of the index
Use loc[]
to grab a portion of index. Note we use :
for slicing
idx = mcdon.loc['2007-01-01':'2007-05-01'].index
Also grab a portion of stock data
stock = mcdon.loc['2007-01-01':'2007-05-01']['Adj. Close']
idx
stock
Use plot_date()
instead of plot()
. plot_date()
can handle date time index information
fig, ax = plt.subplots()
ax.plot_date(idx, stock,'-')
plt.tight_layout()
plt.show()
Note the overlapping label still shown after plt.tight_layout()
is called
fig, ax = plt.subplots()
ax.plot_date(idx, stock,'-')
fig.autofmt_xdate() # Auto fixes the overlap!
plt.tight_layout()
plt.show()
Use ax.xaxis.grid(True)
and yaxis.grid(True)
to add grids to x-axis and y-axis respectively
fig, ax = plt.subplots()
ax.plot_date(idx, stock,'-')
ax.yaxis.grid(True)
ax.xaxis.grid(True)
fig.autofmt_xdate() # Auto fixes the overlap!
plt.tight_layout()
plt.show()
This is a two step process. The first step is SELECT display location followed by FORMATTING
Use xaxis.set_major_locator()
to select, then use xaxis.set_major_formatter()
to format
Example of format string: %b-%Y
"Apr-2007", %B-%Y
"April-2007", %b\n%Y
fig, ax = plt.subplots()
ax.plot_date(idx, stock,'-')
# Grids
ax.yaxis.grid(True)
ax.xaxis.grid(True)
# Major Axis
ax.xaxis.set_major_locator(dates.MonthLocator())
ax.xaxis.set_major_formatter(dates.DateFormatter('%b\n%Y'))
fig.autofmt_xdate() # Auto fixes the overlap!
plt.tight_layout()
plt.show()
In the following example, we pass in the format string \n\n\n\n%Y--%B'
to dates.DateFormatter()
in order to add some space for inserting a minor axis
Same as major axis, we locate and format the minor axis by calling xaxis.set_minor_locator()
followed by xaxis.set_minor_formatter()
fig, ax = plt.subplots()
ax.plot_date(idx, stock,'-')
ax.xaxis.set_major_locator(dates.MonthLocator())
ax.xaxis.set_major_formatter(dates.DateFormatter('\n\n%Y--%B'))
ax.xaxis.set_minor_locator(dates.WeekdayLocator())
ax.xaxis.set_minor_formatter(dates.DateFormatter('%d'))
ax.yaxis.grid(True)
ax.xaxis.grid(True)
fig.autofmt_xdate() # Auto fixes the overlap!
plt.tight_layout()
plt.show()
The parameter byweekday=1
pass to dates.WeekdayLocator()
set the actual day of the week to display. '0' is Monday, '1' is Tuesday etc.
The format string %B-%d-%a
pass to dates.DateFormatter()
: %a: 'Tue', %d: day of month, %B: full month name
fig, ax = plt.subplots(figsize=(10,8))
ax.plot_date(idx, stock,'-')
ax.xaxis.set_major_locator(dates.WeekdayLocator(byweekday=1))
ax.xaxis.set_major_formatter(dates.DateFormatter('%B-%d-%a'))
ax.yaxis.grid(True)
ax.xaxis.grid(True)
fig.autofmt_xdate()
plt.tight_layout()
plt.show()
Kernel > Restart
%matplotlib inline
to %matplotlib notebook
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib notebook