In [1]:
##### HW 3, problem 2 #####
# Sean Lubner
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import AutoMinorLocator
%matplotlib inline
# import the data
google = np.loadtxt('hw_3_data/google_data.txt', skiprows=1)
ny = np.loadtxt('hw_3_data/ny_temps.txt', skiprows=1)
yahoo = np.loadtxt('hw_3_data/yahoo_data.txt', skiprows=1)
# Generate the initial plot (figure, axes, lines)
f1, ax1 = plt.subplots()
f1.set_size_inches(11.2,8.56) # size of figure
yahoo_line, = ax1.plot(yahoo[:,0], yahoo[:,1], c='purple', lw=2, label='Yahoo! Stock Value')
google_line, = ax1.plot(google[:,0], google[:,1], c='blue', lw=2, label='Google Stock Value')
ax2 = ax1.twinx() # double axis
ny_line, = ax2.plot(ny[:,0], ny[:,1], c='red', ls='--', lw=2, label='NY Mon. High Temp')
t = ax1.set_title('New York Temperature, Google, and Yahoo!', size=32, fontweight='bold', fontname='Times New Roman')
ax2.set_ylabel('Temperature ($^{\circ}$F)', size=20)
ax1.set_xlabel('Date (MJD)', size=18)
ax1.set_ylabel('Value (Dollars)', size=18)
t.set_y(1.02) # move title up a little
# Format axes, ticks & labels
ax1.axis([48800, 55600, -20, 780])
ax2.set_ylim(bottom=-150, top=100)
ax1.xaxis.tick_bottom() # remove ticks at top
plt.setp(ax1.get_xticklabels(), fontsize=14)
plt.setp(ax1.get_yticklabels(), fontsize=14)
plt.setp(ax2.get_yticklabels(), fontsize=14)
ax1.tick_params('both', width=2, length=10, which='major')
ax2.tick_params('both', width=2, length=10, which='major')
ax1.xaxis.set_minor_locator(AutoMinorLocator())
ax1.yaxis.set_minor_locator(AutoMinorLocator())
ax2.yaxis.set_minor_locator(AutoMinorLocator())
ax1.tick_params(which='minor', width=1, length=5)
ax2.tick_params(which='minor', width=1, length=5)
[i.set_linewidth(2) for i in ax1.spines.itervalues()]
# Render and format legend
ax1.lines.extend(ax2.lines) # add for legend
l = ax1.legend(bbox_to_anchor=(0.33, 0.6), prop={'size':14})
l.draw_frame(False) # no box
f1.tight_layout()
plt.savefig('stocks_matplotlib.png', dpi=150) # Save the figure
In [ ]: