I am new to bokeh and am trying to plot stock data in a line plot The X-axis should be dates and the y-axis will be Closing Price of the stock.
Here is an example of my code:
data = pd.DataFrame({'Symbol' : ['AMBA','FB','WWD','AMBA','FB','WWD','AMBA','FB','WWD','AMBA','FB','WWD','AMBA','FB','WWD','AMBA','FB','WWD',],
'Previous_Close' : [10,20,30,10,20,30,10,20,30,10,20,30,10,20,30,10,20,30,],
'Date' : pd.to_datetime(['01/01/16', '01/01/16', '01/01/16', '01/02/16', '01/02/16', '01/02/16', '01/03/16', '01/03/16', '01/03/16', '01/04/16',
'01/04/16', '01/04/16', '01/05/16', '01/05/16', '01/05/16', '01/06/16', '01/06/16', '01/06/16'], format = '%m/%d/%y')})
Symbols = [sym for sym in data.Symbol.unique()]
Dates = [date for date in data.Date.unique()]
format_dates = [date.strftime('%b%d_%y') for date in dates]
colors = ['Darkred', 'Orange', 'Navy']
output_notebook()
TOOLS = ['hover','pan','box_zoom','resize', 'save', 'reset']
# CREATE FIGURE
p = figure(width = 800, height = 600, tools = TOOLS)
cnum = 0
# PLOT
for sym in Symbols:
# plot stock closing price
p.line(range(len(dates)), data[data.Symbol==sym].Previous_Close, color = colors[cnum])
# add stock symbol as text label to end of line
p.text(len(Symbols), data[data.Symbol==sym].Previous_Close.iloc[-1], [sym],
text_color = colors[cnum], text_font_size = '10pt')
cnum +=1
show(p)
Obviously the data is made up here but the idea is the same. All i want is to set the x tick labels to the formatted date names. In Pyplot I would just do:
ax.set_xticklables(format_dates)
axis()
I have tried setting the x_axis_type to 'datetime' but this only gives me milliseconds. I've tried using DateTimeFormatter() but either I'm not understanding how to use it or it's not working (probably the former).
THINGS I'VE TRIED (RE ANSWERS):
This changes the x axis to milliseconds: p = figure(width = 800, height = 600, x_axis_type='datetime', tools = TOOLS)
cnum = 0
# PLOT
for sym in Symbols:
# NO CHANGE TO X ARG
p.line(range(len(dates)), data[data.Symbol==sym].Previous_Close, color = colors[cnum])
This changes the x ticks to decades from 1970: p = figure(width = 800, height = 600, x_axis_type='datetime', tools = TOOLS)
cnum = 0
# PLOT
for sym in Symbols:
# CHANGE X ARG TO DATETIME DATES
p.line(dates, data[data.Symbol==sym].Previous_Close, color = colors[cnum])