def init_rtplot(cust_palette: List, patchsources: Dict[str, Tuple], default_county: str, low: float = 0.5, high: float = 1.5) -> Tuple[Figure, Dict]: mapper = linear_cmap(field_name='Rt', palette=cust_palette, low=low, high=high, low_color='#33FF33', high_color='#FF3333') plot_size_and_tools = { 'height_policy': 'fit', 'width_policy': 'fit', 'plot_width': 300, 'plot_height': 250, 'min_width': 250, 'max_height': 300, 'min_height': 250, 'border_fill_alpha': 0, 'background_fill_alpha': 0, 'margin': 5, 'y_axis_label': 'Effective R', 'x_axis_type': 'datetime', 'tools': ['reset', 'zoom_in', 'zoom_out', 'pan'] } plot = Figure(title='', **plot_size_and_tools) # need to pre-build separate dict of per-county patch columndatasources since bokeh doesn't allow patches to be # built from cdsviews at the moment patch = Patch(x="dates", y="rt", fill_color="#282e54", fill_alpha=0.3, line_alpha=0.1) initialcds = ColumnDataSource( dict(dates=np.empty((100, 100)), rt=np.empty((100, 100)))) plot.add_glyph(initialcds, patch) hline = Span(location=1, dimension='width', line_color='#003566', line_alpha=0.8, line_width=3, line_dash='dashed') plot.renderers.extend([hline]) plot.y_range.end = patchsources[default_county][3] plot.yaxis.axis_label_text_font_size = "12pt" plot.yaxis.axis_label_text_color = '#003566' plot.xaxis.axis_label_text_color = '#003566' plot.x_range.range_padding = 0 plot.y_range.range_padding = 0 plot.xaxis.bounds = (patchsources[default_county][1], patchsources[default_county][2]) return plot, mapper
class Plot2d: def __init__(self, x_range=None, y_range=None, x_label="", y_label="", tooltip=None, title="", width=600, height=400, yscale="auto", font_size="1vw", hover_mode="mouse"): hover = HoverTool(tooltips=tooltip, mode=hover_mode) if y_range == None: self.plot = Figure( tools=[hover, "pan,wheel_zoom,box_zoom,reset,crosshair,tap"], plot_width=width, active_drag="box_zoom", plot_height=height, background_fill_color="white", x_axis_type="auto", y_axis_type=yscale) else: self.plot = Figure( tools=[hover, "pan,wheel_zoom,box_zoom,reset,crosshair,tap"], plot_width=width, active_drag="box_zoom", plot_height=height, background_fill_color="white", x_axis_type="auto", y_axis_type=yscale, y_range=y_range, x_range=x_range) self.plot.xaxis.axis_label = x_label self.plot.yaxis.axis_label = y_label self.plot.xaxis.major_label_text_font_size = font_size self.plot.yaxis.major_label_text_font_size = font_size self.plot.xaxis.axis_label_text_font_size = font_size self.plot.yaxis.axis_label_text_font_size = font_size self.plot.legend.label_text_font_size = font_size self.plot.title.text_font_size = font_size self.plot.title.text = title def vbar(self, source, y="y", x="x", line_width=0.01): self.plot.vbar(top=y, x=x, width=0.8, source=source, fill_color="dodgerblue", line_color="black", line_width=line_width, alpha=0.8, hover_fill_color='red', hover_line_color='red', hover_alpha=0.8) return self.plot def quad(self, source, top, bottom='bottom', left='left', right='right', name='data', line_width=0.1): self.plot.quad(top=top, bottom=bottom, left=left, right=right, name=name, source=source, fill_color="dodgerblue", line_color="black", line_width=line_width, alpha=0.8, hover_fill_color='red', hover_line_color='red', hover_alpha=0.8) return self.plot def line(self, source, x='x', y='y', color="black", line_width=1.5, line_alpha=0.9): self.plot.line(x=x, y=y, source=source, color=color, line_width=line_width, line_alpha=line_alpha) return self def circle(self, source, x='x', y='y', color="blue", size=8, line_color='black', alpha=0.7, hover_color="blue", hover_alpha=1, hover_line_color='red', radius=0.0165, fill_color='lightgray', line_width=0.3, hover_fill_color=None): self.plot.circle( x=x, y=y, source=source, color=color, size=size, line_color=line_color, alpha=alpha, hover_color=hover_color, hover_alpha=hover_alpha, hover_line_color=hover_line_color, fill_color=fill_color, hover_fill_color=hover_fill_color, ) return self def set_colorbar(self, z_value): ''' Setup for colorbar ''' dz = z_value # removing NaN in ranges dz_valid = [x if x > -999 else np.nan for x in dz] dzmax, dzmin = np.nanmax(dz_valid), np.nanmin(dz_valid) if np.log10(dzmax) > 4 or np.log10(dzmin) < -3: ztext = ['{:4.2e}'.format(i) for i in dz_valid] cbarformat = "%2.1e" elif np.log10(dzmin) > 0: ztext = ['{:5.2f}'.format(i) for i in dz_valid] cbarformat = "%4.2f" else: ztext = ['{:6.2f}'.format(i) for i in dz_valid] cbarformat = "%5.2f" return ztext, cbarformat def colorbar(self, mapper, title='(Val - Ref)'): formatter = PrintfTickFormatter(format="%4.2f") color_bar = ColorBar(color_mapper=mapper, label_standoff=16, title=title, major_label_text_font_style='bold', padding=26, major_label_text_align='left', major_label_text_font_size="10pt", formatter=formatter, title_text_baseline='alphabetic', location=(0, 0)) self.plot.add_layout(color_bar, 'right') return self def wedge(self, source, field=None, x="x", y="y", mapper=None, radius=0.0165, colorbar_title='counts'): if mapper: self.plot = self.circle( source, y=y, x=x, radius=radius, fill_color={ 'field': field, 'transform': mapper }, line_color='black', line_width=0.3, ).circle(source, y=y, x=x, radius=radius + 0.005, fill_color=None, line_color=None, line_width=3, hover_fill_color={ 'field': field, 'transform': mapper }, hover_line_color='red').colorbar( mapper, title=colorbar_title).plot elif field: self.plot = self.circle( source, y=y, x=x, radius=radius, hover_fill_color='lightgrey', fill_color={ 'field': 'color' }, line_color='black', line_width=0.3, ).plot else: self.plot = self.circle( source, y=y, x=x, radius=radius, hover_fill_color='lightgrey', fill_color='darkgrey', line_color='black', line_width=0.3, ).plot return self def segment(self, source, x0='segx0', x1='segx1', y0='segy0', y1='segy1', line_width=2, line_color='#1e90ff'): seg = Segment(x0=x0, x1=x1, y0=y0, y1=y1, line_width=line_width, line_color=line_color) self.plot.add_glyph(source, seg) self.plot.yaxis.ticker = FixedTicker(ticks=[0, 1]) self.plot.yaxis.major_label_overrides = {'0': 'bad', '1': 'good'} return self
thing=[(np.random.random(contrast.shape)-0.5)/CNR+baseline] #set up data object to hand to the plot sourceLine=ColumnDataSource(dict( xs=[[0,1],contrast,contrast,contrast], ys=[[baseline,baseline],thing,response, response_plus_noise] )) p1 = Figure(plot_width=300, plot_height=300, title='CNR = %2.1f' %CNRslider.value, title_text_font_size='14pt', x_range=[0.0, 1.0], y_range=[-0.5, 1.5]) #oh, try this next--it's even simpler #http://thankcoder.com/questions/j8zde/interacting-with-multi-line-in-bokeh lineGlyph=MultiLine(xs="xs", ys="ys") p1.add_glyph(sourceLine,lineGlyph) p2 = Figure(plot_width=300, plot_height=300, title='simulated results (n=%d)' %nRep, title_text_font_size='14pt', x_range=[0.05, 0.35], y_range=[-0.2, 1.0]) curdoc().add_root(hplot(vplot(CNRslider,redrawButton),p1,p2)) #vform, from bokeh.io also works in there ## Alternate method of formatting page: #inputs=VBox(children=[redrawButton,CNRslider]) #figs=HBox(children=[p1,p2]) #curdoc().add_root(HBox(children=[inputs,figs],width=800)) def update(attrname,old,new): CNR = CNRslider.value
text_font_size='9pt', text_font_style='bold', text_color='white') glyph4 = Text(x=0.25, y=-0.845, text="label", text_font_size='9pt', text_font_style='bold', text_color='white') glyph5 = Text(x=0.255, y=-0.19, text="label", text_font_size='9pt', text_font_style='bold', text_color='white') snr_plot.add_glyph(textlabel, glyph2) snr_plot.add_glyph(textlabel, glyph3) snr_plot.add_glyph(textlabel, glyph4) snr_plot.add_glyph(textlabel, glyph5) snr_plot.add_glyph(textlabel, glyph) #hovertool hover = snr_plot.select(dict(type=HoverTool)) from math import log #logobj = [log(y,10) for y in co] #logobj = [round(y,4) for y in logobj] #logobj = np.asarray(logobj) hover.tooltips = [('object', '@co{int}'), ('zodi', '@cz{int}'), ('dark current', '@cD{int}'), ('read noise', '@cR{int}'), ('thermal', '@cth{int}')]
###### MAIN PLOT (beam, supports, load) ###### # define plot: plot_main = Figure(title="Rod with Supports and Load", tools="", x_range=x_range, y_range=(-2.0,2.5), height=fig_height) # set properties plot_main.axis.visible = False plot_main.outline_line_width = 2 plot_main.outline_line_color = "Black" plot_main.title.text_font_size = "13pt" plot_main.toolbar.logo = None # plot error message if both supports are set to sliding error_label = LabelSet(x='x', y='y', text='name', source=error_msg) plot_main.add_layout(error_label) plot_main.add_glyph(error_msg_frame,Rect(x="x", y="y", width=8, height=1, angle=0, fill_color='red', fill_alpha=0.2)) # plot helper line plot_main.add_glyph(aux_line, aux_line_glyph) # measures beam_measure_doublearrow_glyph = Arrow(start=OpenHead(line_color=c_black, line_width=2, size=8), end=OpenHead(line_color='black',line_width= 2, size=8), x_start=xr_start, y_start=-1.2, x_end=xr_end, y_end=-1.2, line_width=5, line_color=c_black) beam_measure_singlearrow_glyph = Arrow(end=NormalHead(fill_color=c_gray, line_width=1, size=6), x_start=xr_start-0.1, y_start=-0.8, x_end=xr_start+0.8, y_end=-0.8, line_width=1, line_color=c_black) beam_measure_label_source.data = dict(x=[xr_start+0.25, 0.5*(xr_end-xr_start)], y=[-0.7,-1.6], text=["x","L"]) beam_measure_label = LatexLabelSet(x='x', y='y', text='text', source=beam_measure_label_source, level='glyph')#, x_offset=3, y_offset=-15) plot_main.line(x=[xr_start, xr_start], y=[-0.7,-0.9], line_color=c_black) # vertical line for single x-arrow plot_main.add_layout(beam_measure_singlearrow_glyph) plot_main.add_layout(beam_measure_doublearrow_glyph)
class AppView(object): def __init__(self, app_model): self.model = app_model self.create_layout() def create_layout(self): # create figure self.x_range = Range1d(start=self.model.map_extent[0], end=self.model.map_extent[2], bounds=None) self.y_range = Range1d(start=self.model.map_extent[1], end=self.model.map_extent[3], bounds=None) self.fig = Figure(tools='wheel_zoom,pan', x_range=self.x_range, lod_threshold=None, plot_width=self.model.plot_width, plot_height=self.model.plot_height, y_range=self.y_range) self.fig.min_border_top = 0 self.fig.min_border_bottom = 10 self.fig.min_border_left = 0 self.fig.min_border_right = 0 self.fig.axis.visible = False self.fig.xgrid.grid_line_color = None self.fig.ygrid.grid_line_color = None # add tiled basemap self.tile_source = WMTSTileSource(url=self.model.basemap) self.tile_renderer = TileRenderer(tile_source=self.tile_source) self.fig.renderers.append(self.tile_renderer) # add datashader layer self.image_source = ImageSource(url=self.model.service_url, extra_url_vars=self.model.shader_url_vars) self.image_renderer = DynamicImageRenderer(image_source=self.image_source) self.fig.renderers.append(self.image_renderer) # add label layer self.label_source = WMTSTileSource(url=self.model.labels_url) self.label_renderer = TileRenderer(tile_source=self.label_source) self.fig.renderers.append(self.label_renderer) # Add a hover tool self.invisible_square = Square(x='x', y='y', fill_color=None, line_color=None, size=self.model.hover_size) self.visible_square = Square(x='x', y='y', fill_color='#79DCDE', fill_alpha=.5, line_color='#79DCDE', line_alpha=1, size=self.model.hover_size) cr = self.fig.add_glyph(self.model.hover_source, self.invisible_square, selection_glyph=self.visible_square, nonselection_glyph=self.invisible_square) code = "source.set('selected', cb_data['index']);" callback = CustomJS(args={'source': self.model.hover_source}, code=code) self.model.hover_tool = HoverTool(tooltips=[(self.model.fields.keys()[0], "@value")], callback=callback, renderers=[cr], mode='mouse') self.fig.add_tools(self.model.hover_tool) self.model.legend_side_vbox = VBox() self.model.legend_bottom_vbox = VBox() # add ui components controls = [] axes_select = Select.create(name='Axes', options=self.model.axes) axes_select.on_change('value', self.on_axes_change) controls.append(axes_select) self.field_select = Select.create(name='Field', options=self.model.fields) self.field_select.on_change('value', self.on_field_change) controls.append(self.field_select) self.aggregate_select = Select.create(name='Aggregate', options=self.model.aggregate_functions) self.aggregate_select.on_change('value', self.on_aggregate_change) controls.append(self.aggregate_select) transfer_select = Select.create(name='Transfer Function', options=self.model.transfer_functions) transfer_select.on_change('value', self.on_transfer_function_change) controls.append(transfer_select) color_ramp_select = Select.create(name='Color Ramp', options=self.model.color_ramps) color_ramp_select.on_change('value', self.on_color_ramp_change) controls.append(color_ramp_select) spread_size_slider = Slider(title="Spread Size (px)", value=0, start=0, end=10, step=1) spread_size_slider.on_change('value', self.on_spread_size_change) controls.append(spread_size_slider) hover_size_slider = Slider(title="Hover Size (px)", value=8, start=4, end=30, step=1) hover_size_slider.on_change('value', self.on_hover_size_change) controls.append(hover_size_slider) controls.append(self.model.legend_side_vbox) # add map components basemap_select = Select.create(name='Basemap', value='Imagery', options=self.model.basemaps) basemap_select.on_change('value', self.on_basemap_change) image_opacity_slider = Slider(title="Opacity", value=100, start=0, end=100, step=1) image_opacity_slider.on_change('value', self.on_image_opacity_slider_change) basemap_opacity_slider = Slider(title="Basemap Opacity", value=100, start=0, end=100, step=1) basemap_opacity_slider.on_change('value', self.on_basemap_opacity_slider_change) show_labels_chk = CheckboxGroup(labels=["Show Labels"], active=[0]) show_labels_chk.on_click(self.on_labels_change) map_controls = [basemap_select, basemap_opacity_slider, image_opacity_slider, show_labels_chk] self.controls = VBox(width=200, height=600, children=controls) self.map_controls = HBox(width=self.fig.plot_width, children=map_controls) self.map_area = VBox(width=self.fig.plot_width, children=[self.map_controls, self.fig, self.model.legend_bottom_vbox]) self.layout = HBox(width=1366, children=[self.controls, self.map_area]) def update_image(self): self.model.shader_url_vars['cachebust'] = str(uuid.uuid4()) self.image_renderer.image_source = ImageSource(url=self.model.service_url, extra_url_vars=self.model.shader_url_vars) def on_field_change(self, attr, old, new): self.model.field_title = new self.model.field = self.model.fields[new] self.update_image() if not self.model.field: self.aggregate_select.options = [dict(name="No Aggregates Available", value="")] elif self.model.field in self.model.categorical_fields: self.aggregate_select.options = [dict(name="Categorical", value="count_cat")] else: opts = [dict(name=k, value=k) for k in self.model.aggregate_functions.keys()] self.aggregate_select.options = opts def on_basemap_change(self, attr, old, new): self.model.basemap = self.model.basemaps[new] self.tile_renderer.tile_source = WMTSTileSource(url=self.model.basemap) def on_hover_size_change(self, attr, old, new): self.model.hover_size = int(new) self.invisible_square.size = int(new) self.visible_square.size = int(new) self.model.update_hover() def on_spread_size_change(self, attr, old, new): self.model.spread_size = int(new) self.update_image() def on_axes_change(self, attr, old, new): self.model.active_axes = self.model.axes[new] self.update_image() def on_aggregate_change(self, attr, old, new): self.model.agg_function_name = new self.update_image() def on_transfer_function_change(self, attr, old, new): self.model.transfer_function = self.model.transfer_functions[new] self.update_image() def on_color_ramp_change(self, attr, old, new): self.model.color_ramp = self.model.color_ramps[new] self.update_image() def on_image_opacity_slider_change(self, attr, old, new): self.image_renderer.alpha = new / 100 def on_basemap_opacity_slider_change(self, attr, old, new): self.tile_renderer.alpha = new / 100 def on_labels_change(self, new): self.label_renderer.alpha = 1 if new else 0
plot.add_layout(e2_arrow_glyph) plot.add_layout(p1_arrow_glyph) plot.add_layout(p2_arrow_glyph) plot.add_layout(w11_arrow_glyph) plot.add_layout(w12_arrow_glyph) plot.add_layout(w21_arrow_glyph) plot.add_layout(w22_arrow_glyph) #EDIT Start plot.add_layout(w12_11_arrow_glyph) plot.add_layout(w12_12_arrow_glyph) plot.add_layout(w21_11_arrow_glyph) plot.add_layout(w21_12_arrow_glyph) #EDIT End plot.add_glyph(absource, abtext_glyph) plot.add_layout(labels1) plot.add_layout(labels2) #plot.add_layout(labelsn1) #plot.add_layout(labelsn2) plot.add_layout(labelsw1) plot.add_layout(labelsw2) #EDIT Start plot.add_layout(labelsw12) plot.add_layout(labelsw21) #EDIT End ######### ######################################
Min_Y=int(min(source_small.data['y'])-10) Max_Y=int(max(source_small.data['y'])+10) #Range_Y=Range1d(Min_Y,Max_Y) PLOT_WIDTH=500 sourceQuad = ColumnDataSource(dict(left=[min(source_small.data['x'])],top=[Max_Y],right=[max(source_small.data['x'])],bottom=[Min_Y])) toolset = "box_zoom,resize,pan,reset,save,xwheel_zoom" plot_zoomed = Figure(plot_width=1000, plot_height=500, x_axis_type="datetime",tools=toolset, lod_factor=100,lod_interval=1000) plot_zoomed.line('x', 'y',source=source_zoomed, color='navy', alpha=0.5) plot = Figure(plot_width=PLOT_WIDTH, plot_height=250, x_axis_type="datetime",toolbar_location=None,lod_factor=100,lod_interval=1000) plot.line('x', 'y',source=source_small, color='navy', alpha=0.5) plot.y_range.start=Min_Y plot.y_range.end=Max_Y glyph = Quad(left="left", right="right", top="top", bottom="bottom", fill_color="#b3de69", fill_alpha=0.1) plot.add_glyph(sourceQuad, glyph) RangeStartX=0 RangeEndX=1 text_start_x = TextInput(title="X range start", name='x_range_start', value="0") text_end_x = TextInput(title="X range end", name='x_range_end', value="1") text_start_x.on_change('value', update_data) text_end_x.on_change('value', update_data) toolset = "box_zoom,resize,pan,reset,save,x_wheel_zoom" plot_zoomed.x_range.callback = CustomJS(args=dict(xrange=plot_zoomed.x_range,start_x=text_start_x,end_x=text_end_x),code=""" var start = xrange.get("start"); var end = xrange.get("end"); start_x.set("value",start.toString()); end_x.set("value",end.toString());
plot = Figure(plot_width=PLOT_WIDTH, plot_height=250, x_axis_type="datetime", toolbar_location=None, lod_factor=100, lod_interval=1000) plot.line('x', 'y', source=source_small, color='navy', alpha=0.5) plot.y_range.start = Min_Y plot.y_range.end = Max_Y glyph = Quad(left="left", right="right", top="top", bottom="bottom", fill_color="#b3de69", fill_alpha=0.1) plot.add_glyph(sourceQuad, glyph) RangeStartX = 0 RangeEndX = 1 text_start_x = TextInput(title="X range start", name='x_range_start', value="0") text_end_x = TextInput(title="X range end", name='x_range_end', value="1") text_start_x.on_change('value', update_data) text_end_x.on_change('value', update_data) toolset = "box_zoom,resize,pan,reset,save,x_wheel_zoom" plot_zoomed.x_range.callback = CustomJS(args=dict(xrange=plot_zoomed.x_range, start_x=text_start_x, end_x=text_end_x),