def std_distance_time_plot(nx, ny, source, x_range=None, output_backend=DEFAULT_BACKEND): # avoid range errors if len(source[N.ACTIVE_TIME_S].dropna()) < 2: return None groups = [ group for statistic, group in related_statistics(source, N.ACTIVE_TIME) ] if not groups: # original monochrome plot return multi_dot_plot(nx, ny, N.TIME, [N.ACTIVE_TIME_H, N.ACTIVE_DISTANCE_KM], source, ['black', 'grey'], alphas=[1, 0.5], x_range=x_range, rescale=True) times = [f'{N.ACTIVE_TIME}:{group}' for group in groups] distances = [f'{N.ACTIVE_DISTANCE}:{group}' for group in groups] time_y_range = make_range(source[N.ACTIVE_TIME_H]) distance_y_range = make_range(source[N.ACTIVE_DISTANCE_KM]) colours = list(evenly_spaced_hues(len(groups))) tooltip_names = [ N.ACTIVE_TIME_H, N.ACTIVE_DISTANCE_KM, N.ACTIVITY_GROUP, N.LOCAL_TIME ] tooltip_names += [ name for name in like(N._delta(N.FITNESS_ANY), source.columns) if ':' not in name ] tooltip_names += [ name for name in like(N._delta(N.FATIGUE_ANY), source.columns) if ':' not in name ] tools = [ PanTool(dimensions='width'), ZoomInTool(dimensions='width'), ZoomOutTool(dimensions='width'), ResetTool(), HoverTool(tooltips=[tooltip(name) for name in tooltip_names], names=['with_hover']) ] f = figure(output_backend=output_backend, plot_width=nx, plot_height=ny, x_axis_type='datetime', tools=tools) f.yaxis.axis_label = f'lines - {N.ACTIVE_TIME_H}' f.y_range = time_y_range f.extra_y_ranges = {N.ACTIVE_DISTANCE: distance_y_range} f.add_layout( LinearAxis(y_range_name=N.ACTIVE_DISTANCE, axis_label=f'dots - {N.ACTIVE_DISTANCE_KM}'), 'right') plotter = comb_plotter() for time, colour, group in zip(times, colours, groups): time_h = N._slash(time, U.H) source[time_h] = source[time] / 3600 source[N.ACTIVITY_GROUP] = group plotter(f, x=N.TIME, y=time_h, source=source, color=colour, alpha=1) plotter = dot_plotter() for distance, colour, group in zip(distances, colours, groups): distance_km = N._slash(distance, U.KM) source[distance_km] = source[distance] source[N.ACTIVITY_GROUP] = group plotter(f, x=N.TIME, y=distance_km, source=source, color=colour, alpha=1, name='with_hover', y_range_name=N.ACTIVE_DISTANCE) f.xaxis.axis_label = N.TIME f.toolbar.logo = None if ny < 300: f.toolbar_location = None if x_range: f.x_range = x_range return f
def plot_data(data_df, connections, year, geoSource_new): data_df_countries = data_df #.drop_duplicates(subset=None, keep='first', inplace=True) connections_df = connections node_source = ColumnDataSource( data_df_countries[["country_id", "Country", "Longitude", "Latitude"]]) edge_source = ColumnDataSource(connections_df[["start", "end"]]) node_renderer = GlyphRenderer(data_source=node_source, glyph=node_glyph, selection_glyph=node_selection, nonselection_glyph=node_nonselection) ## Create edge_renderer edge_renderer = GlyphRenderer(data_source=edge_source, glyph=edge_glyph, hover_glyph=edge_hover, selection_glyph=edge_selection, nonselection_glyph=edge_nonselection) ## Create layout_provider graph_layout = dict( zip(data_df_countries.country_id.astype(str), zip(data_df_countries.Longitude, data_df_countries.Latitude))) layout_provider = StaticLayoutProvider(graph_layout=graph_layout) ## Create graph renderer graph = GraphRenderer(edge_renderer=edge_renderer, node_renderer=node_renderer, layout_provider=layout_provider, inspection_policy=NodesAndLinkedEdges(), selection_policy=NodesAndLinkedEdges()) plot = Plot(x_range=Range1d(-150, 150), y_range=Range1d(15, 75), plot_width=800, plot_height=600, background_fill_color=Set3_12[4], background_fill_alpha=0.2) plot.title.text = "Human Trafficing Visualization for " + str(year) # plot.add_glyph( geoSource_data, Patches(xs='xs', ys='ys', line_color='grey' # , line_width=.5, fill_color=Set3_12[6], fill_alpha=0.25)) plot.add_glyph( geoSource_new, Patches(xs='xs', ys='ys', line_color='grey', line_width=.2, fill_color={ 'field': 'Tier', 'transform': mapper2 }, fill_alpha=0.25)) plot.renderers.append(graph) plot.add_layout(LinearAxis(axis_label="Latitude"), "below") plot.add_layout(LinearAxis(axis_label="Longitude"), "left") hover = HoverTool( show_arrow=True, # tooltips= # [("Country Involved: ", "@Country")], tooltips=""" <div> <div> <span style="font-size: 15px;">Country Information </span> <span style="font-size: 12px; color: #696;">@Destination_Country </span> </div> </div> """, renderers=[graph]) hover_no_tooltips = HoverTool(tooltips=None, renderers=[graph]) box_zoom = BoxZoomTool() plot.add_tools(hover, hover_no_tooltips, box_zoom, TapTool(), BoxSelectTool(), ResetTool(), WheelZoomTool()) plot.toolbar.active_inspect = [hover, hover_no_tooltips] plot.toolbar.active_drag = box_zoom plot.outline_line_color = "navy" plot.outline_line_alpha = 0.3 plot.outline_line_width = 3 plot.add_tile(STAMEN_TONER_LABELS) return plot
#Plotting flower species #Importing libraries from bokeh.plotting import figure from bokeh.io import output_file, show from bokeh.sampledata.iris import flowers from bokeh.models import Range1d, PanTool, ResetTool, HoverTool #Define the output file path output_file("iris.html") #Create the figure object f=figure() #Style the tools f.tools=[PanTool(),ResetTool()] hover=HoverTool(tooltips=[("Species","@species"),("Sepal Width","@sepal_width")]) f.add_tools(hover) f.toolbar_location='above' f.toolbar.logo=None #Style the plot area f.plot_width=1100 f.plot_height=650 f.background_fill_color="olive" f.background_fill_alpha=0.3 #Style the title f.title.text="Iris Morphology" f.title.text_color="olive" f.title.text_font="Agency FB"
def _make_graph_plot(self): """ Builds the graph portion of the final model. """ import networkx as nx nodes = nx.nx_pydot.graphviz_layout(self._graph, prog="dot") node_x, node_y = zip(*nodes.values()) models = [self._graph.nodes[x]["model"] for x in nodes] node_id = list(nodes.keys()) node_source = ColumnDataSource({ "x": node_x, "y": node_y, "index": node_id, "model": models }) edge_x_coords = [] edge_y_coords = [] for start_node, end_node in self._graph.edges: edge_x_coords.extend([[nodes[start_node][0], nodes[end_node][0]]]) edge_y_coords.extend([[nodes[start_node][1], nodes[end_node][1]]]) edge_source = ColumnDataSource({ "xs": edge_x_coords, "ys": edge_y_coords }) p2 = Plot(outline_line_alpha=0.0) xinterval = max(max(node_x) - min(node_x), 200) yinterval = max(max(node_y) - min(node_y), 200) p2.x_range = Range1d(start=min(node_x) - 0.15 * xinterval, end=max(node_x) + 0.15 * xinterval) p2.y_range = Range1d(start=min(node_y) - 0.15 * yinterval, end=max(node_y) + 0.15 * yinterval) node_renderer = GlyphRenderer( data_source=node_source, glyph=Circle(x="x", y="y", size=15, fill_color="lightblue"), nonselection_glyph=Circle(x="x", y="y", size=15, fill_color="lightblue"), selection_glyph=Circle(x="x", y="y", size=15, fill_color="green"), ) edge_renderer = GlyphRenderer(data_source=edge_source, glyph=MultiLine(xs="xs", ys="ys")) node_hover_tool = HoverTool(tooltips=[("id", "@index"), ("model", "@model")]) node_hover_tool.renderers = [node_renderer] tap_tool = TapTool() tap_tool.renderers = [node_renderer] labels = LabelSet( x="x", y="y", text="model", source=node_source, text_font_size="8pt", x_offset=-20, y_offset=7, ) help = Label( x=20, y=20, x_units="screen", y_units="screen", text_font_size="8pt", text_font_style="italic", text="Click on a model to see its attributes", ) p2.add_layout(help) p2.add_layout(edge_renderer) p2.add_layout(node_renderer) p2.tools.extend( [node_hover_tool, tap_tool, BoxZoomTool(), ResetTool(), PanTool()]) p2.renderers.append(labels) self._node_source = node_source self._edge_source = edge_source return p2
def create(self, request, symbol=None): """ """ if not symbol: return Response(json='Company not found', status=404) url = f'https://api.iextrading.com/1.0/stock/{symbol}/chart/5y' data = requests.get(url) if not data: return Response(json='Invalid Company', status=404) import pdb pdb.set_trace() df = pd.DataFrame(data) seqs = np.arange(df.shape[0]) df['seqs'] = pd.Series(seqs) df['changePercent'] = df['changePercent'].apply(lambda x: str(x) + '%') df['mid'] = df.apply(lambda x: (x['open'] + x['close']) / 2, axis=1) df['height'] = df.apply(lambda x: x['close'] - x['open'] if x['close'] != x['open'] else 0.001, axis=1) inc = df.close > df.open dec = df.close < df.open w = .3 sourceInc = bk.ColumnDataSource(df.loc[inc]) sourceDec = bk.ColumnDataSource(df.loc[dec]) hover = HoverTool( tooltips=[('date', '@date'), ('low', '@low'), ( 'high', '@high'), ('open', '@open'), ('close', '@close'), ('percent', '@changePercent')]) TOOLS = [ hover, BoxZoomTool(), PanTool(), ZoomInTool(), ZoomOutTool(), ResetTool() ] p = bk.figure(plot_width=1500, plot_height=800, tools=TOOLS, title=f'{symbol}', toolbar_location='above') p.xaxis.major_label_orientation = np.pi / 4 p.grid.grid_line_alpha = w descriptor = Label(x=70, y=70, text='Past 5 years') p.add_layout(descriptor) p.segment(df.seqs[inc], df.high[inc], df.seqs[inc], df.low[inc], color='green') p.segment(df.seqs[dec], df.high[dec], df.seqs[dec], df.low[dec], color='red') p.rect(x='seqs', y='mid', width=w, height='height', fill_color='green', line_color='green', source=sourceInc) p.rect(x='seqs', y='mid', width=w, height='height', fill_color='red', line_color='red', source=sourceDec) bk.save(p, f'./static/{symbol}candle_stick.html', filename=f'5yr_candlestock') return Response(json='Data Created', status=200)
def create(palm): fit_max = 1 fit_min = 0 doc = curdoc() # THz calibration plot scan_plot = Plot( title=Title(text="THz calibration"), x_range=DataRange1d(), y_range=DataRange1d(), plot_height=PLOT_CANVAS_HEIGHT, plot_width=PLOT_CANVAS_WIDTH, toolbar_location="right", ) # ---- tools scan_plot.toolbar.logo = None scan_plot.add_tools(PanTool(), BoxZoomTool(), WheelZoomTool(), ResetTool()) # ---- axes scan_plot.add_layout(LinearAxis(axis_label="Stage delay motor"), place="below") scan_plot.add_layout(LinearAxis(axis_label="Energy shift, eV", major_label_orientation="vertical"), place="left") # ---- grid lines scan_plot.add_layout(Grid(dimension=0, ticker=BasicTicker())) scan_plot.add_layout(Grid(dimension=1, ticker=BasicTicker())) # ---- circle cluster glyphs scan_circle_source = ColumnDataSource(dict(x=[], y=[])) scan_plot.add_glyph(scan_circle_source, Circle(x="x", y="y", line_alpha=0, fill_alpha=0.5)) # ---- circle glyphs scan_avg_circle_source = ColumnDataSource(dict(x=[], y=[])) scan_plot.add_glyph( scan_avg_circle_source, Circle(x="x", y="y", line_color="purple", fill_color="purple")) # ---- line glyphs fit_line_source = ColumnDataSource(dict(x=[], y=[])) scan_plot.add_glyph(fit_line_source, Line(x="x", y="y", line_color="purple")) # THz calibration folder path text input def path_textinput_callback(_attr, _old_value, _new_value): update_load_dropdown_menu() path_periodic_update() path_textinput = TextInput(title="THz calibration path:", value=os.path.join(os.path.expanduser("~")), width=510) path_textinput.on_change("value", path_textinput_callback) # THz calibration eco scans dropdown def scans_dropdown_callback(_attr, _old_value, new_value): scans_dropdown.label = new_value scans_dropdown = Dropdown(label="ECO scans", button_type="default", menu=[]) scans_dropdown.on_change("value", scans_dropdown_callback) # ---- eco scans periodic update def path_periodic_update(): new_menu = [] if os.path.isdir(path_textinput.value): for entry in os.scandir(path_textinput.value): if entry.is_file() and entry.name.endswith(".json"): new_menu.append((entry.name, entry.name)) scans_dropdown.menu = sorted(new_menu, reverse=True) doc.add_periodic_callback(path_periodic_update, 5000) # Calibrate button def calibrate_button_callback(): palm.calibrate_thz( path=os.path.join(path_textinput.value, scans_dropdown.value)) fit_max_spinner.value = np.ceil(palm.thz_calib_data.index.values.max()) fit_min_spinner.value = np.floor( palm.thz_calib_data.index.values.min()) update_calibration_plot() def update_calibration_plot(): scan_plot.xaxis.axis_label = f"{palm.thz_motor_name}, {palm.thz_motor_unit}" scan_circle_source.data.update( x=np.repeat(palm.thz_calib_data.index, palm.thz_calib_data["peak_shift"].apply(len)).tolist(), y=np.concatenate( palm.thz_calib_data["peak_shift"].values).tolist(), ) scan_avg_circle_source.data.update( x=palm.thz_calib_data.index.tolist(), y=palm.thz_calib_data["peak_shift_mean"].tolist()) x = np.linspace(fit_min, fit_max, 100) y = palm.thz_slope * x + palm.thz_intersect fit_line_source.data.update(x=np.round(x, decimals=5), y=np.round(y, decimals=5)) calib_const_div.text = f""" thz_slope = {palm.thz_slope} """ calibrate_button = Button(label="Calibrate THz", button_type="default", width=250) calibrate_button.on_click(calibrate_button_callback) # THz fit maximal value text input def fit_max_spinner_callback(_attr, old_value, new_value): nonlocal fit_max if new_value > fit_min: fit_max = new_value palm.calibrate_thz( path=os.path.join(path_textinput.value, scans_dropdown.value), fit_range=(fit_min, fit_max), ) update_calibration_plot() else: fit_max_spinner.value = old_value fit_max_spinner = Spinner(title="Maximal fit value:", value=fit_max, step=0.1) fit_max_spinner.on_change("value", fit_max_spinner_callback) # THz fit maximal value text input def fit_min_spinner_callback(_attr, old_value, new_value): nonlocal fit_min if new_value < fit_max: fit_min = new_value palm.calibrate_thz( path=os.path.join(path_textinput.value, scans_dropdown.value), fit_range=(fit_min, fit_max), ) update_calibration_plot() else: fit_min_spinner.value = old_value fit_min_spinner = Spinner(title="Minimal fit value:", value=fit_min, step=0.1) fit_min_spinner.on_change("value", fit_min_spinner_callback) # Save calibration button def save_button_callback(): palm.save_thz_calib(path=path_textinput.value) update_load_dropdown_menu() save_button = Button(label="Save", button_type="default", width=250) save_button.on_click(save_button_callback) # Load calibration button def load_dropdown_callback(_attr, _old_value, new_value): palm.load_thz_calib(os.path.join(path_textinput.value, new_value)) update_calibration_plot() def update_load_dropdown_menu(): new_menu = [] calib_file_ext = ".palm_thz" if os.path.isdir(path_textinput.value): for entry in os.scandir(path_textinput.value): if entry.is_file() and entry.name.endswith((calib_file_ext)): new_menu.append( (entry.name[:-len(calib_file_ext)], entry.name)) load_dropdown.button_type = "default" load_dropdown.menu = sorted(new_menu, reverse=True) else: load_dropdown.button_type = "danger" load_dropdown.menu = new_menu doc.add_next_tick_callback(update_load_dropdown_menu) doc.add_periodic_callback(update_load_dropdown_menu, 5000) load_dropdown = Dropdown(label="Load", menu=[], width=250) load_dropdown.on_change("value", load_dropdown_callback) # Calibration constants calib_const_div = Div(text=f""" thz_slope = {0} """) # assemble tab_layout = column( row( scan_plot, Spacer(width=30), column( path_textinput, scans_dropdown, calibrate_button, fit_max_spinner, fit_min_spinner, row(save_button, load_dropdown), calib_const_div, ), )) return Panel(child=tab_layout, title="THz Calibration")
def makeplot_2(df, FC_P, PV_P, Inhibitor): df = df[df['Kinase'] != ''] # Drop of data with no Kinase allocated df.loc[(df['Fold_change'] > FC_P) & (df['p_value'] < PV_P), 'color'] = "Blue" # upregulated #df.loc - Selects single row or subset of rows from the DataFrame by label df.loc[(df['Fold_change'] <= FC_P) & (df['p_value'] < PV_P), 'color'] = "Purple" # downregulated df['color'].fillna('grey', inplace=True) df["log_pvalue"] = -np.log10(df['p_value']) df["log_FC"] = np.log2(df['Fold_change']) df.head() output_notebook() category = 'Substrate' category_items = df[category].unique() title = Inhibitor + " :Data with identified kinases" #feeding data into ColumnDataSource source = ColumnDataSource(df) #Editing the hover that need to displayed while hovering hover = HoverTool( tooltips=[('Kinase', '@Kinase'), ('Substrate', '@Substrate'), ('Sub_gene', '@Sub_gene'), ('Phosphosite', '@Phosphosite'), ('Fold_change', '@Fold_change'), ('p_value', '@p_value')]) #tools that are need to explote data tools = [ hover, WheelZoomTool(), PanTool(), BoxZoomTool(), ResetTool(), SaveTool() ] #finally making figure with scatter plot p = figure( tools=tools, title=title, plot_width=700, plot_height=400, toolbar_location='right', toolbar_sticky=False, ) p.scatter(x='log_FC', y='log_pvalue', source=source, size=10, color='color') #displaying the graph return (p)
def _heatmap_summary(pvals, coefs, plot_width=1200, plot_height=400): """ Plots heatmap of coefficients colored by pvalues Parameters ---------- pvals : pd.DataFrame Table of pvalues where rows are balances and columns are covariates. coefs : pd.DataFrame Table of coefficients where rows are balances and columns are covariates. plot_width : int, optional Width of plot. plot_height : int, optional Height of plot. Returns ------- bokeh.charts.Heatmap Heatmap summarizing the regression statistics. """ c = coefs.reset_index() c = c.rename(columns={'index': 'balance'}) # fix alpha in fdr to account for the number of covariates def fdr(x): return multipletests(x, method='fdr_bh', alpha=0.05 / pvals.shape[1])[1] cpvals = pvals.apply(fdr, axis=0) # log scale for coloring log_p = -np.log10(cpvals + 1e-200) log_p = log_p.reset_index() log_p = log_p.rename(columns={'index': 'balance'}) p = pvals.reset_index() p = p.rename(columns={'index': 'balance'}) cp = cpvals.reset_index() cp = cp.rename(columns={'index': 'balance'}) cm = pd.melt(c, id_vars='balance', var_name='Covariate', value_name='Coefficient') pm = pd.melt(p, id_vars='balance', var_name='Covariate', value_name='Pvalue') cpm = pd.melt(cp, id_vars='balance', var_name='Covariate', value_name='Corrected_Pvalue') logpm = pd.melt(log_p, id_vars='balance', var_name='Covariate', value_name='log_Pvalue') m = pd.merge(cm, pm, left_on=['balance', 'Covariate'], right_on=['balance', 'Covariate']) m = pd.merge(m, logpm, left_on=['balance', 'Covariate'], right_on=['balance', 'Covariate']) m = pd.merge(m, cpm, left_on=['balance', 'Covariate'], right_on=['balance', 'Covariate']) hover = HoverTool(tooltips=[( "Pvalue", "@Pvalue"), ("Corrected Pvalue", "@Corrected_Pvalue"), ("Coefficient", "@Coefficient")]) N, _min, _max = len(palette), m.log_Pvalue.min(), m.log_Pvalue.max() X = pd.Series(np.arange(len(pvals.index)), index=pvals.index) Y = pd.Series(np.arange(len(pvals.columns)), index=pvals.columns) m['X'] = [X.loc[i] for i in m.balance] m['Y'] = [Y.loc[i] for i in m.Covariate] # fill in nans with zero. Sometimes the pvalue calculation fails. m = m.fillna(0) for i in m.index: x = m.loc[i, 'log_Pvalue'] ind = int(np.floor((x - _min) / (_max - _min) * (N - 1))) m.loc[i, 'color'] = palette[ind] source = ColumnDataSource(ColumnDataSource.from_df(m)) hm = figure(title='Regression Coefficients Summary', plot_width=1200, plot_height=400, tools=[ hover, PanTool(), BoxZoomTool(), WheelZoomTool(), ResetTool(), SaveTool() ]) hm.rect(x='X', y='Y', width=1, height=1, fill_color='color', line_color="white", source=source) Xlabels = pd.Series(pvals.index, index=np.arange(len(pvals.index))) Ylabels = pd.Series( pvals.columns, index=np.arange(len(pvals.columns)), ) hm.xaxis[0].ticker = FixedTicker(ticks=Xlabels.index) hm.xaxis.formatter = FuncTickFormatter(code=""" var labels = %s; return labels[tick]; """ % Xlabels.to_dict()) hm.yaxis[0].ticker = FixedTicker(ticks=Ylabels.index) hm.yaxis.formatter = FuncTickFormatter(code=""" var labels = %s; return labels[tick]; """ % Ylabels.to_dict()) return hm
def make_weight_graph(stock_name=None): """ """ try: API_URL = 'https://api.iextrading.com/1.0' res = requests.get(f'{ API_URL }/stock/{ stock_name }/chart/5y') data = res.json() except JSONDecodeError: abort(404) df = pd.DataFrame(data) # df['date'] = pd.to_datetime(df['date']) df['adjVolume'] = 6 * df['volume'] // df['volume'].mean() df = df[['date', 'vwap', 'volume', 'adjVolume']] seqs = np.arange(df.shape[0]) df['seqs'] = pd.Series(seqs) source = ColumnDataSource(df) colors = [ "#75968f", "#a5bab7", "#c9d9d3", "#e2e2e2", "#dfccce", "#ddb7b1", "#cc7878", "#933b41", "#550b1d" ] mapper = LinearColorMapper(palette=colors, low=df.adjVolume.min(), high=df.adjVolume.max()) hover = HoverTool(tooltips=[ ('Date', '@date'), ('Vwap', '@vwap'), ('Volume', '@volume'), ], names=["circle"]) TOOLS = [ hover, BoxZoomTool(), PanTool(), ZoomInTool(), ZoomOutTool(), ResetTool() ] p = figure(plot_width=1000, plot_height=800, title=f'5 year weighted performace of {stock_name}', tools=TOOLS, toolbar_location='above') p.circle(x="seqs", y="vwap", source=source, size='adjVolume', fill_color=transform('adjVolume', mapper), name="circle") color_bar = ColorBar(color_mapper=mapper, location=(0, 0), ticker=BasicTicker(desired_num_ticks=len(colors)), formatter=PrintfTickFormatter(format="%s%%")) p.add_layout(color_bar, 'right') p.axis.axis_line_color = None p.axis.major_tick_line_color = None p.axis.major_label_text_font_size = "5pt" p.axis.major_label_standoff = 0 p.xaxis.major_label_orientation = 1.0 script, div = components(p) return script, div, stock_name
def plot_total_all_time(result): color_palette = ["#EF476F", "#FFD166", "#06D6A0", "#118AB2", "#073B4C"] x = [] y = [] colors = [] for participant, value, color in zip(result.keys(), result.values(), color_palette): x.append(participant) y.append(value["total_all_time"]) colors.append(color) tools = [ WheelZoomTool(), PanTool(), BoxZoomTool(), ResetTool(), SaveTool() ] fig = figure( x_range=x, plot_height=300, plot_width=300, sizing_mode="scale_height", title="Total messages sent", toolbar_location="below", tools=tools, toolbar_sticky=False, background_fill_color="beige", background_fill_alpha=0.25, ) data = {"x_values": x, "y_values": y, "color_values": colors} source = ColumnDataSource(data=data) fig.vbar(x="x_values", top="y_values", width=0.8, color="color_values", legend_field="x_values", source=source) # adds the count above the bar chart labels = LabelSet(x='x_values', y='y_values', text='y_values', level='glyph', x_offset=-13.5, y_offset=0, source=source, render_mode='canvas') fig.add_layout(labels) # title styling fig.title.align = "center" fig.title.text_font_size = "14pt" # y-axis styling fig.yaxis.axis_label = "Count" fig.yaxis.axis_label_text_font_size = "12pt" fig.yaxis.axis_label_text_font_style = "bold" fig.y_range.start = 0 # x axis styling fig.xaxis.axis_label = "Sender" fig.xaxis.axis_label_text_font_size = "12pt" fig.xaxis.axis_label_text_font_style = "bold" # legend styling fig.legend.location = "top_left" # shows the actual numerical value instead of scientific fig.left[0].formatter.use_scientific = False output_file("total_messages_sent.html") return fig
def multi_plot(nx, ny, x, ys, source, colors, alphas=None, x_range=None, y_label=None, rescale=False, plotters=None): if not ys or not present(source, x, *ys): return None tools = [ PanTool(dimensions='width'), ZoomInTool(dimensions='width'), ZoomOutTool(dimensions='width'), ResetTool(), HoverTool(tooltips=[tooltip(x) for x in ys + [LOCAL_TIME]], names=['with_hover']) ] f = figure(plot_width=nx, plot_height=ny, x_axis_type='datetime' if TIME in x else 'linear', tools=tools) if y_label: f.yaxis.axis_label = y_label elif rescale: f.yaxis.axis_label = ys[0] else: f.yaxis.axis_label = ', '.join(ys) if rescale: f.extra_y_ranges = {} if alphas is None: alphas = [1 for _ in ys] while len(plotters) < len(ys): plotters += plotters for y, color, alpha, plotter in zip(ys, colors, alphas, plotters): y_range = make_range(source, y) if rescale and y != ys[0]: f.extra_y_ranges[y] = y_range f.add_layout(LinearAxis(y_range_name=y, axis_label=y), 'right') plotter(f, x=x, y=y, source=source, color=color, alpha=alpha, y_range_name=y, name='with_hover') else: f.y_range = y_range plotter(f, x=x, y=y, source=source, color=color, alpha=alpha, name='with_hover') f.xaxis.axis_label = x f.toolbar.logo = None if ny < 300: f.toolbar_location = None if x_range: f.x_range = x_range return f
def draw_plot(G, query, expr, type='publications'): # plot plot = figure( x_range=Range1d(-1.1, 1.1), y_range=Range1d(-1.1, 1.1), sizing_mode="stretch_both", tools="") plot.axis.visible = False plot.xgrid.grid_line_color = None plot.ygrid.grid_line_color = None # legend plot.circle( x=[-200000, ], y=[-200000, ], fill_color='white', size=0, line_width=0, legend_label='Visualization for "' + query + '"') plot.circle( x=[-200000, ], y=[-200000, ], fill_color='white', size=0, line_width=0, legend_label='created using Microsoft Academic Graph and') plot.circle( x=[-200000, ], y=[-200000, ], fill_color='white', size=0, line_width=0, legend_label='Sciencegraph by Marcus Ossiander, 2020') if type == 'publications': plot.circle( x=[-200000, ], y=[-200000, ], fill_color=cm2[3], size=20, legend_label='Publication, Color measures Citation Count') plot.circle( x=[-200000, ], y=[-200000, ], fill_color=cm1[3], size=10, legend_label='Reference, Color measures Citation Count') if type == 'authors': plot.circle( x=[-200000, ], y=[-200000, ], fill_color=cm2[3], size=15, legend_label='Publication, Color measures Citation Count') plot.circle( x=[-200000, ], y=[-200000, ], fill_color=cm1[3], size=10, legend_label='Co-Author, Color measures Collaboration') plot.legend.background_fill_alpha = 0 plot.legend.border_line_alpha = 0 plot.legend.location = 'top_left' # tools node_hover_tool = HoverTool(tooltips=tooltips) zoom_tool = WheelZoomTool() div = Div( text='<div id="showpaper" style="position: absolute; display: none; width=500px"></div>', name='showpaper', sizing_mode="stretch_width") tap_tool_open = TapTool() tap_tool_open.callback = CustomJS( args={'tp': showpaper_content}, code=code) help_tool = HelpTool( help_tooltip='Created using Microsoft Academic Graph and Sciencegraph by Marcus Ossiander, 2020', redirect='https://github.com/marcus-o/sciencegraph/') plot.add_tools( node_hover_tool, zoom_tool, BoxZoomTool(), ResetTool(), tap_tool_open, help_tool) plot.toolbar.active_scroll = zoom_tool # graph graph_renderer = from_networkx( G, nx.spring_layout, scale=1, center=(0, 0), seed=12345) # normal graph_renderer.node_renderer.glyph = Circle( size="size", fill_color="color") graph_renderer.edge_renderer.glyph = MultiLine( line_alpha=0.2) # selection graph_renderer.node_renderer.selection_glyph = Circle( fill_color="color", fill_alpha=1, line_alpha=1) graph_renderer.edge_renderer.selection_glyph = MultiLine( line_width=3, line_alpha=1) graph_renderer.node_renderer.nonselection_glyph = Circle( fill_color="color", fill_alpha=0.5, line_alpha=0.5) graph_renderer.edge_renderer.nonselection_glyph = MultiLine( line_alpha=0.2) # hover graph_renderer.node_renderer.hover_glyph = Circle( fill_color='#abdda4') graph_renderer.edge_renderer.hover_glyph = MultiLine( line_color='#abdda4', line_width=3) graph_renderer.inspection_policy = NodesAndLinkedEdges() graph_renderer.selection_policy = NodesAndLinkedEdges() # add everything plot.renderers.append(graph_renderer) script, div = components(Column(children=[plot, div], sizing_mode="stretch_both")) return script, div
def draw_bubble_map(request, start_date: str = None, end_date: str = None, pollutants: list = None): if request is not None: pollutants = request.GET.get('pollutants', ['PM25', 'PM10', 'NO2']) start_date = request.GET.get('start_date', None) end_date = request.GET.get('end_date', None) if start_date is None: return JsonResponse("'start_date' is a required parameter.", safe=False) if end_date is None: end_date = start_date bubble_data = get_target_bubblemap_data(start_date=start_date, end_date=end_date, pollutants=pollutants) # create bokeh elements _tabs = [] for k, v in bubble_data.items(): p = get_nuts_map(0, outline_map=True, include_tools=False, exclude_countries=['TR']) p.name = k # add annotation top = p.properties_with_values().get('plot_height') note1 = Label(x=10, y=50, x_units='screen', y_units='screen', text='NOTE: bubble size denotes Nuts2 Population.', render_mode='canvas', border_line_color=None, border_line_alpha=1.0, text_alpha=.5, text_font_size='12px', background_fill_color=None, background_fill_alpha=0.5) note2 = Label(x=10, y=30, x_units='screen', y_units='screen', text='NOTE: color denotes percentage of target.', render_mode='canvas', border_line_color=None, border_line_alpha=1.0, text_alpha=0.5, text_font_size='12px', background_fill_color=None, background_fill_alpha=0.5) p.add_layout(note1) p.add_layout(note2) _tabs.append(Panel(child=p, title=p.name)) tabs = Tabs(tabs=_tabs) tabs.sizing_mode = 'scale_both' color_mapper = LinearColorMapper(palette=RdYlGn11, low=.5, low_color='green', high=1.5, high_color='red') tick_format = NumeralTickFormatter(format='+0%') color_bar = ColorBar( color_mapper=color_mapper, ticker=FixedTicker(ticks=[0, .25, 0.50, .75, 1, 1.25, 1.50]), formatter=tick_format, label_standoff=9, border_line_color=None, location=(0, 0)) s_zoom = WheelZoomTool() s_pan = PanTool() s_reset = ResetTool() # create the bubbles and hover elements for t in tabs.tabs: # add colorbar t.child.add_layout(color_bar, 'right') # add bubbles glyphs = t.child.scatter(x='x', y='y', size='radius', source=bubble_data.get(t.child.name), fill_alpha=0.6, fill_color={ 'field': 'achievement', 'transform': color_mapper }, line_color=None) # add hover tool for stations hover_tool = HoverTool( renderers=[glyphs], tooltips=[("air_quality_station", "@air_quality_station"), ("Country", "@country_code_id"), ("NUTS 2", "@nuts_2_name"), ("NUTS 2 Pop", "@population"), (f"{t.child.name} Target Value", "@target_value"), ("Avg Value", "@value__avg"), ("% of Target", "@achievement{:+%0.0}")]) t.child.add_tools(hover_tool, s_zoom, s_pan, s_reset) # jupyter notebook # return tabs # django item = json_item(tabs) # item['metadata'] = 'somemetadata' return JsonResponse(item)
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]))
def get_plot(raw, today): dfs, cats = get_sources_and_categories(raw) # Some times first_day = raw.loc[0, 'timestamp'] one_week_ago = today - datetime.timedelta(weeks=1) two_weeks_ago = today - datetime.timedelta(weeks=2) one_week_forward = today + datetime.timedelta(weeks=1) # The ranges all_range = Range1d(start=first_day, end=today) month_range = Range1d(start=two_weeks_ago, end=one_week_forward) week_range = Range1d(start=one_week_ago, end=today) # Selection indicators highlight = Quad( left='start', right='end', bottom=0, top=12, fill_color='white', line_color=COLOR_PRIMARY_CONTRAST, fill_alpha=0.2, ) lowlight = Quad( left='start', right='end', bottom=0, top=12, fill_color=COLOR_PRIMARY, line_color=COLOR_PRIMARY_CONTRAST, fill_alpha=0.5, ) # Make the complete timeline plot all_plot = _make_base_plot(dfs, cats, all_range) detail_selection_source = ColumnDataSource({ 'start': [all_range.start, month_range.end], 'end': [month_range.start, all_range.end] }) all_plot.add_glyph(detail_selection_source, lowlight) # add a second axis to all_layout plot for presentation year_ticker = DatetimeTicker(desired_num_ticks=4) year_ticks = DatetimeTickFormatter( formats={ 'years': ["%Y"], 'months': ["%Y"], 'days': ["%Y"], 'hours': ["%Y"] } ) all_plot.add_layout( DatetimeAxis(formatter=year_ticks, ticker=year_ticker, **AXIS_PROPERTIES), 'below' ) # Make the detail plot detail_plot = _make_base_plot(dfs, cats, month_range) detail_plot.add_tools(PanTool(dimensions=['width'])) detail_plot.add_tools(WheelZoomTool(dimensions=['width'])) detail_plot.add_tools(ResetTool()) week_selection_source = ColumnDataSource({'start': [week_range.start], 'end': [week_range.end]}) detail_plot.add_glyph(week_selection_source, highlight) detail_code = """ // Update the month selection box on the all_data plot when month pans var detail_selection_data = detail_selection_source.get('data'); var detail_start = cb_obj.get('frame').get('x_range').get('start'); var detail_end = cb_obj.get('frame').get('x_range').get('end'); new_detail_selection = { 'start': [detail_selection_data['start'][0], detail_end], 'end': [detail_start, detail_selection_data['end'][1]] }; detail_selection_source.set('data', new_detail_selection); // Always make sure the week highlight box on detail is visible and centered var x = moment.duration(detail_end - detail_start).asWeeks() / 2.4; var start = moment(detail_start); var week_end = start.add(x, 'weeks').format('x'); $("#one_week_before").text(start.format('ddd, DD MMM YYYY')); var newStart = start.format('YYYY-MM-DD'); var week_start = start.add(6, 'days').format('x'); $("#today").text(start.format('ddd, DD MMM YYYY')); new_week_selection = { 'start': [week_start, ], 'end': [week_end, ] }; week_selection_source.set('data', new_week_selection); var url = '/timesheet/?start=' + newStart; $("#timesheet_submit").attr('href', url).addClass("mdl-button--colored"); """ detail_xrange_callback = CustomJS(args={}, code=detail_code) detail_xrange_callback.args['detail_selection_source'] = detail_selection_source detail_xrange_callback.args['week_selection_source'] = week_selection_source detail_plot.x_range.callback = detail_xrange_callback return all_plot, detail_plot
def make_candle_chart(stock_name=None): """ """ try: API_URL = 'https://api.iextrading.com/1.0' res = requests.get(f'{ API_URL }/stock/{ stock_name }/chart/5y') data = res.json() except JSONDecodeError: abort(404) df = pd.DataFrame(data) # df["date"] = pd.to_datetime(df["date"]) # import pdb; pdb.set_trace() seqs = np.arange(df.shape[0]) df['seqs'] = pd.Series(seqs) df['changePercent'] = df['changePercent'].apply(lambda x: str(x) + '%') df['mid'] = df.apply(lambda x: (x['open'] + x['close']) / 2, axis=1) df['height'] = df.apply(lambda x: x['close'] - x['open'] if x['close'] != x['open'] else 0.01, axis=1) inc = df.close > df.open dec = df.close < df.open w = .3 sourceInc = bk.ColumnDataSource(df.loc[inc]) sourceDec = bk.ColumnDataSource(df.loc[dec]) hover = HoverTool(tooltips=[ ('Date', '@date'), ('Low', '@low'), ('High', '@high'), ('Open', '@open'), ('Close', '@close'), ('Percent', '@changePercent'), ], names=["rect1", "rect2"]) TOOLS = [ hover, BoxZoomTool(), PanTool(), ZoomInTool(), ZoomOutTool(), ResetTool() ] p = figure(plot_width=1000, plot_height=800, title=stock_name, tools=TOOLS, toolbar_location='above') p.xaxis.major_label_orientation = np.pi / 4 p.grid.grid_line_alpha = w # descriptor = Label(x=70, y=70, text=f"5-year stock chart of {stock_name}") # p.add_layout(descriptor) p.segment(df.seqs[inc], df.high[inc], df.seqs[inc], df.low[inc], color='green') p.segment(df.seqs[dec], df.high[dec], df.seqs[dec], df.low[dec], color='red') p.rect(x='seqs', y='mid', width=w, height='height', fill_color='red', line_color='red', source=sourceDec, name="rect1") p.rect(x='seqs', y='mid', width=w, height='height', fill_color='green', line_color='green', source=sourceInc, name="rect2") script, div = components(p) return script, div, stock_name
# # relevant_tweet = relevant_tweet[(tweet_dataset.Created_at > start) & (tweet_dataset.Created_at < end)] # use Word2vec # ########### Create Visualizations ################## # # Line graph for trend hover1 = HoverTool(tooltips=[ ("Count", "@y"), ("Time", "@x") ]) src_line_1 = ColumnDataSource(data=dict(x=[], y=[])) plot_trend = figure(title='Trend of Microblogs', plot_width=500, plot_height=250, x_axis_type="datetime", tools=[hover1, ResetTool(), BoxZoomTool()]) line1 = plot_trend.line(x='x', y='y', source=src_line_1, line_width=2, line_color=new_col, legend='Previous users') line1_datasource = line1.data_source line2 = plot_trend.line(x=[], y=[], line_width=2, line_color='Yellow', legend='New users') line2_datasource = line2.data_source plot_trend.legend.location = "top_left" plot_trend.toolbar.logo = None plot_trend.xaxis.axis_label = "Date" plot_trend.yaxis.axis_label = "Relevant Microblog Count" plot_trend.min_border = 0 ts_curr = Span(location=0,dimension='height', line_color='green', line_dash='dashed', line_width=3) plot_trend.renderers.extend([ts_curr]) # Widgets - Search, button button_go = Button(label="Search Revelant Microblogs", width=50, button_type="success")
def __init__(self, **kwargs): self.source = ColumnDataSource( data={ 'time': [], 'cpu': [], 'memory_percent': [], 'network-send': [], 'network-recv': [] }) x_range = DataRange1d(follow='end', follow_interval=30000, range_padding=0) resource_plot = Plot(x_range=x_range, y_range=Range1d(start=0, end=1), toolbar_location=None, min_border_bottom=10, **kwargs) line_opts = dict(line_width=2, line_alpha=0.8) g1 = resource_plot.add_glyph( self.source, Line(x='time', y='memory_percent', line_color="#33a02c", **line_opts)) g2 = resource_plot.add_glyph( self.source, Line(x='time', y='cpu', line_color="#1f78b4", **line_opts)) resource_plot.add_layout( LinearAxis(formatter=NumeralTickFormatter(format="0 %")), 'left') legend_opts = dict(location='top_left', orientation='horizontal', padding=5, margin=5, label_height=5) resource_plot.add_layout( Legend(items=[('Memory', [g1]), ('CPU', [g2])], **legend_opts)) network_plot = Plot(x_range=x_range, y_range=DataRange1d(start=0), toolbar_location=None, **kwargs) g1 = network_plot.add_glyph( self.source, Line(x='time', y='network-send', line_color="#a6cee3", **line_opts)) g2 = network_plot.add_glyph( self.source, Line(x='time', y='network-recv', line_color="#b2df8a", **line_opts)) network_plot.add_layout(DatetimeAxis(axis_label="Time"), "below") network_plot.add_layout(LinearAxis(axis_label="MB/s"), 'left') network_plot.add_layout( Legend(items=[('Network Send', [g1]), ('Network Recv', [g2])], **legend_opts)) tools = [ PanTool(dimensions='width'), WheelZoomTool(dimensions='width'), BoxZoomTool(), ResetTool() ] if 'sizing_mode' in kwargs: sizing_mode = {'sizing_mode': kwargs['sizing_mode']} else: sizing_mode = {} combo_toolbar = ToolbarBox(tools=tools, logo=None, toolbar_location='right', **sizing_mode) self.root = row(column(resource_plot, network_plot, **sizing_mode), column(combo_toolbar, **sizing_mode), id='bk-resource-profiles-plot', **sizing_mode) # Required for update callback self.resource_index = [0]
def bubble_plot_tabs(dataframes): dataframes = dataframes.copy() # convert asset dicts to pandas dataframes base_df = pd.DataFrame.from_dict(dataframes['base_output_by_asset']) reform_df = pd.DataFrame.from_dict(dataframes['reform_output_by_asset']) change_df = pd.DataFrame.from_dict(dataframes['changed_output_by_asset']) list_df = [base_df, change_df, reform_df] list_string = ['base', 'change', 'reform'] data_sources = {} for i, df in enumerate(list_df): # remove data from Intellectual Property, Land, and Inventories Categories df = df[~df['asset_category']. isin(['Intellectual Property', 'Land', 'Inventories'])].copy() df = df.dropna() # define the size DataFrame, if change, use base sizes if list_string[i] == 'base': SIZES = list(range(20, 80, 15)) size = pd.qcut(df['assets_c'].values, len(SIZES), labels=SIZES) size_c = pd.qcut(df['assets_c'].values, len(SIZES), labels=SIZES) size_nc = pd.qcut(df['assets_nc'].values, len(SIZES), labels=SIZES) df['size'] = size df['size_c'] = size_c df['size_nc'] = size_nc else: df['size'] = size df['size_c'] = size_c df['size_nc'] = size_nc # form the two Categories: Equipment and Structures equipment_df = df[(~df.asset_category.str.contains('Structures')) & (~df.asset_category.str.contains('Buildings'))] structure_df = df[(df.asset_category.str.contains('Structures')) | (df.asset_category.str.contains('Buildings'))] format_fields = [ 'metr_c', 'metr_nc', 'metr_c_d', 'metr_nc_d', 'metr_c_e', 'metr_nc_e', 'mettr_c', 'mettr_nc', 'mettr_c_d', 'mettr_nc_d', 'mettr_c_e', 'mettr_nc_e', 'rho_c', 'rho_nc', 'rho_c_d', 'rho_nc_d', 'rho_c_e', 'rho_nc_e', 'z_c', 'z_nc', 'z_c_d', 'z_nc_d', 'z_c_e', 'z_nc_e' ] # Make short category make_short = { 'Instruments and Communications Equipment': 'Instruments and Communications', 'Office and Residential Equipment': 'Office and Residential', 'Other Equipment': 'Other', 'Transportation Equipment': 'Transportation', 'Other Industrial Equipment': 'Other Industrial', 'Nonresidential Buildings': 'Nonresidential Bldgs', 'Residential Buildings': 'Residential Bldgs', 'Mining and Drilling Structures': 'Mining and Drilling', 'Other Structures': 'Other', 'Computers and Software': 'Computers and Software', 'Industrial Machinery': 'Industrial Machinery' } equipment_df['short_category'] = equipment_df['asset_category'].map( make_short) structure_df['short_category'] = structure_df['asset_category'].map( make_short) # Add the Reform and the Baseline to Equipment Asset for f in format_fields: equipment_copy = equipment_df.copy() equipment_copy['rate'] = equipment_copy[f] equipment_copy['hover'] = equipment_copy.apply( lambda x: "{0:.1f}%".format(x[f] * 100), axis=1) simple_equipment_copy = equipment_copy.filter(items=[ 'size', 'size_c', 'size_nc', 'rate', 'hover', 'short_category', 'Asset' ]) data_sources[list_string[i] + '_equipment_' + f] = ColumnDataSource(simple_equipment_copy) # Add the Reform and the Baseline to Structures Asset for f in format_fields: structure_copy = structure_df.copy() structure_copy['rate'] = structure_copy[f] structure_copy['hover'] = structure_copy.apply( lambda x: "{0:.1f}%".format(x[f] * 100), axis=1) simple_structure_copy = structure_copy.filter(items=[ 'size', 'size_c', 'size_nc', 'rate', 'hover', 'short_category', 'Asset' ]) data_sources[list_string[i] + '_structure_' + f] = ColumnDataSource(simple_structure_copy) # Create initial data sources to plot on load if list_string[i] == 'base': equipment_copy = equipment_df.copy() equipment_copy['rate'] = equipment_copy['mettr_c'] equipment_copy['hover'] = equipment_copy.apply( lambda x: "{0:.1f}%".format(x['mettr_c'] * 100), axis=1) simple_equipment_copy = equipment_copy.filter(items=[ 'size', 'size_c', 'size_nc', 'rate', 'hover', 'short_category', 'Asset' ]) data_sources['equip_source'] = ColumnDataSource( simple_equipment_copy) structure_copy = structure_df.copy() structure_copy['rate'] = structure_copy['mettr_c'] structure_copy['hover'] = structure_copy.apply( lambda x: "{0:.1f}%".format(x['mettr_c'] * 100), axis=1) simple_structure_copy = structure_copy.filter(items=[ 'size', 'size_c', 'size_nc', 'rate', 'hover', 'short_category', 'Asset' ]) data_sources['struc_source'] = ColumnDataSource( simple_structure_copy) # Define categories for Equipments assets equipment_assets = [ 'Computers and Software', 'Instruments and Communications', 'Office and Residential', 'Transportation', 'Industrial Machinery', 'Other Industrial', 'Other' ] # Define categories for Structures assets structure_assets = [ 'Residential Bldgs', 'Nonresidential Bldgs', 'Mining and Drilling', 'Other' ] # Equipment plot p = figure( plot_height=540, plot_width=990, y_range=list(reversed(equipment_assets)), tools='hover', background_fill_alpha=0, title= 'Marginal Effective Total Tax Rates on Corporate Investments in Equipment' ) p.title.align = 'center' p.title.text_color = '#6B6B73' hover = p.select(dict(type=HoverTool)) hover.tooltips = [('Asset', ' @Asset (@hover)')] p.xaxis.axis_label = "Marginal effective total tax rate" p.xaxis[0].formatter = NumeralTickFormatter(format="0.1%") p.toolbar_location = None p.min_border_right = 5 p.outline_line_width = 5 p.border_fill_alpha = 0 p.xaxis.major_tick_line_color = "firebrick" p.xaxis.major_tick_line_width = 3 p.xaxis.minor_tick_line_color = "orange" p.outline_line_width = 1 p.outline_line_alpha = 1 p.outline_line_color = "black" p.circle(x='rate', y='short_category', color=BLUE, size='size', line_color="#333333", fill_alpha=.4, source=data_sources['equip_source'], alpha=.4) # Style the tools p.add_tools(WheelZoomTool(), ResetTool(), SaveTool()) p.toolbar_location = "right" p.toolbar.logo = None # Define and add a legend legend_cds = ColumnDataSource({ 'size': SIZES, 'label': ['<$20B', '', '', '<$1T'], 'x': [0, .15, .35, .6] }) p_legend = figure(height=150, width=480, x_range=(-0.075, .75), title='Asset Amount') p_legend.circle(y=None, x='x', size='size', source=legend_cds, color=BLUE, fill_alpha=.4, alpha=.4, line_color="#333333") l = LabelSet(y=None, x='x', text='label', x_offset=-20, y_offset=-50, source=legend_cds) p_legend.add_layout(l) p_legend.axis.visible = False p_legend.grid.grid_line_color = None p_legend.toolbar.active_drag = None data_sources['equip_plot'] = p # Structures plot p2 = figure( plot_height=540, plot_width=990, y_range=list(reversed(structure_assets)), tools='hover', background_fill_alpha=0, title= 'Marginal Effective Total Tax Rates on Corporate Investments in Structures' ) p2.title.align = 'center' p2.title.text_color = '#6B6B73' hover = p2.select(dict(type=HoverTool)) hover.tooltips = [('Asset', ' @Asset (@hover)')] p2.xaxis.axis_label = "Marginal effective total tax rate" p2.xaxis[0].formatter = NumeralTickFormatter(format="0.1%") p2.toolbar_location = None p2.min_border_right = 5 p2.outline_line_width = 0 p2.border_fill_alpha = 0 p2.xaxis.major_tick_line_color = "firebrick" p2.xaxis.major_tick_line_width = 3 p2.xaxis.minor_tick_line_color = "orange" p2.circle(x='rate', y='short_category', color=RED, size='size', line_color="#333333", fill_alpha=.4, source=data_sources['struc_source'], alpha=.4) p2.outline_line_width = 1 p2.outline_line_alpha = 1 p2.outline_line_color = "black" # Style the tools p2.add_tools(WheelZoomTool(), ResetTool(), SaveTool()) p2.toolbar_location = "right" p2.toolbar.logo = None # Define and add a legend p2_legend = figure(height=150, width=380, x_range=(-0.075, .75), title='Asset Amount') p2_legend.circle(y=None, x='x', size='size', source=legend_cds, color=RED, fill_alpha=.4, alpha=.4, line_color="#333333") l2 = LabelSet(y=None, x='x', text='label', x_offset=-20, y_offset=-50, source=legend_cds) p2_legend.add_layout(l2) p2_legend.axis.visible = False p2_legend.grid.grid_line_color = None p2_legend.toolbar.active_drag = None data_sources['struc_plot'] = p2 # add buttons controls_callback = CustomJS(args=data_sources, code=CONTROLS_CALLBACK_SCRIPT) c_nc_buttons = RadioButtonGroup(labels=['Corporate', 'Noncorporate'], active=0, callback=controls_callback) controls_callback.args['c_nc_buttons'] = c_nc_buttons format_buttons = RadioButtonGroup(labels=['Baseline', 'Reform', 'Change'], active=0, callback=controls_callback) controls_callback.args['format_buttons'] = format_buttons interest_buttons = RadioButtonGroup( labels=['METTR', 'METR', 'Cost of Capital', 'Depreciation'], active=0, width=700, callback=controls_callback) controls_callback.args['interest_buttons'] = interest_buttons type_buttons = RadioButtonGroup( labels=['Typically Financed', 'Equity Financed', 'Debt Financed'], active=0, width=700, callback=controls_callback) controls_callback.args['type_buttons'] = type_buttons # Create Tabs tab = Panel(child=column([p, p_legend]), title='Equipment') tab2 = Panel(child=column([p2, p2_legend]), title='Structures') tabs = Tabs(tabs=[tab, tab2]) layout = gridplot(children=[[tabs], [c_nc_buttons, interest_buttons], [format_buttons, type_buttons]]) # Create components js, div = components(layout) cdn_js = CDN.js_files[0] cdn_css = CDN.css_files[0] return js, div, cdn_js, cdn_css
def __init__(self, n_rectangles=1000, clear_interval=20000, **kwargs): """ kwargs are applied to the bokeh.models.plots.Plot constructor """ self.n_rectangles = n_rectangles self.clear_interval = clear_interval self.last = 0 self.source = ColumnDataSource(data=dict(start=[], duration=[], key=[], name=[], color=[], worker=[], y=[], worker_thread=[], alpha=[])) x_range = DataRange1d() y_range = DataRange1d(range_padding=0) self.root = Plot(title=Title(text="Task Stream"), id='bk-task-stream-plot', x_range=x_range, y_range=y_range, toolbar_location="above", min_border_right=35, **kwargs) self.root.add_glyph( self.source, Rect(x="start", y="y", width="duration", height=0.8, fill_color="color", line_color="color", line_alpha=0.6, fill_alpha="alpha", line_width=3)) self.root.add_layout(DatetimeAxis(axis_label="Time"), "below") ticker = BasicTicker(num_minor_ticks=0) self.root.add_layout( LinearAxis(axis_label="Worker Core", ticker=ticker), "left") self.root.add_layout( Grid(dimension=1, grid_line_alpha=0.4, ticker=ticker)) hover = HoverTool(point_policy="follow_mouse", tooltips=""" <div> <span style="font-size: 12px; font-weight: bold;">@name:</span> <span style="font-size: 10px; font-family: Monaco, monospace;">@duration</span> <span style="font-size: 10px;">ms</span> </div> """) # export = ExportTool() # export.register_plot(self.root) self.root.add_tools( hover, # export, BoxZoomTool(), ResetTool(reset_size=False), PanTool(dimensions="width"), WheelZoomTool(dimensions="width")) # Required for update callback self.task_stream_index = [0]
def large_plot(n): from bokeh.models import (Plot, LinearAxis, Grid, GlyphRenderer, ColumnDataSource, DataRange1d, PanTool, ZoomInTool, ZoomOutTool, WheelZoomTool, BoxZoomTool, BoxSelectTool, SaveTool, ResetTool) from bokeh.models.layouts import Column from bokeh.models.glyphs import Line col = Column() objects = set([col]) for i in xrange(n): source = ColumnDataSource(data=dict(x=[0, i + 1], y=[0, i + 1])) xdr = DataRange1d() ydr = DataRange1d() plot = Plot(x_range=xdr, y_range=ydr) xaxis = LinearAxis(plot=plot) yaxis = LinearAxis(plot=plot) xgrid = Grid(plot=plot, dimension=0) ygrid = Grid(plot=plot, dimension=1) tickers = [ xaxis.ticker, xaxis.formatter, yaxis.ticker, yaxis.formatter ] glyph = Line(x='x', y='y') renderer = GlyphRenderer(data_source=source, glyph=glyph) plot.renderers.append(renderer) pan = PanTool() zoom_in = ZoomInTool() zoom_out = ZoomOutTool() wheel_zoom = WheelZoomTool() box_zoom = BoxZoomTool() box_select = BoxSelectTool() save = SaveTool() reset = ResetTool() tools = [ pan, zoom_in, zoom_out, wheel_zoom, box_zoom, box_select, save, reset ] plot.add_tools(*tools) col.children.append(plot) objects |= set([ xdr, ydr, xaxis, yaxis, xgrid, ygrid, renderer, renderer.view, glyph, source, source.selected, source.selection_policy, plot, plot.x_scale, plot.y_scale, plot.toolbar, plot.title, box_zoom.overlay, box_select.overlay, ] + tickers + tools) return col, objects
def draw_map(request): """ Returns a rendered map with a script and div object. :param request: Django request :param nuts_level: The nuts level for which to display the map :param countries: The country code of the regions to be mapped (eg: de, nl, fr) :param pollutants: The pollutants to be mapped (eg: o3, co) :param start_date: The start_date to be mapped (eg: o3, co) :return: Returns a rendered map with a script and div object. """ # Read in paramters and set default values when no parameter is provided nuts_level = request.GET.get('nuts_level', '0') countries = request.GET.get('countries', None) start_date = request.GET.get('start_date', None) end_date = request.GET.get('end_date', None) if start_date is None: return JsonResponse("'start_date' is a required parameter.", safe=False) if end_date is None: end_date = start_date daily_levels = get_daily_data(countries=countries, pollutants=None, start_date=start_date, end_date=end_date) # Uncomment this to use dummy data for testing """ import os from eugreendeal.settings import MEDIA_ROOT json_dir = os.path.join(MEDIA_ROOT, "media", "mock_api_payloads") with open(json_dir + "/daily.json") as f: daily_levels = json.load(f) """ daily_df = gpd.GeoDataFrame() for region_key, region_value in daily_levels.items(): for date_key, date_value in region_value.items(): for pollutant_key, pollutant_value in date_value.items(): if pollutant_value is None: continue day = pollutant_value.get("day-avg-level", 0) if day is None: day = 0 dictionary = { "nuts_id": region_key.upper(), "date": date_key, "pollutant": pollutant_key.upper(), "pollutant_level": round(day, 2) } daily_df = daily_df.append(dictionary, ignore_index=True) if len(daily_df) > 0: # Aggregate daily pollutant data over date range daily_df = daily_df.groupby(["nuts_id", 'pollutant']).mean().reset_index() nuts_boundaries = get_region_boundaries_data(level=nuts_level, regions=countries) df = gpd.GeoDataFrame() level = nuts_boundaries.get(str(nuts_level)) for key, record in level.items(): dictionary = { "nuts_id": key.upper(), "name": record["name"], "country": record["country_code"].upper(), "geometry": gpd.GeoSeries(shapely.wkt.loads(record["geography"]))} df = df.append(gpd.GeoDataFrame(dictionary), ignore_index=True) df.crs = 4326 # Merge NUTS data frame with daily pollutant level dataframe df = df.merge(daily_df) #Reverse the list order of the spectrum since all the online tools put them in the oppposite order to what we want colors = ["#a50026", "#d3322b", "#f16d43", "#fcab63", "#fedc8c", "#f9f7ae", "#d7ee8e", "#a4d86f", "#64bc61"][::-1] # Define the bounds of the EU's main territories (excluding far off islands) eu_bounds = Polygon([(-25, 35), (33, 35), (33, 70), (-25, 70), (-25, 35)]) # Unique pollutants in df unique_pollutants = df.pollutant.unique() # create bokeh elements tabs_list = [] for pollutant in unique_pollutants: filtered_df = df[df.pollutant.eq(pollutant)] # get unique areas base on the region type selected pollution_values = filtered_df['pollutant_level'] min_pollution_value = pollution_values.min() max_pollution_value = pollution_values.max() color_mapper = LinearColorMapper(palette=RdYlGn11, low=min_pollution_value, low_color='green', high=max_pollution_value, high_color='red') tick_format = NumeralTickFormatter(format='0.0') color_bar = ColorBar(color_mapper=color_mapper, #ticker=FixedTicker(ticks=[min_pollution_value, 2.7, max_pollution_value - min_pollution_value, max_pollution_value]), ticker=BasicTicker(desired_num_ticks=len(colors)), formatter=tick_format, major_label_text_font_size="7px", location=(0, 0)) tools = [HoverTool( tooltips=[ ("Latitude, Longitude", "$x{0.000}, $y{0.000}"), ("Country", "@country"), ("NUTS ID", "@nuts_id"), ("Avg " + pollutant + " level", "@pollutant_level{0.0}") ] ), WheelZoomTool(), PanTool(), ResetTool()] # Clip the polygons to the EU bounds filtered_df = gpd.clip(filtered_df, eu_bounds) bokeh_figure = figure(title="Air quality by region", sizing_mode='scale_both', tools=tools, toolbar_location = None, x_axis_location=None, y_axis_location=None, match_aspect=True, border_fill_color=None ) bokeh_figure.xgrid.grid_line_color = None bokeh_figure.ygrid.grid_line_color = None geo_json_data_source = GeoJSONDataSource(geojson=json.dumps(filtered_df.__geo_interface__)) # don't use a line color. the map consists of numerous patches, so you will only see the line color bokeh_figure.patches(xs="xs", ys="ys", source=geo_json_data_source, fill_color={"field": "pollutant_level", "transform": color_mapper}, line_color=None ) bokeh_figure.name = pollutant # add colorbar if(df.nuts_id.nunique() > 1): bokeh_figure.add_layout(color_bar, 'right') tabs_list.append(Panel(child=bokeh_figure, title=bokeh_figure.name)) tabs = Tabs(tabs=tabs_list) tabs.sizing_mode = 'scale_both' # script, div = components(bokeh_figure) item = json_item(tabs) # return render(request, 'airpollution/test-maps.html', dict(script=script, div=div)) return JsonResponse(item) else: nuts_boundaries = get_region_boundaries_data(level=nuts_level, regions=countries) df = gpd.GeoDataFrame() level = nuts_boundaries.get(str(nuts_level)) for key, record in level.items(): dictionary = { "nuts_id": key.upper(), "name": record["name"], "country": record["country_code"].upper(), "geometry": gpd.GeoSeries(shapely.wkt.loads(record["geography"]))} df = df.append(gpd.GeoDataFrame(dictionary), ignore_index=True) df.crs = 4326 #Filter down to a pretty set of countries as background blank_countries = ["NL", "BE", "LU", "FR", "DE"] df = df[df.nuts_id.isin(blank_countries)] # Define the bounds of the EU's main territories (excluding far off islands) eu_bounds = Polygon([(-25, 35), (33, 35), (33, 70), (-25, 70), (-25, 35)]) # Clip the polygons to the EU bounds filtered_df = gpd.clip(df, eu_bounds) bokeh_figure = figure(title="No air quality data for the selected dates", sizing_mode='scale_both', toolbar_location = None, x_axis_location=None, y_axis_location=None, match_aspect=True, border_fill_color=None ) bokeh_figure.xgrid.grid_line_color = None bokeh_figure.ygrid.grid_line_color = None geo_json_data_source = GeoJSONDataSource(geojson=json.dumps(filtered_df.__geo_interface__)) # don't use a line color. the map consists of numerous patches, so you will only see the line color bokeh_figure.patches(xs="xs", ys="ys", source=geo_json_data_source, line_color=None, fill_color="#D3D3D3" ) # script, div = components(bokeh_figure) item = json_item(bokeh_figure) # return render(request, 'airpollution/test-maps.html', dict(script=script, div=div)) return JsonResponse(item)
def plot_dag( functions, targets=None, columns_overriding_functions=None, check_minimal_specification="ignore", selectors=None, labels=True, tooltips=False, plot_kwargs=None, arrow_kwargs=None, edge_kwargs=None, label_kwargs=None, node_kwargs=None, ): """Plot the dag of the tax and transfer system. Parameters ---------- functions : str, pathlib.Path, callable, module, imports statements, dict Functions can be anything of the specified types and a list of the same objects. If the object is a dictionary, the keys of the dictionary are used as a name instead of the function name. For all other objects, the name is inferred from the function name. targets : str, list of str String or list of strings with names of functions whose output is actually needed by the user. columns_overriding_functions : str list of str Names of columns in the data which are preferred over function defined in the tax and transfer system. check_minimal_specification : {"ignore", "warn", "raise"}, default "ignore" Indicator for whether checks which ensure the most minimal configuration should be silenced, emitted as warnings or errors. selectors : str or list of str or dict or list of dict or list of str and dict Selectors allow to you to select and de-select nodes in the graph for visualization. For the full list of options, see the tutorial about `visualization <../docs/tutorials/visualize.ipynb>`_. By default, all nodes are shown. labels : bool, default True Annotate nodes with labels. tooltips : bool, default False Experimental feature which makes the source code of the functions accessible as a tooltip. Sometimes, the tooltip is not properly displayed. plot_kwargs : dict Additional keyword arguments passed to :class:`bokeh.models.Plot`. arrow_kwargs : dict Additional keyword arguments passed to :class:`bokeh.models.Arrow`. For example, change the size of the head with ``{"size": 10}``. edge_kwargs : dict Additional keyword arguments passed to :class:`bokeh.models.MultiLine`. For example, change the color with ``{"fill_color": "green"}``. label_kwargs : dict Additional keyword arguments passed to :class:`bokeh.models.LabelSet`. For example, change the fontsize with ``{"text_font_size": "12px"}``. node_kwargs : dict Additional keyword arguments passed to :class:`bokeh.models.Circle`. For example, change the color with ``{"fill_color": "orange"}``. """ targets = DEFAULT_TARGETS if targets is None else targets targets = parse_to_list_of_strings(targets, "targets") columns_overriding_functions = parse_to_list_of_strings( columns_overriding_functions, "columns_overriding_functions") # Load functions and perform checks. functions, internal_functions = load_user_and_internal_functions(functions) # Create one dictionary of functions and perform check. functions = {**internal_functions, **functions} functions = { k: v for k, v in functions.items() if k not in columns_overriding_functions } _fail_if_targets_not_in_functions(functions, targets) # Partial parameters to functions such that they disappear in the DAG. functions = _mock_parameters_arguments(functions) dag = create_dag(functions, targets, columns_overriding_functions, check_minimal_specification) selectors = [] if selectors is None else _to_list(selectors) plot_kwargs = {} if plot_kwargs is None else plot_kwargs arrow_kwargs = {} if arrow_kwargs is None else arrow_kwargs edge_kwargs = {} if edge_kwargs is None else edge_kwargs label_kwargs = {} if label_kwargs is None else label_kwargs node_kwargs = {} if node_kwargs is None else node_kwargs dag = _select_nodes_in_dag(dag, selectors) dag = _add_url_to_dag(dag) # Even if we do not use the source codes as tooltips, we need to remove the # functions. dag = _replace_functions_with_source_code(dag) plot_kwargs["title"] = _to_bokeh_title( plot_kwargs.get("title", "Tax and Transfer System")) plot = Plot(**{**PLOT_KWARGS_DEFAULTS, **plot_kwargs}) layout = _create_pydot_layout(dag) graph_renderer = from_networkx(dag, layout, scale=1, center=(0, 0)) graph_renderer.node_renderer.glyph = Circle(**{ **NODE_KWARGS_DEFAULTS, **node_kwargs }) graph_renderer.edge_renderer.visible = False for ( _, (start_node, end_node), ) in graph_renderer.edge_renderer.data_source.to_df().iterrows(): (x_start, y_start), (x_end, y_end) = _compute_arrow_coordinates( layout[start_node], layout[end_node]) plot.add_layout( Arrow( end=NormalHead(**{ **ARROW_KWARGS_DEFAULTS, **arrow_kwargs }), x_start=x_start, y_start=y_start, x_end=x_end, y_end=y_end, **{ **EDGE_KWARGS_DEFAULTS, **edge_kwargs }, )) plot.renderers.append(graph_renderer) tools = [BoxZoomTool(), ResetTool()] tools.append(TapTool(callback=OpenURL(url="@url"))) if tooltips: tools.append(HoverTool(tooltips=TOOLTIPS)) plot.add_tools(*tools) if labels: source = ColumnDataSource( pd.DataFrame(layout).T.rename(columns={ 0: "x", 1: "y" })) labels = LabelSet( x="x", y="y", text="index", source=source, **{ **LABEL_KWARGS_DEFAULT, **label_kwargs }, ) plot.add_layout(labels) output_notebook() show(plot) return plot
def make_fig(df, x_col, y_col, x_lab, y_lab, code, best_fit=None): """ function to make interactive two variable bokeh scatter plot :param df: plot dataframe :param x_col: (str) column name for x axis data :param y_col: (str) column name for y axis data :param x_lab: (str) name for x axis label :param y_lab: (str) name for y axis label :param code: (str) column name for request code data :param best_fit: best fit line values [xvalues array, yvalues array] :return: (obj) bokeh figure """ ds = ColumnDataSource(df) # set the axis range if y_col == SERVICE_STD: x_range = None y_range = Range1d(0, SERVICE_STD_MIN + 30, bounds="auto") scale = "linear" hline = Span(location=SERVICE_STD_MIN, dimension='width', line_color=STANDARD_RED, line_dash='dashed', line_width=1, line_alpha=0.9, name="cutoff") else: x_range = None y_range = None scale = "linear" # initialize the figure fig = figure( plot_height=None, plot_width=None, sizing_mode="scale_both", border_fill_color='white', background_fill_color=SEABORN_BGR, background_fill_alpha=1, x_axis_label=x_lab, x_axis_type="log", x_axis_location='below', y_axis_label=y_lab, y_axis_type=scale, y_axis_location='left', x_range=x_range, y_range=y_range, title=None, tools=[BoxZoomTool(), ResetTool(), PanTool(), WheelZoomTool()], toolbar_location='right', ) # add the observed count points = fig.circle(x=x_col, y=y_col, source=ds, color='color', size=6, name="data") if y_col == BACKLOG and best_fit: fig.line(x=best_fit[0], y=best_fit[1], color=BACKLOG_BLUE, alpha=0.8, line_dash='dashed', line_width=1.5, name="bestfit") # set the hovertools tooltips = [ (y_lab, f'@{y_col}{{0.[0] a}}'), (x_lab, f'@{x_col}{{0.[0] a}}'), (code, f'@{code}'), ] hover_tool = HoverTool(tooltips=tooltips, mode='mouse', renderers=[points]) # figure aesthetics fig.add_tools(hover_tool) fig.xaxis.formatter = NumeralTickFormatter(format="0.[0] a") fig.axis.axis_label_text_font_size = "12pt" fig.axis.minor_tick_line_color = None fig.grid.grid_line_color = "white" if y_col == SERVICE_STD: fig.renderers.extend([hline]) return fig
SAME_CLUB_COLOR, DIFFERENT_CLUB_COLOR = "black", "red" edge_attrs = {} for start_node, end_node, _ in G.edges(data=True): edge_color = SAME_CLUB_COLOR if G.nodes[start_node]["club"] == G.nodes[ end_node]["club"] else DIFFERENT_CLUB_COLOR edge_attrs[(start_node, end_node)] = edge_color nx.set_edge_attributes(G, edge_attrs, "edge_color") # Show with Bokeh plot = Plot(width=400, height=400, x_range=Range1d(-1.1, 1.1), y_range=Range1d(-1.1, 1.1)) plot.title.text = "Graph Interaction Demonstration" node_hover_tool = HoverTool(tooltips=[("index", "@index"), ("club", "@club")]) plot.add_tools(node_hover_tool, BoxZoomTool(), ResetTool()) graph_renderer = from_networkx(G, nx.spring_layout, scale=1, center=(0, 0)) graph_renderer.node_renderer.glyph = Circle(size=15, fill_color=Spectral4[0]) graph_renderer.edge_renderer.glyph = MultiLine(line_color="edge_color", line_alpha=0.8, line_width=1) plot.renderers.append(graph_renderer) output_file("interactive_graphs.html") show(plot)
def quadrant_fig(df, x_col, y_col, x_lab, y_lab, code, label_df, fx=5, fy=5): """ function to make interactive bokeh scatter quadrant plot :param df: plot dataframe :param x_col: (str) column name for x axis data :param y_col: (str) column name for y axis data :param x_lab: (str) name for x axis label :param y_lab: (str) name for y axis label :param code: (str) column name for request code data :param label_df: dataframe for annotating labels :param fx: (int) annotation x offset :param fy: (int) annotation y offset :return: (obj) bokeh figure """ ds = ColumnDataSource(df) label_ds = ColumnDataSource(label_df) vline = Span(location=SERVICE_STD_MIN, dimension='height', line_color=STANDARD_RED, line_dash='dashed', line_width=1, line_alpha=0.9) hline = Span(location=1, dimension='width', line_color=BACKLOG_BLUE, line_dash='dashed', line_width=1, line_alpha=0.9) # set the y_axis range x_range = Range1d(0, SERVICE_STD_MIN + 30, bounds="auto") y_min = df[y_col].min() if y_min > BACKLOG_MIN + 200: set_y_min = BACKLOG_MIN else: set_y_min = y_min - 200 y_max = df[y_col].max() set_y_max = y_max + 200 y_range = Range1d(set_y_max, set_y_min, bounds="auto") # set the hovertools tooltips = [ (y_lab, f'@{y_col}{{0.[0] a}}'), (x_lab, f'@{x_col}{{0.[0] a}}'), (code, f'@{code}'), ("Requests Opened", f'@{OPENED_COUNT}'), ("Department", f'@department'), ] hover_tool = HoverTool(tooltips=tooltips, mode='mouse') # initialize the figure fig = figure( plot_height=None, plot_width=None, sizing_mode="scale_both", border_fill_color='white', background_fill_color=SEABORN_BGR, background_fill_alpha=1, x_axis_label=x_lab, x_axis_type="linear", x_axis_location='below', y_axis_label=y_lab, y_axis_type="linear", y_axis_location='left', x_range=x_range, y_range=y_range, title=None, tools=[ BoxZoomTool(), ResetTool(), PanTool(), WheelZoomTool(), hover_tool ], toolbar_location='below', ) # add quadrant colors green_box = BoxAnnotation(bottom=0, top=set_y_min, left=80, right=110, fill_color='green', fill_alpha=0.1) orange_box_1 = BoxAnnotation(bottom=0, top=set_y_min, left=0, right=80, fill_color='orange', fill_alpha=0.1) orange_box_2 = BoxAnnotation(bottom=set_y_max, top=0, left=80, right=110, fill_color='orange', fill_alpha=0.1) red_box = BoxAnnotation(bottom=set_y_max, top=0, left=0, right=80, fill_color='red', fill_alpha=0.1) # add the observed count fig.circle(x=x_col, y=y_col, source=ds, color='color', size="size") fig.circle(x=x_col, y=y_col, source=label_ds, color='dept_color', legend_group='department', size="size") # add the label annotations labels = LabelSet(x=SERVICE_STD, y=BACKLOG, text=code, x_offset=fx, y_offset=fy, source=label_ds, render_mode='canvas', level='glyph') # figure aesthetics fig.axis.axis_label_text_font_size = "16pt" fig.legend.location = "bottom_left" fig.yaxis.formatter = NumeralTickFormatter(format="0.[0] a") fig.axis.minor_tick_line_color = None fig.grid.grid_line_color = "white" fig.add_layout(labels) fig.add_layout(green_box) fig.add_layout(orange_box_1) fig.add_layout(orange_box_2) fig.add_layout(red_box) fig.renderers.extend([hline, vline]) return fig
plot_width=1200, plot_height=800, # tools='pan,box_zoom,wheel_zoom,crosshair,undo,redo,reset,save', toolbar_location=None, x_axis_label='Episodes', x_range=Range1d(0, 10000), y_range=Range1d(0, 100000), lod_factor=1000) plot.extra_y_ranges = {"secondary": Range1d(start=-100, end=200)} plot.add_layout(LinearAxis(y_range_name="secondary"), 'right') toolbar = Toolbar(tools=[ PanTool(), BoxZoomTool(), WheelZoomTool(), CrosshairTool(), ResetTool(), SaveTool() ]) # plot.toolbar = toolbar plot.add_tools(*toolbar.tools) plot.yaxis[-1].visible = False bokeh_legend = Legend(items=[("", [])], orientation="vertical", border_line_color="black", label_text_font_size={'value': '9pt'}, click_policy='hide', visible=False) bokeh_legend.label_width = 100 plot.add_layout(bokeh_legend, "right") plot.y_range = Range1d(0, 100)
# convert data into proper form for bokeh: parcel_source = ColumnDataSource(btv_map) border_source = ColumnDataSource(outline[['x', 'y']]) color_mapper = LinearColorMapper(palette=viridis(100), low=0, high=200) display_list = [(n, f'@{n}') for n in [ 'PropertyAddress', 'PreviousAppraisedValue', 'CurrentAppraisedValue', 'Percent_change', 'LandUseCode', 'OwnerName1' ]] #configure tools hover = HoverTool(tooltips=display_list) zoom = BoxZoomTool() pan = PanTool() reset = ResetTool() p = figure(title="Change in Burlington Assessements", tools=[hover, zoom, pan, reset]) color_bar = ColorBar(color_mapper=color_mapper, title='Percent Increase in Assessment') p.add_layout(color_bar, 'below') #plot town boundary p.multi_line('x', 'y', source=border_source, color="k", line_width=2) #plot parcels p.patches('x', 'y', source=parcel_source, fill_color={
def rca(functionNode): logger = functionNode.get_logger() logger.info("==>>>> in rca (root cause analysis " + functionNode.get_browse_path()) progressNode = functionNode.get_child("control").get_child("progress") progressNode.set_value(0.1) variables = functionNode.get_child("selectedVariables").get_leaves() tag = functionNode.get_child("selectedTags").get_value() #only one tag annotations = functionNode.get_child("annotations").get_leaves() feature = functionNode.get_child("selectedFeatures").get_value() algo = functionNode.get_child("selectedAlgorithms").get_value() target = functionNode.get_child("selectedTarget").get_target() p = Progress(progressNode) p.set_divisor(len(annotations) / 0.5) p.set_offset(0.1) #now create the data as x-y results = {"x": [], "y": []} var = variables[0] #now iterate over all annotations of the matching type and create feature for idx, anno in enumerate(annotations): p.set_progress(idx) if (anno.get_child("type").get_value() == "time") and (tag in anno.get_child("tags").get_value()): startTime = anno.get_child("startTime").get_value() endTime = anno.get_child("endTime").get_value() data = var.get_time_series(startTime, endTime) #now create the feature feat = calc_feature(data["values"], feature) targetValue = get_target( target, (date2secs(startTime) + date2secs(endTime)) / 2) if feat and targetValue and numpy.isfinite( feat) and numpy.isfinite(targetValue): results["x"].append(feat) results["y"].append(targetValue) else: logger.warning( f"no result for {var.get_name} @ {startTime}, anno:{tag}, feat:{feat}, target: {target}" ) #now we have all the x-y progressNode.set_value(0.7) fig = figure(title="x-y Correlation Plot " + var.get_name(), tools=[PanTool(), WheelZoomTool(), ResetTool(), SaveTool()], plot_height=300, x_axis_label=feature + "(" + var.get_name() + ") @ " + tag, y_axis_label=target.get_name()) fig.toolbar.logo = None curdoc().theme = Theme(json=themes.darkTheme) fig.xaxis.major_label_text_color = themes.darkTickColor fig.yaxis.major_label_text_color = themes.darkTickColor fig.scatter(x=results["x"], y=results["y"], size=5, fill_color="#d9b100", marker="o") fileName = functionNode.get_child("outputFileName").get_value() filePath = os.path.join(myDir, './../web/customui/' + fileName) progressNode.set_value(0.8) output_file( filePath, mode="inline" ) #inline: put the bokeh .js into this html, otherwise the default cdn will be taken, might cause CORS problems) save(fig) #print(results) return True
y=[0, 0, 0], sentiment_color_stream1=["red", "red", 'white'], kmeans_color_stream1=["red", "red", 'white'], tweet_text=['a', 'a', 'a'], sentiment_legend=["red", "red", 'white'], kmeans_legend=["red", "red", 'white'], )) hover = HoverTool(tooltips=[("Tweet", "@tweet_text")]) hover1 = HoverTool(tooltips=[("Tweet", "@tweet_text")]) # Scatter plot for clustering using sentiment - Category 1 sentiment_stream1_plot = figure(plot_width=scatterplot_width, plot_height=scatterplot_height, title="Clustering using sentiments on Term 1", tools=[hover, LassoSelectTool(), ResetTool()]) sentiment_scatter1 = sentiment_stream1_plot.circle( 'x', 'y', size=5, fill_color="sentiment_color_stream1", line_color=None, legend='sentiment_legend', source=source) sentiment_stream1_plot.axis.visible = False # sentiment_stream1_plot.xgrid.grid_line_color = None # sentiment_stream1_plot.ygrid.grid_line_color = None # Scatter plot for clustering using kmeans - Category 1 kmeans_stream1_plot = figure(plot_width=scatterplot_width, plot_height=scatterplot_height,