def plot_bokeh(df, ticker): p = figure(width=800, height=400, title=ticker.upper(), tools="") hover = HoverTool(tooltips=""" <div> <table> <tr><td class="ttlab">Date:</td><td>@date_str</td></tr> <tr><td class="ttlab">Close:</td><td>@close_str</td></tr> </table> </div> """) hover.mode = 'vline' hover.line_policy = 'nearest' p.add_tools(hover) crosshair = CrosshairTool() crosshair.dimensions = 'height' crosshair.line_color = "#ffffff" p.add_tools(crosshair) dfcds = ColumnDataSource(df) p.line('date', 'close', source=dfcds, color="#44ddaa") p.xaxis.formatter = DatetimeTickFormatter(days=["%d %b"]) p.x_range = Range1d(df['date'].min(), df['date'].max()) p.toolbar.logo = None p.toolbar_location = None return p
def bokehplot(df_1, ticker): """Create a time-series line plot in Bokeh.""" p = figure(width=600, height=300, title=ticker.upper(), tools="") hover = HoverTool(tooltips=""" <div> <table> <tr><td class="ttlab">Date:</td><td>@date_str</td></tr> <tr><td class="ttlab">Close:</td><td>@close</td></tr> </table> </div> """) hover.mode = 'vline' hover.line_policy = 'nearest' p.add_tools(hover) crosshair = CrosshairTool() crosshair.dimensions = 'height' crosshair.line_color = "#ffffff" p.add_tools(crosshair) dfcds = ColumnDataSource(df_1) p.line('date', 'close', source=dfcds, color="#44ddaa") p.xaxis.formatter = DatetimeTickFormatter(days=["%d %b"]) p.x_range = Range1d(df_1['date'].min(), df_1['date'].max()) p.toolbar.logo = None p.toolbar_location = None # Style plot p.background_fill_color = "#234567" p.border_fill_color = "#234567" p.title.text_color = "#ffffff" p.title.text_font_size = "1.25em" p.axis.major_label_text_color = "#ffffff" p.axis.major_label_text_font_size = "0.875em" p.axis.axis_line_color = "#ffffff" p.axis.major_tick_line_color = "#ffffff" p.axis.minor_tick_line_color = "#ffffff" p.xgrid.grid_line_color = None p.ygrid.grid_line_alpha = 0.5 p.ygrid.grid_line_dash = [4, 6] p.outline_line_color = None p.yaxis.axis_label = "Closing price" p.yaxis.axis_label_text_color = "#ffffff" p.yaxis.axis_label_text_font_size = "1em" p.yaxis.axis_label_text_font_style = "normal" p.yaxis.axis_label_standoff = 12 return p
def plot_ticker(ticker): # Retrieve and process data: url = urlhead + ticker + urltail page = requests.get(url) json = page.json() df = pd.DataFrame(json['Time Series (Daily)']) # New DataFrame to append values: df_1 = pd.DataFrame() close = np.asarray(df.iloc[3]) df_1['date'] = pd.to_datetime(list(df)) df_1['close'] = close # Last 30 days: df_1 = df_1[0:30] # Create a new column with dates as string: df_1['date_str'] = df_1['date'].map(lambda x: x.strftime("%Y-%m-%d")) dfcds = ColumnDataSource(df_1) # Create Bokeh plot: p = figure(width=600, height=300, title=ticker.upper(), tools="") hover = HoverTool(tooltips = [ ('Date', '@date_str'), ('Close', '@close')]) hover.mode = 'vline' hover.line_policy = 'nearest' p.add_tools(hover) crosshair = CrosshairTool() crosshair.dimensions = 'height' p.add_tools(crosshair) p.line('date', 'close', source = dfcds) p.xaxis.formatter=DatetimeTickFormatter(days=["%d %b"]) p.x_range=Range1d(df_1['date'].min(), df_1['date'].max()) p.toolbar.logo = None p.toolbar_location = None return p