Matplotlib Tutorial Part 10 - Handling Unix Time


In [1]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import datetime as dt
import urllib

Base


In [2]:
stock = "EBAY"

# Yahoo Finance API (Using 10 days instead of 10 Years)
stock_price_url = 'http://chartapi.finance.yahoo.com/instrument/1.0/' +stock+ '/chartdata;type=quote;range=10d/csv'

# Getting Data from Yahoo Finance API 
source_code = urllib.request.urlopen(stock_price_url).read().decode()

# Cleaning Data
stock_data = []    
split_source = source_code.split('\n')

for line in split_source:
    split_line = line.split(',')
    if len(split_line) == 6:
        if 'values' not in line and 'labels' not in line:
            stock_data.append(line)

Data Conversion


In [3]:
fig = plt.figure(figsize=(17,9))

# Unpacking Data
date, closep, highp, lowp, openp, volume = \
np.loadtxt(stock_data, delimiter=',', unpack=True)

# Date conversion
dateconv = np.vectorize(dt.datetime.fromtimestamp)
date = dateconv(date)

# Plotting
ax1 = plt.subplot2grid((1,1),(0,0))

ax1.plot_date(date, closep,"-")

# Rotate Labels
for label in ax1.xaxis.get_ticklabels():
    label.set_rotation(60)