Reputation: 543
I wanted to remove the "gaps" between the weekends when graphing stock data using matplotlib. I was able to do this and the result is the following graph:
Now, when I try to run the index formatter on the x-axis, all I get is a blank canvas. Can anyone figure out what I'm doing wrong here? If I comment out the FuncFormatter
line everything works again.
#Build parameters for candlestick graph. Adjust dates so that they are graphed equi-distant on chart
x = 0
y = len(date)
ind = np.arange(y)
candleAr = []
while x < y:
appendLine = ind[x], openp[x], closep[x], highp[x], lowp[x], volume[x]
candleAr.append(appendLine)
x+=1
def format_date(x, pos=None):
thisind = np.clip(int(x+0.5), 0, y-1)
return candleAr.date[thisind].strftime('%Y-%m-%d')
#Define plot area and candlestick chart
fig = plt.figure(facecolor='w')
ax1 = plt.subplot2grid((1,1), (0,0), axisbg='w')
candlestick(ax1, candleAr, width=0.8, colorup='#9eff15', colordown='#ff1717')
ax1.xaxis.set_major_formatter(mticker.FuncFormatter(format_date))
fig.autofmt_xdate()
plt.show()
Upvotes: 2
Views: 481
Reputation: 543
For anyone interested, here is how I had to write the format_date
function to get this to work:
def format_date(x, pos=None):
newdate = mdates.num2date(date[int(x)])
if newdate.strftime('%m') == '02':
return newdate.strftime('%b\n%Y')
else: return newdate.strftime('%b')
Upvotes: 2