Question

I have slightly modified the original sample from matplotlib (finance_work2.py) to plot a Candlestick graph instead of a Line graph. What I'd like to know is how can I remove the gap between friday and monday candles ?

Any help will be appreciated.

This is what I've done so far:

import datetime
import numpy as np
import matplotlib.finance as finance
import matplotlib.dates as mdates
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
from matplotlib.finance import candlestick
import matplotlib.font_manager as font_manager
from matplotlib.dates import date2num


startdate = datetime.date(2013,5,1)
today = enddate = datetime.date.today()
ticker = 'SPY'

fh = finance.fetch_historical_yahoo(ticker, startdate, enddate)
# a numpy record array with fields: date, open, high, low, close, volume, adj_close)

r = mlab.csv2rec(fh); fh.close()
r.sort()


### plot graph divisions
plt.rc('axes', grid=True)
plt.rc('grid', color='0.75', linestyle='-', linewidth=0.5)
textsize = 9
left, width = 0.1, 0.8
rect1 = [left, 0.3, width, 0.4]
fig = plt.figure(facecolor='white')
axescolor  = '#f6f6f6'
ax1 = fig.add_axes(rect1, axisbg=axescolor)


### plot the price
prices = r.adj_close
dx = r.adj_close - r.close
low = r.low + dx
high = r.high + dx
deltas = np.zeros_like(prices)
deltas[1:] = np.diff(prices)
up = deltas>0

candlesticks = zip(date2num(r['date']),r['open'],r['close'],r['high'],r['low'],r['volume'])
candlestick(ax1, candlesticks,width=1,colorup='g', colordown='r')

plt.show()

i've tried with this modification but I get an error I don't understand:

weekday_quotes = [tuple([i]+list(quote[1:])) for i,quote in enumerate(candlesticks)]
candlestick(ax2, weekday_quotes,width=1,colorup='g', colordown='r')

There is another question on this site regarding the same problem, but the code is NOT based on numpy arrays and I suspect that's why the workaround is not working.

Thanks in advance.

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top