def hook(plot, element): # work around for this issue: https://github.com/holoviz/holoviews/issues/2441 plot.state.x_range = DataRange1d(follow='end', follow_interval=60000, range_padding=0) plot.state.y_range = DataRange1d(follow='end', follow_interval=60000, range_padding=0)
def test_set(self): obj = bmp._list_attr_splat([DataRange1d(), DataRange1d()]) assert len(obj) == 2 assert obj[0].start == None assert obj[1].start == None obj.start = 10 assert obj[0].start == 10 assert obj[1].start == 10
def test_axis_type_auto(self): assert(bph._get_axis_class("auto", FactorRange(), 0)) == (CategoricalAxis, {}) assert(bph._get_axis_class("auto", FactorRange(), 1)) == (CategoricalAxis, {}) assert(bph._get_axis_class("auto", DataRange1d(), 0)) == (LinearAxis, {}) assert(bph._get_axis_class("auto", DataRange1d(), 1)) == (LinearAxis, {}) assert(bph._get_axis_class("auto", Range1d(), 0)) == (LinearAxis, {}) assert(bph._get_axis_class("auto", Range1d(), 1)) == (LinearAxis, {}) assert(bph._get_axis_class("auto", Range1d(start=datetime.datetime(2018, 3, 21)), 0)) == (DatetimeAxis, {}) assert(bph._get_axis_class("auto", Range1d(start=datetime.datetime(2018, 3, 21)), 1)) == (DatetimeAxis, {})
def _init_bokeh_map(self): lg.info('>> TS STATE: {}'.format(self.env.ts_state)) if self.env.ts_state is None: # this should not happen, I add it here just in case self.env.ts_state = 'online' # I set online because I cannot run tile server from here if self.env.ts_state == 'online': tile_options = dict(url=ARGIS_TS) else: tile_options = dict(url=LOCAL_TS) tile_source = WMTSTileSource(**tile_options) range_padding = 0.30 # TODO: when a profile is selected, the range size is changed?? x_range = DataRange1d(range_padding=range_padding, # range_padding_units='absolute', ) y_range = DataRange1d(range_padding=range_padding, # range_padding_units='absolute', ) self.env.wmts_map = Figure( plot_height=240, plot_width=200, output_backend=OUTPUT_BACKEND, tools='', toolbar_location='right', x_axis_type='mercator', # to avoid weird axis numbers y_axis_type='mercator', y_axis_location='left', x_range=x_range, y_range=y_range, border_fill_color= 'whitesmoke', # TODO: this should be declared on the yaml file background_fill_color='whitesmoke') self.env.wmts_map.axis.visible = True self.env.wmts_map.add_tile(tile_source) self.env.wmts_map_scatter = self.env.wmts_map.circle( x='X_WMTS', y='Y_WMTS', size=4, source=self.env.wmts_map_source, # color='#00FF00', # marker='circle', # value by default line_color=None, fill_color='#000e7a', fill_alpha=1.0, nonselection_line_color=None, nonselection_fill_color='#000e7a', nonselection_fill_alpha=1.0, ) self.env.wmts_map_scatter.selection_glyph = Circle( line_color=RED, line_alpha=1.0, fill_color='yellow', )
def make_document(doc): ser = serial.Serial(port=RECIEVER_PORT, baudrate=9600, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, timeout=2) t0, f0 = read_value_pair(ser) t1, f1 = read_value_pair(ser) source = ColumnDataSource(data=dict(time=[t0, t1], y=[f0, f1])) x_range = DataRange1d(follow="end", follow_interval=20000, range_padding=0) fig = figure(title="Value sniffer", x_axis_type="datetime", height=150, tools="", x_range=x_range, y_range=[-1.1, 1.1]) fig.line(x="time", y="y", source=source, line_width=3, line_alpha=0.6) fig.yaxis.minor_tick_line_color = None doc.add_root(fig) def update(): t, f = read_value_pair(ser) source.stream({"time": [t], "y": [f]}, 200) doc.add_periodic_callback(update, 1)
def test__check_compatible_scale_and_ranges_compat_numeric(): plot = Plot(x_scale=LinearScale(), x_range=Range1d()) check = plot._check_compatible_scale_and_ranges() assert check == [] plot = Plot(y_scale=LogScale(), y_range=DataRange1d()) check = plot._check_compatible_scale_and_ranges() assert check == []
def _init_ranges(self): lg.info('-- INIT RANGES') # TODO: do no create axis for some parameters (if not needed) for col in self.env.cur_plotted_cols: # gmax = self.env.cruise_data.df[col].max() # gmin = self.env.cruise_data.df[col].min() # d = gmax - gmin range_padding = 0.25 x_range = DataRange1d( range_padding=range_padding, renderers=[] ) y_range = DataRange1d( range_padding=range_padding, renderers=[] ) # x_range = Range1d( # start=gmin, # bounds automatically detected with DataRange1d # end=gmax, # # max_interval=gmax + d * p, # zoom out limit >> useful if hovers are used # # min_interval # zoom in limit # ) # y_range = Range1d( # start=gmin, # bounds automatically detected with DataRange1d # end=gmax, # # max_interval=gmax + d * p, # zoom out limit >> useful if hovers are used # # min_interval # zoom in limit # ) # lg.info('>> COLUMN: {} | X START: {} | X END: {} | Y START: {} | Y END: {}'.format( # col, gmin - d * p, gmax + d *p, gmin - d * p, gmax + d * p # )) if col not in self.ranges: self.env.ranges[col] = {} self.env.ranges[col]['x_range'] = x_range self.env.ranges[col]['y_range'] = y_range
def _update_histogram(self, figure, source, hist, edges, name=""): if isinstance(edges[0], str): figure.x_range = FactorRange(factors=edges) else: figure.x_range = DataRange1d() source.data = self._hist_source_dict(hist, edges) if name: figure.title.text = name figure.y_range.start = -0.1 * np.max(hist) figure.y_range.end = 1.1 * np.max(hist)
def test_validates_colons_only_in_factorial_range(self): plot = figure( x_range=DataRange1d(start=0.0, end=2.2), y_range=['0', '1', '2:0'], plot_width=900, plot_height=400, ) errors = plot._check_colon_in_category_label() self.assertEqual(errors, [(1003, 'MALFORMED_CATEGORY_LABEL', 'Category labels cannot contain colons', '[range:y_range] [first_value: 2:0] ' '[renderer: Figure(id=%r, ...)]' % plot._id)])
def test_validates_colons_only_in_factorial_range(self): plot = figure( x_range=DataRange1d(start=0.0, end=2.2), y_range=['0', '1', '2:0'], plot_width=900, plot_height=400, ) errors = plot._check_colon_in_category_label() self.assertEqual(errors, [ (1003, 'MALFORMED_CATEGORY_LABEL', 'Category labels are malformed', '[range:y_range] [first_value: 2:0] ' '[renderer: Figure, ViewModel:Plot, ref _id: ' '%s]' % plot._id) ])
def set_data(self): # set same x-range for all models self.x_range = DataRange1d(start=0, end=10) # end is initial guess weighted = self.data['weighted'] if 'kernel' in weighted: rows, x_max = self.get_single_plot() elif 'added_kernel' in weighted and 'extra_kernel' in weighted: rows, x_max = self.get_dual_plot() self.x_range.end = x_max self.plot = gridplot(rows, toolbar_options={ 'logo': None, 'merge_tools': True, 'toolbar_location': 'above', })
def set_ranges(self): super(HorizonBuilder, self).set_ranges() self.x_range = DataRange1d(range_padding=0) self.y_range.start = 0 self.y_range.end = self.y.max
def test__check_compatible_scale_and_ranges_incompat_factor_scale_and_numeric_range( ): plot = Plot(x_scale=CategoricalScale(), x_range=DataRange1d()) check = plot._check_compatible_scale_and_ranges() assert check != []
def make_plot(src): print("Make Plot called") # Blank plot with correct labels histogram = figure(plot_height=500, title='Histogram of Mortality Rate by Year', x_axis_label='Year', y_axis_label='Mortality Rate') # Quad glyphs to create a histogram histogram.quad(source=src, bottom=0, top='mortality', left='left', right='right', color='color', fill_alpha=0.7, hover_fill_color='color', legend='name', hover_fill_alpha=1.0, line_color='black') y_range = DataRange1d(start=0, bounds=None) # Blank plot for Bar Chart bar_chart = figure(plot_height=500, plot_width=1000, title="Mortality Rate by Year", toolbar_location=None, y_range=y_range) bar_chart.rect(x='year_id', y='mortalitybytwo', source=src, width=0.7, height='mortality') bar_chart.line(x='year_id', y='mortality', source=src, color='red', line_width=2) bar_chart.xaxis.major_label_orientation = math.pi / 2 # Blank plot for scatter plot scatter_plot = figure(plot_width=800, plot_height=300, y_range=y_range, title="Scatter Plot") scatter_plot.circle(x='year_id', y='mortality', source=src, alpha=0.3) # Hover tool hover = HoverTool( tooltips=[('Name', '@name'), ('Mortality Rate', '@mortality')]) histogram.add_tools(hover) bar_chart.add_tools(hover) scatter_plot.add_tools(hover) # Styling histogram = style(histogram) bar_chart = style(bar_chart) scatter_plot = style(scatter_plot) result = column(histogram, bar_chart, scatter_plot) print("Make Plot end") return result
def make_document(context, doc): print('make document') socket = context.socket(zmq.SUB) socket.connect('tcp://psdev7b:55562') socket.setsockopt(zmq.SUBSCRIBE, b'') poller = zmq.Poller() poller.register(socket, zmq.POLLIN) sources = {} figures = [] columns = [] color_cycle = itertools.cycle(Category10[10]) formatter = DatetimeTickFormatter( seconds=["%H:%M:%S"], minsec=["%H:%M:%S"], minutes=["%H:%M:%S"], hourmin=["%H:%M:%S"], hours=["%H:%M:%S"], days=["%H:%M:%S"], months=["%H:%M:%S"], years=["%H:%M:%S"], ) x_range = DataRange1d(follow='end', follow_interval=5*60*1000, range_padding=0) legend = Legend(border_line_color=None, padding=0, location=(0, 0)) figleg = figure(x_axis_type='datetime', x_range=x_range, plot_width=400, plot_height=233, title='Legend') figleg.xaxis.formatter = formatter fig = figure(x_axis_type='datetime', x_range=x_range, plot_width=400, plot_height=233, title='Event rate') fig.xaxis.formatter = formatter fig.yaxis.axis_label = 'KHz' figures.append(fig) columns.append('EventCount') fig = figure(x_axis_type='datetime', x_range=x_range, plot_width=400, plot_height=233, title='Batch rate') fig.xaxis.formatter = formatter fig.yaxis.axis_label = 'KHz' figures.append(fig) columns.append('BatchCount') fig = figure(x_axis_type='datetime', x_range=x_range, plot_width=400, plot_height=233, title='Free batch count') fig.xaxis.formatter = formatter fig.yaxis.axis_label = 'Count' figures.append(fig) columns.append('FreeBatchCnt') fig = figure(x_axis_type='datetime', x_range=x_range, plot_width=400, plot_height=233, title='Free epoch count') fig.xaxis.formatter = formatter fig.yaxis.axis_label = 'Count' figures.append(fig) columns.append('FreeEpochCnt') fig = figure(x_axis_type='datetime', x_range=x_range, plot_width=400, plot_height=233, title='Free event count') fig.xaxis.formatter = formatter fig.yaxis.axis_label = 'Count' figures.append(fig) columns.append('FreeEventCnt') layout = gridplot([[figleg, None, None ], [figures[0], figures[1], None ], [figures[2], figures[3], figures[4]]])#, sizing_mode='scale_both') doc.add_root(layout) doc.title = 'Event Builder monitor' def update(): while True: socks = dict(poller.poll(timeout=0)) if not socks: break hostname, metrics = socket.recv_json() #print(hostname, metrics) if hostname not in sources: data = {'time': [], 'EventCount': [], 'BatchCount': [], 'FreeBatchCnt': [], 'FreeEpochCnt': [], 'FreeEventCnt': []} source = ColumnDataSource(data=data) color = next(color_cycle) for i in range(len(figures)): line = figures[i].line(x='time', y=columns[i], source=source, line_width=1, color=color) #if i == 0: # legend.items.append(LegendItem(label=hostname, renderers=[line])) figleg.line(x=0, y=0, line_width=2, color=color, legend=hostname) sources[hostname] = source print('new host', hostname) # shift timestamp from UTC to current timezone and convert to milliseconds metrics['time'] = [(t - time.altzone)*1000 for t in metrics['time']] sources[hostname].stream(metrics) doc.add_periodic_callback(update, 1000)
from bokeh.models.ranges import DataRange1d from bokeh.models import DatetimeTickFormatter, NumeralTickFormatter import numpy as np from datetime import datetime, timedelta import logging import time LOGGER = logging.getLogger(__name__) LOGGER.info("HI") pair = "BITF.BTC.USD" num_levels = 5 window_width = timedelta(seconds=30).seconds * 1000 window_height = num_levels*2 x_range = DataRange1d(follow='end', follow_interval=window_width) y_range = DataRange1d(follow='end', follow_interval=window_height, range_padding=1) source = ColumnDataSource(dict.fromkeys( ['datetime', 'trade_price', 'trade_size', 'trade_aggressor'] + [part.format(i) for i in range(0, num_levels) for part in ['order_bid_{0}_price', 'order_ask_{0}_price', 'order_bid_{0}_qty', 'order_ask_{0}_qty', 'order_bid_{0}_scaled_qty', 'order_ask_{0}_scaled_qty']], [])) fig = figure(title='Order Map', x_axis_type='datetime', x_range=x_range, y_range=y_range)
def test__get_scale_numeric_range_log_axis(): s = bph._get_scale(DataRange1d(), "log") assert isinstance(s, LogScale)
r = kw['edge_renderer'] assert r.glyph.line_color == "purple" assert r.selection_glyph.line_color == "blue" assert r.nonselection_glyph.line_color == "yellow" assert r.hover_glyph.line_color == "red" assert r.muted_glyph.line_color == "orange" assert r.glyph.line_width == 23 assert r.selection_glyph.line_width == 23 assert r.nonselection_glyph.line_width == 23 assert r.hover_glyph.line_width == 23 assert r.muted_glyph.line_width == 23 _RANGES = [Range1d(), DataRange1d(), FactorRange()] class Test__get_axis_class(object): @pytest.mark.parametrize('range', _RANGES) @pytest.mark.unit def test_axis_type_None(self, range): assert (bph._get_axis_class(None, range, 0)) == (None, {}) assert (bph._get_axis_class(None, range, 1)) == (None, {}) @pytest.mark.parametrize('range', _RANGES) @pytest.mark.unit def test_axis_type_linear(self, range): assert (bph._get_axis_class("linear", range, 0)) == (LinearAxis, {}) assert (bph._get_axis_class("linear", range, 1)) == (LinearAxis, {})
description = Div( text= """<b><code>tile_demo.py</code></b> - Bokeh tile provider examples. Linked Pan and Zoom on all maps!""" ) # pick a location and generate a 4-point window around it: bottom-left, upper-right lat = 30.268801 # Lady Bird Lake, Austin Texas lon = -97.763347 EN = lnglat_to_meters(lon, lat) dE = 1000 # (m) Easting plus-and-minus from map center dN = 1000 # (m) Northing plus-and-minus from map center x_range = DataRange1d(start=EN[0] - dE, end=EN[0] + dE) # (m) Easting x_lo, x_hi y_range = DataRange1d(start=EN[1] - dN, end=EN[1] + dN) # (m) Northing y_lo, y_hi plot = [0] * len(Vendors) # initialize list to store Vendor plots idx = 0 for vendor_name in Vendors: print(f"cnt={idx}: Vendor={vendor_name}") tile_provider = get_provider(vendor_name) plot[idx] = figure(x_range=x_range, y_range=y_range, x_axis_type="mercator", y_axis_type="mercator", plot_height=200, plot_width=300,
ROLL = 1000 span = 10*60 #Seconds line_width = 4 y_max = None y_min = None range_scale = 0.2 curdoc().theme = 'dark_minimal' #DataRange1d(only_visible = True) p = figure(plot_width=1000, plot_height=400, x_axis_label = 'Time', y_axis_label = 'Random Number', x_axis_type = 'datetime', tools="pan,xwheel_zoom,ywheel_zoom,xbox_zoom,reset", y_range=DataRange1d(only_visible = True, max_interval = y_max, min_interval = y_min), sizing_mode='stretch_both', lod_timeout = 100, lod_threshold = 10, lod_factor = 2000, # output_backend = 'webgl', x_range = DataRange1d(only_visible = True, follow = "end", follow_interval = span*1000, max_interval = 60*1000*60*24, min_interval = 1000, range_padding_units = 'absolute',range_padding = 1000,)) #other_property = here) p.yaxis.visible = False p.xaxis.formatter = DatetimeTickFormatter(milliseconds = '%H:%M:%S.%2N',seconds = "%H:%M:%S",minsec = "%H:%M:%S",minutes = "%H:%M:%S",hourmin = "%H:%M:%S",hours = "%H:%M:%S",days = ['%m/%d', '%a%d'],months = ['%m/%Y', '%b %Y'],years = ['%Y']) #slider = Slider(title = 'Follow Range', start= 2*1000 , end= 60*1000, step=1000, value=span*1000)
def fit(self, cv_results, estimeted_end_time): cv_results, cv_score_std, param_dists = self._init_cv_results(cv_results) if self.bokeh_handle is None: if cv_results is None: return # mk bokeh source self.cv_src, cv_hover = self._mk_score_source(cv_results, xcol=NoteBookVisualizer.time_col, score_cols=[NoteBookVisualizer.score_cols[i] for i in self.data_types], hover_cols=self.all_param_cols) self.end_time_src = ColumnDataSource(data=dict(text=["This search end time(estimated): "+estimeted_end_time])) self.cv_score_std_src = ColumnDataSource(data=cv_score_std) self.best_src = self._mk_score_source(cv_results, xcol=NoteBookVisualizer.time_col, score_cols=["best_"+i for i in self.data_types]) self.param_srcs = dict() for key in param_dists.keys(): self.param_srcs[key] = ColumnDataSource(data= param_dists[key]) # CV Score transition cv_p = figure(title="CV Score transition", x_axis_label="time", y_axis_label="score", x_axis_type="datetime", plot_width=int(NoteBookVisualizer.display_width/2), plot_height=275, toolbar_location="above", tools=[SaveTool(), ResetTool(), PanTool(), WheelZoomTool()]) for data_type in self.data_types: if data_type=="valid": cv_p = self._add_line(cv_p, xcol=NoteBookVisualizer.time_col, ycol=NoteBookVisualizer.score_cols[data_type], score_source=self.cv_src, color=NoteBookVisualizer.colors[data_type], legend=data_type) else: cv_p = self._add_line(cv_p, xcol=NoteBookVisualizer.time_col, ycol=NoteBookVisualizer.score_cols[data_type], score_source=self.cv_src, score_std_source=self.cv_score_std_src, color=NoteBookVisualizer.colors[data_type], legend=data_type) display_etime = LabelSet(x=0, y=0, x_offset=80, y_offset=20, x_units="screen", y_units="screen", render_mode="canvas", text="text", source=self.end_time_src, text_font="segoe ui", text_font_style ="italic", background_fill_color="white", background_fill_alpha=0.5) cv_p.add_layout(display_etime) cv_p.add_tools(cv_hover) cv_p.legend.location = "top_left" cv_p.xaxis.minor_tick_line_color = None cv_p.yaxis.minor_tick_line_color = None cv_p = self._arrange_fig(cv_p) # Best Score transition best_p = figure(title="Best Score transition", x_axis_label="time", y_axis_label="score", x_range=cv_p.x_range, y_range=cv_p.y_range, x_axis_type="datetime", plot_width=int(NoteBookVisualizer.display_width/2), plot_height=275, toolbar_location="above", tools=[PanTool(), WheelZoomTool(), SaveTool(), ResetTool()]) for data_type in self.data_types: best_p = self._add_line(best_p, xcol=NoteBookVisualizer.time_col, ycol="best_"+data_type, score_source=self.best_src, color=NoteBookVisualizer.colors[data_type], legend=data_type) best_p.legend.location = "top_left" best_p.xaxis.minor_tick_line_color = None best_p.yaxis.minor_tick_line_color = None best_p = self._arrange_fig(best_p) # Param distributions param_vbar_ps = dict() param_hist_ps = dict() tmp = list(self.param_cols) if st.FEATURE_SELECT_PARAMNAME_PREFIX in self.param_srcs.keys(): tmp = [st.FEATURE_SELECT_PARAMNAME_PREFIX] + tmp for param_col in tmp: if "label" in list(param_dists[param_col].keys()): # Bar graph param_vbar_ps[param_col] = figure(title=param_col, y_axis_label="frequency", plot_width=int(NoteBookVisualizer.display_width/NoteBookVisualizer.n_col_param), plot_height=int(NoteBookVisualizer.display_width/NoteBookVisualizer.n_col_param), #x_range=FactorRange(factors=self.param_srcs[param_col].data["x"]), y_range=DataRange1d(min_interval=1.0, start=0, default_span=1.0), toolbar_location="above", tools=[SaveTool(), HoverTool(tooltips=[("label","@label"), ("top","@top")])]) param_vbar_ps[param_col].vbar(x="x", top="top", source=self.param_srcs[param_col], width=0.5, bottom=0, color="#9467bd", fill_alpha=0.5) labels = LabelSet(x="x", y=0, level="glyph", text="label", text_align="center", text_font="segoe ui", text_font_style="normal", text_font_size="8pt", x_offset=0, y_offset=0, source=self.param_srcs[param_col], render_mode="canvas") param_vbar_ps[param_col].add_layout(labels) param_vbar_ps[param_col].xaxis.major_label_text_font_size = "0pt" param_vbar_ps[param_col].xaxis.major_tick_line_color = None param_vbar_ps[param_col].xaxis.minor_tick_line_color = None param_vbar_ps[param_col].yaxis.minor_tick_line_color = None param_vbar_ps[param_col] = self._arrange_fig(param_vbar_ps[param_col]) else: # Histgram param_hist_ps[param_col] = figure(title=param_col, y_axis_label="frequency", plot_width=int(NoteBookVisualizer.display_width/NoteBookVisualizer.n_col_param), plot_height=int(NoteBookVisualizer.display_width/NoteBookVisualizer.n_col_param), y_range=DataRange1d(min_interval=1.0, start=0), toolbar_location="above", tools=[SaveTool(), HoverTool(tooltips=[("left","@left"), ("right","@right"), ("top","@top")])]) param_hist_ps[param_col].quad(top="top", bottom=0, left="left", right="right", source=self.param_srcs[param_col], color="#17becf", fill_alpha=0.5) param_hist_ps[param_col].xaxis.minor_tick_line_color = None param_hist_ps[param_col].yaxis.minor_tick_line_color = None param_hist_ps[param_col] = self._arrange_fig(param_hist_ps[param_col]) title = Div(text=NoteBookVisualizer.title.replace("TEXT", self.model_id), width=int(NoteBookVisualizer.display_width)) scores_headline = Div(text=NoteBookVisualizer.headline.replace("TEXT", " Score History"), width=int(NoteBookVisualizer.display_width*0.9)) params_headline = Div(text=NoteBookVisualizer.headline.replace("TEXT", " Parameter History"), width=int(NoteBookVisualizer.display_width*0.9)) self.p = layouts.layout([title, [scores_headline]]+[[cv_p, best_p]]+[[params_headline]]+\ [list(param_vbar_ps.values())[i:i+NoteBookVisualizer.n_col_param] for i in range(0, len(param_vbar_ps), NoteBookVisualizer.n_col_param)]+\ [list(param_hist_ps.values())[i:i+NoteBookVisualizer.n_col_param] for i in range(0, len(param_hist_ps), NoteBookVisualizer.n_col_param)]) self.bokeh_handle = show(self.p, notebook_handle=True) else: # update bokeh src self.end_time_src.patch({"text":[(0, "This search end time(estimated): "+estimeted_end_time)]}) if len(cv_results) != len(self.cv_src.data[NoteBookVisualizer.time_col]): self.cv_src.stream(cv_results[list(self.cv_src.data.keys())].iloc[-1:].to_dict(orient="list"), rollover=NoteBookVisualizer.stream_rollover) self.best_src.stream(cv_results[list(self.best_src.data.keys())].iloc[-1:].to_dict(orient="list"), rollover=NoteBookVisualizer.stream_rollover) push_notebook(handle=self.bokeh_handle) self._update_cv_score_std_src(cv_score_std) self._update_param_srcs(param_dists) if self.savepath is not None: self._save_graph(search_algo=str(cv_results["search_algo"].iloc[0]), n_iter=int(cv_results["index"].iloc[-1]))
published. The red borders indicate the high point of total article count for each calendar week. </p> ''' # must be done by hand bk_section3_text4 = ''' <p> As the graph shows, topics 6, 7, and 8 run almost constantly through the time period in question, although topic 6 is a smaller element of the total conversation. Topics 1 and 2, on the other hand, are frequently important elements on the weekly highpoints. </p> <hr> ''' # keyword frequency plot plot_kf = Plot(x_range=DataRange1d(), y_range=DataRange1d(start=0), plot_height=300, plot_width=600, toolbar_location=None, logo=None) glyph_kf = Rect(x='width_midpoint', y='height_midpoint', width=1, height='height') plot_kf.add_glyph(ColumnDataSource(kf_df), glyph_kf) plot_kf.add_layout( LinearAxis(axis_label='Number of keywords', **axis_defaults), 'left') plot_kf.add_layout( LinearAxis(axis_label='Number of times used', **axis_defaults), 'below')
def bar_subdivisions(cds, category, metric): """ Create bar graphs comparing a single metric across subdivisions of time for multiple forecasts. e.g.:: Fx 1 MAE | |_________________ Fx 2 MAE | |_________________ Year, Month of the year, etc. Parameters ---------- cds : bokeh.models.ColumnDataSource Fields must be kind and the names of the forecasts category : str One of the available metrics grouping categories (e.g., total) Returns ------- figs : dict of figures """ palette = cycle(PALETTE) tools = 'pan,xwheel_zoom,box_zoom,reset,save' fig_kwargs = dict(tools=tools, toolbar_location='above') figs = {} width = 0.8 human_category = datamodel.ALLOWED_CATEGORIES[category] metric_name = datamodel.ALLOWED_DETERMINISTIC_METRICS[metric] fig_kwargs['x_axis_label'] = human_category fig_kwargs['y_axis_label'] = metric_name filter_ = ((np.asarray(cds.data['category']) == category) & (np.asarray(cds.data['metric']) == metric)) # Special handling for x-axis with dates if category == 'date': fig_kwargs['x_axis_type'] = 'datetime' width = width * pd.Timedelta(days=1) fig_kwargs['x_range'] = DataRange1d() elif category == 'month': fig_kwargs['x_range'] = FactorRange(factors=calendar.month_abbr[1:]) elif category == 'weekday': fig_kwargs['x_range'] = FactorRange(factors=calendar.day_abbr[0:]) elif category == 'hour': fig_kwargs['x_range'] = FactorRange( factors=[str(i) for i in range(25)]) else: fig_kwargs['x_range'] = FactorRange( factors=np.unique(cds.data['index'][filter_])) y_data = np.asarray(cds.data['value'])[filter_] if len(y_data) == 0: start, end = None, None else: y_min = np.nanmin(y_data) y_max = np.nanmax(y_data) start, end = calc_y_start_end(y_min, y_max) fig_kwargs['y_range'] = DataRange1d(start=start, end=end) unique_names = np.unique(np.asarray(cds.data['name'])[filter_]) for name in unique_names: view = CDSView(source=cds, filters=[ GroupFilter(column_name='metric', group=metric), GroupFilter(column_name='category', group=category), GroupFilter(column_name='name', group=name) ]) # Create figure title = name + ' ' + metric_name fig = figure(width=800, height=200, title=title, name=f'{category}_{metric}_{name}', **fig_kwargs) # Custom bar alignment if category == 'hour': # Center bars between hour ticks x = dodge('index', 0.5, range=fig.x_range) else: x = 'index' fig.vbar(x=x, top='value', width=width, source=cds, view=view, line_color='white', fill_color=next(palette)) # axes parameters fig.xgrid.grid_line_color = None fig.xaxis.minor_tick_line_color = None # Hover tool and format specific changes if category == 'date': # Datetime x-axis formatter = DatetimeTickFormatter(days='%Y-%m-%d') fig.xaxis.formatter = formatter tooltips = [ ('Forecast', '@name'), (human_category, '@index{%F}'), (metric_name, '@value'), ] hover_kwargs = dict(tooltips=tooltips, formatters={'index': 'datetime'}) elif category == 'month' or category == 'weekday': # Categorical x-axis formatter = CategoricalTickFormatter() fig.xaxis.formatter = formatter tooltips = [ ('Forecast', '@name'), (human_category, '@index'), (metric_name, '@value'), ] hover_kwargs = dict(tooltips=tooltips) else: # Numerical x-axis tooltips = [ ('Forecast', '@name'), (human_category, '@index'), (metric_name, '@value'), ] hover_kwargs = dict(tooltips=tooltips) hover = HoverTool(mode='vline', **hover_kwargs) fig.add_tools(hover) figs[name] = fig return figs
def cones_bokeh(self, windows=[30, 60, 90, 120], quantiles=[0.25, 0.75]): """Plots volatility cones Parameters ---------- windows : [int, int, ...] List of rolling windows for which to calculate the estimator cones quantiles : [lower, upper] List of lower and upper quantiles for which to plot the cones """ top_q, median, bottom_q, realized, min, max, f, data = self.cones_prepare_data( windows, quantiles) colors_list = ['orange', 'blue', 'pink', 'black', 'red', 'green'] methods_list = [ 'x', 'diamond', 'x', 'square', 'inverted_triangle', 'inverted_triangle' ] line_dash_list = [ 'dotted', 'dotdash', 'dotted', 'solid', 'dashed', 'dashed' ] xs = [windows, windows, windows, windows, windows, windows] ys = [top_q, median, bottom_q, realized, min, max] legends_list = [ str(int(quantiles[1] * 100)) + " Prctl", 'Median', str(int(quantiles[0] * 100)) + " Prctl", 'Realized', 'Min', 'Max' ] title = self._estimator + ' (' + self._symbol + ', daily from ' + self._last_date + ' days back ' + str( self._num_days_back) + ')' p = figure(title=title, plot_width=700, plot_height=500, toolbar_sticky=False, x_axis_label="Days", y_axis_label="Volatility", toolbar_location="below") legend_items = [] for (colr, leg, x, y, method, line_dash) in zip(colors_list, legends_list, xs, ys, methods_list, line_dash_list): # call dynamically the method to plot line, circle etc... renderers = [] if method: renderers.append(getattr(p, method)(x, y, color=colr, size=4)) renderers.append(p.line(x, y, color=colr, line_dash=line_dash)) legend_items.append((leg, renderers)) # doesnt work: legend = Legend(location=(0, -30), items=legend_items) legend = Legend(location=(0, -30), items=legend_items) p.add_layout(legend, 'right') from bokeh.charts import BoxPlot df = pandas.DataFrame({"data": data[0], "group": 0}) df = df.append(pandas.DataFrame({"data": data[1], "group": 1})) df = df.append(pandas.DataFrame({"data": data[2], "group": 2})) df = df.append(pandas.DataFrame({"data": data[3], "group": 3})) p2 = BoxPlot(df, values='data', label='group', title="Boxplot Summary (" + self._last_date + ") (" + self._symbol + ")", toolbar_location="below", legend="bottom_right", plot_width=600, plot_height=400, toolbar_sticky=False) from bokeh.models.ranges import DataRange1d p2.y_range = DataRange1d(np.min(df['data'] - 0.01), np.max(df['data'] + 0.01)) layout1 = layout([[p, p2]]) script, div = components(layout1) save_graph_to_db(script, div, self._symbol, self._expiry, self._last_date, self._num_days_back, self._resample, self._estimator, name="VOLEST") return layout1
path = os.path.join(__location__, "auto_ru_yamaha.csv") titanic = pd.read_csv(path) glides = titanic[lambda x: x["model"] == "YZF_R6"] means = glides.groupby(["year"]).mean() diff = means.pct_change() * 100 count = glides.groupby(["year"]).count() print(count) p1 = figure(title="Yamaha YZF-R6", plot_height=350) p1.grid.grid_line_alpha = 0.3 p1.xaxis.axis_label = "Year" p1.yaxis.axis_label = "Price" p1.x_range = DataRange1d(start=2021, end=1995) p1.line(source=ColumnDataSource(means), x="year", y="price", color="#A6CEE3") p2 = figure(title="Yamaha YZF-R6", plot_height=150) p2.xaxis.axis_label = "Year" p2.yaxis.axis_label = "Count" p2.vbar(source=ColumnDataSource(count), x="year", top="price", width=0.9) p2.x_range = DataRange1d(start=2021, end=1995) p3 = figure(title="Yamaha YZF-R6", plot_height=150) p3.xaxis.axis_label = "Year" p3.yaxis.axis_label = "Loose" p3.vbar(source=ColumnDataSource(diff), x="year", top="price", width=0.9) p3.x_range = DataRange1d(start=2021, end=1995) show(gridplot([[p1, None], [p2, None], [p3, None]]))
def timing_plot(genfn): "Draw a timing plot for a prime generator function" if not check_fn(genfn): return global _lines def plot(fig, name, vals, num, dash='solid'): "Add a line with points to a plot" col = _palette[num % len(_palette)] fig.line('x', 'y', legend_label=name, source=vals, line_dash=dash, color=col) fig.scatter('x', 'y', legend_label=name, source=vals, marker='o', color=col) name = genfn.__name__ exist = None args = dict(plot_width=800, plot_height=400, toolbar_location='above', title="Timing") linfig = figure(y_range=[0, 1], x_range=DataRange1d(start=0), **args) logfig = figure(y_range=[1e-6, 1], x_range=DataRange1d(start=1), x_axis_type='log', y_axis_type='log', **args) num = 0 # add previous lines for k, v in _lines.items(): plot(linfig, k, v, num, 'dashed') plot(logfig, k, v, num, 'dashed') if k == name: exist = num num += 1 source = ColumnDataSource(data=dict(x=[], y=[])) for fig in (linfig, logfig): plot(fig, name, source, exist or num) fig.xaxis.axis_label = "Primes" fig.xaxis.formatter = NumeralTickFormatter(format='0[.]0 a') fig.xgrid.minor_grid_line_color = 'lightgrey' fig.xgrid.minor_grid_line_alpha = 0.2 fig.yaxis.axis_label = "Seconds" fig.legend.location = 'bottom_right' fig.legend.click_policy = 'hide' fig.legend.background_fill_alpha = 0.5 linfig.yaxis.formatter = BasicTickFormatter() logfig.yaxis.formatter = BasicTickFormatter(use_scientific=True, precision=0) lintab = Panel(child=linfig, title="Linear") logtab = Panel(child=logfig, title="Log") tabs = Tabs(tabs=[lintab, logtab]) handle = None if _incremental: # Incremental: show plot now, then incrementally add points handle = show(tabs, notebook_handle=True) try: genfn() combined = True except TypeError: combined = False if combined: # Generate line in one go plot_line_combined(genfn, source, handle) else: # Generator takes size, need to generate points separately plot_line_separate(genfn, source, handle) if not _incremental: # Plot not shown yet, show it now show(tabs) # save line data to show on next plot _lines[name] = source.data
def plot_interactive_footprint(self): """Use bokeh to create an interactive algorithm footprint with zoom and hover tooltips. Should avoid problems with overplotting (since we can zoom) and provide better information about instances.""" features = np.array(self.features_2d) instances = self.insts runhistory = self.rh algo = {v: k for k, v in self.algo_name.items()} incumbent = algo['incumbent'] default = algo['default'] source = ColumnDataSource(data=dict(x=features[:, 0], y=features[:, 1])) # Add all necessary information for incumbent and default source.add(instances, 'instance_name') instance_set = [ 'train' if i in self.train_feats.keys() else 'test' for i in instances ] source.add(instance_set, 'instance_set') # train or test for config, name in [(incumbent, 'incumbent'), (default, 'default')]: cost = get_cost_dict_for_config(runhistory, config) source.add([cost[i] for i in instances], '{}_cost'.format(name)) # TODO should be in function good, bad = self._get_good_bad(config) color = [ 1 if idx in good else 0 for idx, i in enumerate(instances) ] # TODO end color = ['blue' if c else 'red' for c in color] self.logger.debug("%s colors: %s", name, str(color)) source.add(color, '{}_color'.format(name)) source.add(source.data['default_color'], 'color') # Define what appears in tooltips hover = HoverTool(tooltips=[ ('instance name', '@instance_name'), ('def cost', '@default_cost'), ('inc_cost', '@incumbent_cost'), ('set', '@instance_set'), ]) # Add radio-button def_inc_callback = CustomJS(args=dict(source=source), code=""" var data = source.data; if (cb_obj.active == 0) { data['color'] = data['default_color']; } else { data['color'] = data['incumbent_color']; } source.change.emit(); """) def_inc_radio_button = RadioButtonGroup( labels=["default", "incumbent"], active=0, callback=def_inc_callback) # Plot x_range = DataRange1d(bounds='auto', start=min(features[:, 0]) - 1, end=max(features[:, 0]) + 1) y_range = DataRange1d(bounds='auto', start=min(features[:, 1]) - 1, end=max(features[:, 1]) + 1) p = figure( plot_height=500, plot_width=600, tools=[hover, 'save', 'wheel_zoom', 'box_zoom', 'pan', 'reset'], active_drag='box_zoom', x_range=x_range, y_range=y_range) # Scatter train and test individually to toggle them train_view = CDSView( source=source, filters=[GroupFilter(column_name='instance_set', group='train')]) test_view = CDSView( source=source, filters=[GroupFilter(column_name='instance_set', group='test')]) train = p.scatter(x='x', y='y', source=source, view=train_view, color='color') test = p.scatter(x='x', y='y', source=source, view=test_view, color='color') p.xaxis.axis_label, p.yaxis.axis_label = 'principal component 1', 'principal component 2' p.xaxis.axis_label_text_font_size = p.yaxis.axis_label_text_font_size = "15pt" train_test_callback = CustomJS(args=dict(source=source, train_view=train, test_view=test), code=""" var data = source.data; if (cb_obj.active == 0) { train_view.visible = true; test_view.visible = true; } else if (cb_obj.active == 1) { train_view.visible = true; test_view.visible = false; } else { train_view.visible = false; test_view.visible = true; } """) train_test_radio_button = RadioButtonGroup( labels=["all", "train", "test"], active=0, callback=train_test_callback) # Export and return if self.output_dir: path = os.path.join(self.output_dir, "content/images/algorithm_footprint.png") export_bokeh(p, path, self.logger) layout = column( p, row(widgetbox(def_inc_radio_button), widgetbox(train_test_radio_button))) return layout
def InitializePlot(stream_flag, start, end, resolution): analog, time_analog, digital, time_digital, start = GetInitialData( stream_flag, start, end, resolution) dic_digital = GetDigitalDataDictionaryFromData(digital, time_digital) dic_analog = GetAnalogDataDictionaryFromData(analog, time_analog) source_digital = ColumnDataSource(dic_digital) source_analog = ColumnDataSource(dic_analog) tools1 = 'xpan,xwheel_zoom,reset,hover,save,tap' p1 = figure(width=800, height=200, tools=tools1, x_axis_type="datetime", active_drag="xpan", title="Digital Input", responsive=True) p1.grid.grid_line_alpha = 0.3 p1.y_range = Range1d(0, 6.2) p1.xaxis.axis_label = 'time' p1.ygrid.grid_line_alpha = 1 p1.ygrid.grid_line_width = 1 p1.ygrid.grid_line_color = '#000000' p1.yaxis.visible = False colors = ['#FF0000', '#00FF00', '#0000FF', '#808000', '#800080', '#20b2aa'] p1.select_one(HoverTool).tooltips = "@timestr" p1.select_one(HoverTool).line_policy = 'nearest' # show nearest datapoint for k in range(6): p1.line(x='time_', y='d{}'.format(k), color=colors[k], legend=config.channel_names_digital_display[k], line_width=3, source=source_digital) p1.legend.location = "top_left" tools2 = 'pan,wheel_zoom,reset,hover,save,tap' p2 = figure(width=800, height=200, tools=tools2, x_axis_type="datetime", active_drag="pan", title=config.channel_names_analog_display[0], responsive=True) p2.grid.grid_line_alpha = 0.3 p2.xaxis.axis_label = 'time' p2.yaxis.axis_label = '{} / bar'.format( config.channel_names_analog_display[0]) p2.select_one(HoverTool).tooltips = [ ("(x,y)", "(@timestr, @a0)"), ] p2.select_one(HoverTool).line_policy = 'nearest' # show nearest datapoint p2.line(x='time_', y='a0', line_width=3, source=source_analog) p3 = figure(width=800, height=200, tools=tools2, x_axis_type="datetime", active_drag="pan", title=config.channel_names_analog_display[1], responsive=True) p3.grid.grid_line_alpha = 0.3 p3.xaxis.axis_label = 'time' p3.yaxis.axis_label = '{} / mbar'.format( config.channel_names_analog_display[1]) p3.select_one(HoverTool).tooltips = [ ("(x,y)", "(@timestr, @a1)"), ] p3.select_one(HoverTool).line_policy = 'nearest' # show nearest datapoint p3.line(x='time_', y='a1', line_width=3, source=source_analog) if stream_flag: p1.x_range = DataRange1d(follow='end', follow_interval=config.rollover_time * 1000) p2.x_range = p1.x_range p3.x_range = p1.x_range return p1, p2, p3, analog, time_analog, digital, time_digital, source_analog, source_digital
highlights = np.zeros(bin_no) histo_dict['highlights'] = highlights return histo_dict, hedges histo_dict, hedges = load_histo(data_dict) source2 = ColumnDataSource(data=histo_dict) histo_data = HoverTool(tooltips=[('bin', '$index'), ('percent contrast', '@r_edges'), ('total particles', '@hhist'), ('particles selected', '@highlights')], ) ph = figure(plot_width=p.plot_width, plot_height=180, x_range=DataRange1d(start=0, follow='end', range_padding=0.1), y_range=DataRange1d(start=0, follow='end', range_padding=0.25), min_border=10, min_border_left=5, y_axis_location='left', tools=['xbox_select,tap,reset', histo_data], toolbar_location='right', title="Particle Contrast Histogram") ph.xgrid.grid_line_color = None ph.yaxis.major_label_orientation = np.pi / 4 ph.background_fill_color = "#fafafa" ph.select(type=TapTool) ph.select(type=BoxSelectTool) main_histo = ph.quad(bottom=0,