Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5774335

Browse files
committed
use urllib2 for yahoo finace; more cleanups to demo
svn path=/trunk/matplotlib/; revision=6993
1 parent fd88c28 commit 5774335

2 files changed

Lines changed: 9 additions & 7 deletions

File tree

examples/pylab_examples/finance_work2.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ def moving_average_convergence(x, nslow=26, nfast=12):
139139
ax1.plot(r.date, rsi, color=fillcolor)
140140
ax1.axhline(70, color=fillcolor)
141141
ax1.axhline(30, color=fillcolor)
142-
ax1.fill_between(r.date, rsi, 70, facecolor=fillcolor, where=(rsi>=70))
143-
ax1.fill_between(r.date, rsi, 30, facecolor=fillcolor, where=(rsi<=30))
142+
ax1.fill_between(r.date, rsi, 70, where=(rsi>=70), facecolor=fillcolor, edgecolor=fillcolor)
143+
ax1.fill_between(r.date, rsi, 30, where=(rsi<=30), facecolor=fillcolor, edgecolor=fillcolor)
144144
ax1.text(0.6, 0.9, '>70 = overbought', va='top', transform=ax1.transAxes, fontsize=textsize)
145145
ax1.text(0.6, 0.1, '<30 = oversold', transform=ax1.transAxes, fontsize=textsize)
146146
ax1.set_ylim(0, 100)
@@ -181,7 +181,7 @@ def moving_average_convergence(x, nslow=26, nfast=12):
181181

182182
volume = (r.close*r.volume)/1e6 # dollar volume in millions
183183
vmax = volume.max()
184-
poly = ax2t.fill_between(r.date, volume, 0, facecolor=fillcolor, label='Volume')
184+
poly = ax2t.fill_between(r.date, volume, 0, label='Volume', facecolor=fillcolor, edgecolor=fillcolor)
185185
ax2t.set_ylim(0, 5*vmax)
186186
ax2t.set_yticks([])
187187

@@ -195,14 +195,14 @@ def moving_average_convergence(x, nslow=26, nfast=12):
195195
ema9 = moving_average(macd, nema, type='exponential')
196196
ax3.plot(r.date, macd, color='black', lw=2)
197197
ax3.plot(r.date, ema9, color='blue', lw=1)
198-
ax3.fill_between(r.date, macd-ema9, 0, facecolor=fillcolor, alpha=0.5)
198+
ax3.fill_between(r.date, macd-ema9, 0, alpha=0.5, facecolor=fillcolor, edgecolor=fillcolor)
199199

200200

201201
ax3.text(0.025, 0.95, 'MACD (%d, %d, %d)'%(nfast, nslow, nema), va='top',
202202
transform=ax3.transAxes, fontsize=textsize)
203203

204204
ax3.set_yticks([])
205-
# turn off tick labels, rorate them, etc
205+
# turn off upper axis tick labels, rotate the lower ones, etc
206206
for ax in ax1, ax2, ax2t, ax3:
207207
if ax!=ax3:
208208
for label in ax.get_xticklabels():

lib/matplotlib/finance.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"""
66
#from __future__ import division
77
import os, time, warnings
8-
from urllib import urlopen
8+
from urllib2 import urlopen
99

1010
try:
1111
from hashlib import md5
@@ -121,8 +121,10 @@ def fetch_historical_yahoo(ticker, date1, date2, cachename=None):
121121
verbose.report('Using cachefile %s for %s'%(cachename, ticker))
122122
else:
123123
if not os.path.isdir(cachedir): os.mkdir(cachedir)
124+
urlfh = urlopen(url)
125+
124126
fh = file(cachename, 'w')
125-
fh.write(urlopen(url).read())
127+
fh.write(urlfh.read())
126128
fh.close()
127129
verbose.report('Saved %s data to cache file %s'%(ticker, cachename))
128130
fh = file(cachename, 'r')

0 commit comments

Comments
 (0)