def verbose_formatter(): ''' Format bokeh time axis verbosely ''' vf = DatetimeTickFormatter() vf.microseconds = ['%f us'] vf.milliseconds = ['%S.%3N s'] # vf.milliseconds = ['%H:%M:%S.%3N'] vf.seconds = ['%H:%M:%S'] vf.minsec = ['%H:%M:%S'] vf.minutes = ['%m/%d %H:%M'] vf.hourmin = ['%m/%d %H:%M'] vf.hours = ['%m/%d %H:%M'] vf.days = ['%m/%d', '%a%d'] vf.months = ['%m/%Y', '%b %Y'] vf.years = ['%Y'] return vf
def create_date_plot(dsource, df, sample_t, legend): x0, x1 = x_rng_percentile(dsource, df, 1) ymax = df.n.max() * 1.05 # handle callback for box zoom box_select_callback = CustomJS(code=''' var geometry = cb_obj["geometry"]; var t0 = geometry["x0"]; var t1 = geometry["x1"]; t0 = new Date(t0-t0%{sample_t}).toISOString(); t1 = new Date(t1-t1%{sample_t}).toISOString(); var url = new URL(window.location.href); url.searchParams.set("start_t", t0); url.searchParams.set("end_t", t1); window.location.assign(url); '''.format(sample_t=sample_t)) box_select = BoxSelectTool( js_event_callbacks={'done': [box_select_callback]}) xwheel_scroll = WheelZoomTool(dimensions="width") tools = [PanTool(), box_select, xwheel_scroll, ResetTool()] p = figure(x_range=(x0, x1), y_range=(0, ymax), x_axis_type='datetime', sizing_mode='stretch_both', tools=tools, active_scroll=xwheel_scroll) p.js_on_event(SelectionGeometry, box_select_callback) p.toolbar.logo = None dtf = DatetimeTickFormatter() dtf.milliseconds = ['%T'] dtf.seconds = dtf.minsec = ['%T'] dtf.hours = dtf.hourmin = dtf.minutes = ['%R'] dtf.days = ['%b %e'] dtf.months = ['%F'] dtf.years = ['%F'] p.xaxis.formatter = dtf p.xgrid.grid_line_color = None p.vbar(x=df.t, top=df.n, width=sample_t, color='#ccffcc') add_line(p, df, color=0, legend=legend) return p
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Table, schema from sqlalchemy.orm import sessionmaker import os # In[ ]: import datetime as dt # In[ ]: XFORMATTER = DatetimeTickFormatter() XFORMATTER.years = ['%Y-%m-%d'] XFORMATTER.months = ['%Y-%m-%d'] XFORMATTER.days = ['%d/%m'] # In[ ]: pn.extension() # In[ ]: POSTGRES_USER = os.getenv('POSTGRES_USER') POSTGRES_PASSWORD = os.getenv('POSTGRES_PASSWORD') POSTGRES_ADDRESS = os.getenv('POSTGRES_ADDRESS') # In[ ]: engine = create_engine('postgresql://{0}:{1}@{2}/sensors'.format( POSTGRES_USER, POSTGRES_PASSWORD, POSTGRES_ADDRESS))
def plot2html(df, title, interval, log, line, ma): df = df.iloc[1:] up = df.close > df.open dn = df.open > df.close kwargs = dict(title=title, tools='xpan, xwheel_zoom, box_zoom, reset', active_drag='box_zoom') if log: kwargs['y_axis_type'] = 'log' plot = figure(x_axis_type='datetime', sizing_mode='stretch_both', **kwargs) plot.toolbar.active_scroll = plot.select_one(WheelZoomTool) plot.background_fill_alpha = 0 plot.border_fill_alpha = 0 plot.grid.grid_line_alpha = 0.2 plot.outline_line_alpha = 0.4 plot.xaxis.axis_line_color = 'whitesmoke' plot.yaxis.axis_line_color = 'whitesmoke' plot.xaxis.axis_line_alpha = 0 plot.yaxis.axis_line_alpha = 0 plot.xaxis.major_tick_line_alpha = 0 plot.yaxis.major_tick_line_alpha = 0 plot.xaxis.minor_tick_line_alpha = 0 plot.yaxis.minor_tick_line_alpha = 0 dtf = DatetimeTickFormatter() dtf.milliseconds = ['%T'] dtf.seconds = dtf.minsec = ['%T'] dtf.hours = dtf.hourmin = dtf.minutes = ['%R'] dtf.days = ['%F'] dtf.months = ['%F'] dtf.years = ['%F'] plot.xaxis.formatter = dtf if line: plot.line(df.time, df.close, color='#55bb77') else: pass if ma: for i, n in enumerate([50, 200]): dma = df.close.rolling(n).mean() col = bokeh.palettes.Category20[20][i % 20] plot.line(df.time, dma, color=col, legend_label='MA-%i' % n) plot.legend.background_fill_color = '#222222' plot.legend.background_fill_alpha = 0.6 plot.legend.label_text_color = 'whitesmoke' if not line: totime = { '1m': 60, '5m': 5 * 60, '15m': 15 * 60, '30m': 30 * 60, '1h': 60 * 60, '2h': 2 * 60 * 60, '4h': 4 * 60 * 60, '6h': 6 * 60 * 60, '12h': 12 * 60 * 60, '1d': 24 * 60 * 60, '1w': 7 * 24 * 60 * 60 } w = totime[interval] * 0.7 * 1000 plot.segment(df.time[up], df.hi[up], df.time[up], df.lo[up], color='#33dd99') plot.segment(df.time[dn], df.hi[dn], df.time[dn], df.lo[dn], color='#ff8866') plot.vbar(df.time[up], w, df.open[up], df.close[up], fill_color='#558866', line_color='#33dd99') plot.vbar(df.time[dn], w, df.open[dn], df.close[dn], fill_color='#cc9988', line_color='#ff8866') script, div = plot_html(plot) return div + script