Artificial Intelligence in Plain English

New AI, ML and Data Science articles every day. Follow to join our 3.5M+ monthly readers.

Follow publication

How to visualize time series data using Pandas?

--

Importing packages

>>> import pandas as pd
>>> import matplotlib.pyplot as plt
>>> %matplotlib inline

Importing data

>>> data = pd.read_csv('time_data.csv', index_col='Date', parse_dates=True)>>> data.head()

Plotting

>>> data.plot()
>>> data['Adj. Volume'].plot()
>>> data['Adj. Close'].plot()
>>> data['Adj. Close'].plot(xlim=['2000-04-01','2003-04-01'])
>>> data['Adj. Close'].plot(xlim=['2005-04-01','2007-04-01'],ylim=(10,40))
>>> import matplotlib.dates as dates
>>> indx = data.loc['2005-04-01':'2005-06-01'].index
>>> indx
DatetimeIndex(['2005-04-01', '2005-04-04', '2005-04-05', '2005-04-06',
'2005-04-07', '2005-04-08', '2005-04-11', '2005-04-12',
'2005-04-13', '2005-04-14', '2005-04-15', '2005-04-18',
'2005-04-19', '2005-04-20', '2005-04-21', '2005-04-22',
'2005-04-25', '2005-04-26', '2005-04-27', '2005-04-28',
'2005-04-29', '2005-05-02', '2005-05-03', '2005-05-04',
'2005-05-05', '2005-05-06', '2005-05-09', '2005-05-10',
'2005-05-11', '2005-05-12', '2005-05-13', '2005-05-16',
'2005-05-17', '2005-05-18', '2005-05-19', '2005-05-20',
'2005-05-23', '2005-05-24', '2005-05-25', '2005-05-26',
'2005-05-27', '2005-05-31', '2005-06-01'],
dtype='datetime64[ns]', name='Date', freq=None)
>>> value = data.loc['2005-04-01':'2005-06-01']['Adj. Close']
>>> value
Date
2005-04-01 21.415836
2005-04-04 21.408927
2005-04-05 21.560911
2005-04-06 21.754344
2005-04-07 21.740527
2005-04-08 21.512552
2005-04-11 21.277669
2005-04-12 21.346752
2005-04-13 21.567819
2005-04-14 21.250035
2005-04-15 20.932252
2005-04-18 20.794085
2005-04-19 20.849352
2005-04-20 20.683552
2005-04-21 20.573019
2005-04-22 20.766452
2005-04-25 20.918435
2005-04-26 20.725002
2005-04-27 20.835535
2005-04-28 20.455577
2005-04-29 20.248327
2005-05-02 20.427944
2005-05-03 20.586835
2005-05-04 20.814810
2005-05-05 20.683552
2005-05-06 20.296685
2005-05-09 20.807902
2005-05-10 20.794085
2005-05-11 20.621377
2005-05-12 20.573019
2005-05-13 20.483210
2005-05-16 20.642102
2005-05-17 20.704277
2005-05-18 21.125685
2005-05-19 21.395111
2005-05-20 21.374386
2005-05-23 21.636902
2005-05-24 21.326027
2005-05-25 21.339844
2005-05-26 21.747436
2005-05-27 21.595452
2005-05-31 21.374386
2005-06-01 21.429652
Name: Adj. Close, dtype: float64
>>> fig,ax = plt.subplots()
>>> ax.plot_date(indx,value)
>>> fig,ax = plt.subplots()
>>> ax.plot_date(indx,value,'-')
>>> ax.xaxis.grid(True)
>>> ax.yaxis.grid(True)
>>> fig.autofmt_xdate()
>>> fig,ax = plt.subplots()
>>> ax.plot_date(indx,value,'-')
# Major Locating
>>> ax.xaxis.set_major_locator(dates.MonthLocator())
# Major Formatting
>>> ax.xaxis.set_major_formatter(dates.DateFormatter('\n\n\n%b-%Y'))
# Minor Locating
>>> ax.xaxis.set_minor_locator(dates.WeekdayLocator(byweekday=0))
# Minor Formatting
>>> ax.xaxis.set_minor_formatter(dates.DateFormatter('%d'))
>>> fig.autofmt_xdate()

Refer to the notebook for code here.

Get the dataset here.

Books to refer to:

--

--

Published in Artificial Intelligence in Plain English

New AI, ML and Data Science articles every day. Follow to join our 3.5M+ monthly readers.

Written by Jayashree domala

Self-driven woman who wishes to deliver creative and engaging ideas and solutions in the field of technology.

No responses yet

Write a response