def buildScatterPlot(word_happiness): wordList = [] wordHappiness = [] for word in word_happiness.keys(): wordList.append(word) wordHappiness.append(word_happiness[word]) datasource = { 'x': range(1, len(wordList) + 1), 'y': wordHappiness, 'labels': wordList } source = ColumnDataSource(datasource) print(len(wordList)) hover = HoverTool(tooltips=[ ("word", "@labels"), ]) ###### attempt at regresssion line\ regression = np.polyfit(source.data['x'], source.data['y'], 1) r_x, r_y = zip(*((i, i * regression[0] + regression[1]) for i in range(len(source.data['x'])))) source.add(r_x, 'r_x') source.add(r_y, 'r_y') p = figure(plot_width=800, plot_height=800, tools=[hover]) p.outline_line_width = 13 p.outline_line_alpha = 0.3 p.outline_line_color = "navy" p.line('x', 'y', source=source) p.line('r_x', 'r_y', source=source, color='red') return p
def compute_data_source(self): source = ColumnDataSource(self.df.reset_index()[2:]) source.add( self.df[2:].LogReturns.ge(0).map( lambda x: "steelblue" if x else "red"), 'LogReturnsColor') source.add(self.df[2:].DevolLogReturns / 2., 'y_mids') return source
def get_analyzers_tables( self, analyzer: bt.analyzers.Analyzer) -> (Paragraph, List[DataTable]): """Return a header for this analyzer and one *or more* data tables.""" if hasattr(analyzer, 'get_analysis_table'): title, table_columns_list = analyzer.get_analysis_table() else: # Analyzer does not provide a table function. Use our generic one title, table_columns_list = TableGenerator._get_analysis_table_generic( analyzer) param_str = get_params_str(analyzer.params) if len(param_str) > 0: title += f' ({param_str})' elems: List[DataTable] = [] for table_columns in table_columns_list: cds = ColumnDataSource() columns = [] for i, c in enumerate(table_columns): col_name = f'col{i}' cds.add(c[2:], col_name) columns.append( TableColumn(field=col_name, title=c[0], formatter=self._get_formatter(c[1]))) column_height = len(table_columns[0]) * 25 elems.append( DataTable(source=cds, columns=columns, index_position=None, height=column_height)) return Paragraph(text=title, style={'font-size': 'large'}), elems
def get_grid(data_by_node, attributes): """Create the ColumnDataSource instance as used by Bokeh for plotting the grid. It transforms the pandas dataframe into the appropriate structure. Todo: link the node id to a list of articles IDs for further bindings to URIs in the database Parameters: ----------- data_by_node: pandas dataframe Contains the node data with the list of nodes linking to the cluster ID and the main topics, and article hits attributes: dictionary Contains the main attributes of the data modelling session. """ n_clusters = attributes["n_clusters"] k_shape = attributes["kshape"] colors = [ "#%02x%02x%02x" % (np.floor(250. * (float(n) / n_clusters)), 100, 100) for n in xrange(n_clusters) ] colormap = {i: colors[i] for i in xrange(n_clusters)} source = ColumnDataSource(data_by_node) source.add([colormap[cl] for cl in source.data["cluster"]], 'color') x_range = [str(x) for x in xrange(k_shape[0])] y_range = [str(x) for x in xrange(k_shape[1])] return source, x_range, y_range
def plot_interactive_timeseries(x, y, data, title, hover_cols=None): hover_cols = hover_cols or list(data.columns) df = data[hover_cols].dropna(axis=0, how='any') source = ColumnDataSource(df) tooltips = [] for col in df.columns: df[col].dtype if df[col].dtype == pd.np.dtype('datetime64[ns]'): # TODO: fix air_date is off by a day # tz_localize('US/Eastern') source.add(df[col].map(lambda x: x.strftime('%x')), col + "_str") tooltips.append([col, "@" + col + "_str"]) elif df[col].dtype == pd.np.dtype('float64'): tooltips.append([col, "@" + col + "{1.11}"]) else: tooltips.append([col, "@" + col]) p = figure( plot_width=bokeh.charts.defaults.width, plot_height=bokeh.charts.defaults.height, x_axis_type="datetime", title=title) p.axis[1].formatter.use_scientific = False hover = HoverTool(tooltips=tooltips) p.circle(x=x, y=y, line_width=2, source=source, size=5) p.add_tools(hover) p.toolbar.logo = None return p
def make_plot(): # prepare data stuff - take the keys from the data in order of likelihood categories = list(reversed(probly.keys())) palette = [cc.rainbow[i * 15] for i in range(17)] x = linspace(-20, 110, 500) source = ColumnDataSource(data=dict(x=x)) p = figure(y_range=categories, plot_width=900, x_range=(-5, 105), toolbar_location=None) for i, cat in enumerate(reversed(categories)): pdf = gaussian_kde(probly[cat]) y = ridge(cat, pdf(x)) source.add(y, cat) p.patch('x', cat, color=palette[i], alpha=0.6, line_color="black", source=source) p.outline_line_color = None p.background_fill_color = "#efefef" p.xaxis.ticker = FixedTicker(ticks=list(range(0, 101, 10))) p.xaxis.formatter = PrintfTickFormatter(format="%d%%") p.ygrid.grid_line_color = None p.xgrid.grid_line_color = "#dddddd" p.xgrid.ticker = p.xaxis[0].ticker p.axis.minor_tick_line_color = None p.axis.major_tick_line_color = None p.axis.axis_line_color = None p.y_range.range_padding = 0.12 return p
def plot_coords(df, filename): lat = 'Latitude|"Degrees"|-180.0|180.0|10' long = 'Longitude|"Degrees"|-180.0|180.0|10' speed = 'Speed|"mph"|0.0|150.0|10' df.loc[df[lat] == 0, lat] = np.nan df.loc[df[long] == 0, long] = np.nan coord_source = ColumnDataSource(df) coord_source.add(df['Interval|"ms"|0|0|1'], name='Time') coord = figure(sizing_mode='scale_both', width=700, height=600, title='GPS Data_{}'.format(filename)) # TODO figure out why this is broken # mapper = linear_cmap(field_name='Speed"|"mph"|0.0|150.0|10', palette=Spectral6, low=min(speed), high=max(speed)) coord.circle(x=lat, y=long, source=coord_source, size=3, color='darkcyan') # TODO figure out how to make the points be connected # coord.line(x=lat, y=long, source=coord_source, line_width=2, color='red') # Tools hover = HoverTool() hover.tooltips = [('Lat', '$x{0.000000}'), ('Long', '$y{0.000000}'), ('Time', '@Time')] hover.point_policy = 'follow_mouse' coord.add_tools(hover) return coord
def plot_ridge(measured): all_times = [item for subl in measured.values() for item in subl] x = np.logspace( np.log10(min(all_times)) - 1, np.log10(max(all_times)) + 1, 500) source = ColumnDataSource(data=dict(x=x)) p = bp.figure( title='Distribution of optimization times ({} measurements)'.format( len(all_times)), x_axis_type='log') for i, (model_type, these_times) in enumerate(measured.items()): if len(these_times) == 0: continue y = logx_kde(x, these_times) + 1 source.add(y, model_type) p.patch('x', model_type, color=cmap[i], alpha=0.6, line_color="black", source=source) # p.output_backend = 'svg' p.y_range.range_padding = 0.12 return p
def draw_plots(CDS_data): # dict(x= xCoord, y = yCoord, names=labels,selected=selStatus) source = ColumnDataSource(data=dict()) for p in CDS_data: source.add(CDS_data[p], name=p)
def get_grid(data_by_node, attributes): """Create the ColumnDataSource instance as used by Bokeh for plotting the grid. It transforms the pandas dataframe into the appropriate structure. Todo: link the node id to a list of articles IDs for further bindings to URIs in the database Parameters: ----------- data_by_node: pandas dataframe Contains the node data with the list of nodes linking to the cluster ID and the main topics, and article hits attributes: dictionary Contains the main attributes of the data modelling session. """ n_clusters = attributes["n_clusters"] k_shape = attributes["kshape"] colors = ["#%02x%02x%02x" % (np.floor(250.0 * (float(n) / n_clusters)), 100, 100) for n in xrange(n_clusters)] colormap = {i: colors[i] for i in xrange(n_clusters)} source = ColumnDataSource(data_by_node) source.add([colormap[cl] for cl in source.data["cluster"]], "color") x_range = [str(x) for x in xrange(k_shape[0])] y_range = [str(x) for x in xrange(k_shape[1])] return source, x_range, y_range
def ridge_plots(model_df): models = list(model_df.keys()) models.reverse() for i, model in enumerate(models): if 'name' in model: models.pop(i) palette = [cc.rainbow[i * 15] for i in range(len(models))] x = np.linspace(-20, 110, 500) source = ColumnDataSource(data=dict(x=x)) p = figure(y_range=models, plot_width=900, x_range=(0, 5), toolbar_location=None) for i, model in enumerate(models): if 'name' in model: continue pdf = gaussian_kde(model_df[model].dropna()) y = ridge(model, pdf(x)) source.add(y, model) p.patch('x', model, color=palette[i], alpha=0.6, line_color="black", source=source) p.outline_line_color = None p.background_fill_color = "#efefef" p.xaxis.ticker = FixedTicker( ticks=list(range(int(model_df.min().min()), 5, 1))) # p.xaxis.formatter = PrintfTickFormatter(format="%d%%") p.ygrid.grid_line_color = None p.xgrid.grid_line_color = "#dddddd" p.xgrid.ticker = p.xaxis[0].ticker p.axis.minor_tick_line_color = None p.axis.major_tick_line_color = None p.axis.axis_line_color = None p.y_range.range_padding = 2 base = Span(location=1, dimension='height', line_color='black', line_dash='dashed', line_width=3) p.add_layout(base) st.bokeh_chart(p) return
def source_from_dataframe(dataframe, settings, current_selection): """""" source = ColumnDataSource(dataframe) embeddings_names = list(settings['embeddings'].keys()) selected_name = embeddings_names[current_selection] selected_column_x = settings['embeddings'][selected_name][0] selected_column_y = settings['embeddings'][selected_name][1] # Create empty columns to put selected coordinate data into source.add(dataframe[selected_column_x], name='x') source.add(dataframe[selected_column_y], name='y') return source
class DataHandler(object): """ Parameters ---------- data : pandas DataFrame """ def __init__(self, data): self.source = ColumnDataSource(data) self.source.add(data.index, "index")
def generate_optresult_model(self, optresult: List[List[bt.OptReturn]], columns=None) -> Model: """Generates and returns an interactive model""" #o = list(self._options.keys()) #selector = Select(title="Result:", value="result", options=o) cds = ColumnDataSource() tab_columns = [] for idx, strat in enumerate(optresult[0]): # add suffix when dealing with more than 1 strategy strat_suffix = '' if len(optresult[0]): strat_suffix = f' [{idx}]' for name, val in strat.params._getitems(): tab_columns.append( TableColumn(field=f"{idx}_{name}", title=f'{name}{strat_suffix}')) # get value for the current param for all results pvals = [] for res in optresult: pvals.append(res[idx].params._get(name)) cds.add(pvals, f"{idx}_{name}") # add user columns specified by parameter 'columns' if columns is not None: for k, v in columns.items(): ll = [str(v(x)) for x in optresult] cds.add(ll, k) tab_columns.append(TableColumn(field=k, title=k)) selector = DataTable(source=cds, columns=tab_columns, width=1600, height=160) model = column( [selector, self.plot_and_generate_model(optresult[idx])]) def update(name, old, new): if len(new['1d']['indices']) == 0: return stratidx = new['1d']['indices'][0] model.children[-1] = self.plot_and_generate_model( optresult[stratidx]) cds.on_change('selected', update) return model
def get_source_geo_and_count_us_continent(state_count_experience_selector_dict, state_count_careerarea_selector_dict, states_map): states = {code: state for code, state in states_map.items()} states_sort_by_name = states.values() states_sort_by_name = sorted(states_sort_by_name, key=lambda x: x['name']) # sort states' geo by its name, it helps decide the order of count data. cause .csv data is already sorted by state name state_xs = [state["lons"] for state in states_sort_by_name] state_ys = [state["lats"] for state in states_sort_by_name] state_names = [state["name"] for state in states_sort_by_name] source = ColumnDataSource( data=dict(x=state_xs, y=state_ys, name=[name + ", United States" for name in state_names])) state_count_total = np.zeros( len(list(state_count_experience_selector_dict.values())[0])) for name, state_count in state_count_experience_selector_dict.items(): state_count_total = [ sum(x) for x in zip(state_count_total, state_count) ] source.add(data=state_count, name='count' + name) source.add(data=state_count_total, name='count_all') source.add(data=state_count_total, name='count') for experience_selector_name, state_count_careerarea in state_count_careerarea_selector_dict.items( ): careerarea_selector_name_initial = 'count' + experience_selector_name for careerarea_name, state_count in state_count_careerarea.items(): source.add(data=state_count, name=careerarea_selector_name_initial + careerarea_name) return source
def create_milage_chart(self): # DataFrameの値を Bokeh用セット source = ColumnDataSource(self.df) source.add(self.df.index, 'index') # チャートのツールを設定 tools = "pan,box_zoom,reset,save" # チャート内のツールチップのツールチップを設定 milage_hover_tool = HoverTool( tooltips=[ ('日付', "@index{%F}"), ('当日走行距離', '@Milage'+' (km)'), ('合計走行距離', '@Sum_milage'+'( km)'), ('目標距離', '@Sum_target'+' (km)')], formatters={ '@index': 'datetime', }, mode='mouse') # figureオブジェクトを生成 # figureオブジェクトのlineプロパティに値を追加する milage_plot = figure( plot_width=self.width, plot_height=self.height, x_axis_type="datetime", x_axis_label = "date", y_axis_label = "distance(km)", title='Milage Line_Chart', tools=[milage_hover_tool, tools] ) milage_plot.line( x = "index", y = "Sum_milage", source = source, legend = "走行距離", color = "blue", line_width = 2 ) milage_plot.line( x = "index", y = "Sum_target", source = source, legend = "目標値", color = "red", line_width = 2 ) milage_plot.add_layout(milage_plot.legend[0], 'below') return milage_plot
def construct_categorical_data(data_frame: pd.DataFrame, c_col: str = None, v_col: str = None, color_col: str = None, text_col: str = None): source = ColumnDataSource(data={ 'category': data_frame[c_col].values, 'value': data_frame[v_col].values }) if text_col is not None: source.add(data_frame[text_col].values, 'text') return source
def build_viz_withparam(df, filterBird='LIAK11EG12'): #print(filterState) gps = getArtox_gps_data() ## filter the dataset to keep only ports of the given state if (filterBird!='LIAK11EG12') : if (filterBird=='Unknown') : df = df[df.bird_id.isnull() ] else : df = df[df.bird_id.eq(filterBird) ] ## Build the map p = figure(x_range=(-9587947, 1113194), y_range=(3503549, 13195489), x_axis_type="mercator", y_axis_type="mercator") tile_provider = get_provider(CARTODBPOSITRON) p.add_tile(tile_provider) msource = ColumnDataSource(df) # https://docs.bokeh.org/en/latest/docs/reference/palettes.html color_mapper = CategoricalColorMapper(palette=["blue", "pink"], factors=["M", "F"]) ## Filter GPS data with the choosen bird gpsnumber = df.loc[df['bird_id']==filterBird, 'clean_glsid'].values[0] gpsnumber #'148' msourceGPS = ColumnDataSource(gps.loc[gps['id']==gpsnumber,]) #process the time dimension thisbirdtimes = gps.loc[gps['id']==gpsnumber, 'timestampgps'].values thisbirdtimes = pd.to_datetime(thisbirdtimes) #transform string into datetime type #First solution for points color : time on a continuous scale t = np.array([xi.timestamp() for xi in thisbirdtimes]) #transform datetime into float type msourceGPS.add(t, 'timeasreal') point_mapper = linear_cmap(field_name='timeasreal', palette=GnBu[9], low=min(t), high=max(t)) #OK for time on a continuous scale, you can also use the palette PiYG11 ## Afficher les coordonnées GPS de cet oiseau #https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.loc.html #p.line(df.iloc[3, 2], df.iloc[3, 3], color='red', line_width=2) p.line(df.loc[df['bird_id']==filterBird, 'x_path_coordinates'].values[0], df.loc[df['bird_id']==filterBird, 'y_path_coordinates'].values[0], color='red', line_width=1) p.circle(x='x', y='y', size=7, source=msourceGPS, fill_color=point_mapper, fill_alpha=1, line_alpha=0) return p
def init_table(view): """Generates the info table""" dimension_values_df = view.get_dimension_values_df() source = ColumnDataSource(dimension_values_df) source.add(dimension_values_df.index, name='name') columns = [ TableColumn(field=field, title=field) for field in dimension_values_df.columns.values ] columns.insert(0, TableColumn(field='name', title='name')) data_table = DataTable(source=source, columns=columns, width=1000, height=600) return data_table
def colorce(): import colorcet as cc from numpy import linspace from scipy.stats.kde import gaussian_kde from bokeh.io import output_file, show from bokeh.models import ColumnDataSource, FixedTicker, PrintfTickFormatter from bokeh.plotting import figure from bokeh.sampledata.perceptions import probly def ridge(category, data, scale=20): return list(zip([category]*len(data), scale*data)) cats = list(reversed(probly.keys())) palette = [cc.rainbow[i*15] for i in range(17)] x = linspace(-20,110, 500) source = ColumnDataSource(data=dict(x=x)) p = figure(y_range=cats, plot_width=900, x_range=(-5, 105), toolbar_location=None) for i, cat in enumerate(reversed(cats)): pdf = gaussian_kde(probly[cat]) y = ridge(cat, pdf(x)) source.add(y, cat) p.patch('x', cat, color=palette[i], alpha=0.6, line_color="black", source=source) p.outline_line_color = None p.background_fill_color = "#efefef" p.xaxis.ticker = FixedTicker(ticks=list(range(0, 101, 10))) p.xaxis.formatter = PrintfTickFormatter(format="%d%%") p.ygrid.grid_line_color = None p.xgrid.grid_line_color = "#dddddd" p.xgrid.ticker = p.xaxis.ticker p.axis.minor_tick_line_color = None p.axis.major_tick_line_color = None p.axis.axis_line_color = None p.y_range.range_padding = 0.12 return p
def asbokeh_scatter( self, hover_names=None, tools="pan,box_zoom,wheel_zoom,reset,hover,save,zoom_in,zoom_out", colors=None): from bokeh.models import (ColumnDataSource, HoverTool, LogColorMapper) from bokeh.io import show, save from bokeh.plotting import figure, output_file from bokeh.embed import components from bokeh.models import LinearAxis, Range1d, Span p = figure(title=self.name, tools=tools) if colors == None: colors = ['red', 'blue', 'green', 'gray', 'orange'] for i in range(len(self.ax.collections)): col = self.ax.collections[i] source = ColumnDataSource(data=dict( x=[x[0] for x in col.get_offsets()], y=[x[1] for x in col.get_offsets()], )) if len(hover_names) == len(self.ax.collections): #print(type(source.data)) #source.data[hover_names[i][0]]=hover_names[i][1] source.add(hover_names[i][1], name=hover_names[i][0]) pass p.circle('x', 'y', source=source, color=colors[i], legend=col.get_label()) continue p.xaxis.axis_label = self.ax.get_xlabel() p.yaxis.axis_label = self.ax.get_ylabel() hover = p.select_one(HoverTool) hover.point_policy = "follow_mouse" hover.tooltips = [ ('x', '@x'), ('y', '@y'), ] if len(hover_names) == len(self.ax.collections): hover.tooltips.append((hover_names[0][0], '@' + hover_names[0][0])) pass return p
def get_States_Sal(states_dict): # call get_Data() function to get salary data salStateData = get_data() # extract states lat and lon information for generating map states = {code: state for code, state in states_dict.items()} # sort the data by state names states_Name = sorted(states.values(), key=lambda x: x['name']) state_xs = [state["lons"] for state in states_Name] state_ys = [state["lats"] for state in states_Name] state_names = [state["name"] for state in states_Name] # create column data source source = ColumnDataSource(data=dict( x=state_xs, y=state_ys, stateN=[name for name in state_names])) # get average of salary occupation group in each state salStateAgg = pd.DataFrame( salStateData.groupby( ['STATECODE', 'stateName'], axis=0, as_index=False)['SALARYAVERAGE', 'SALARYREALTIMEAVERAGE'].mean()).reset_index() # Create colorMap dictionary keys = tuple(pd.unique(salStateAgg["SALARYREALTIMEAVERAGE"])) values = tuple([ "#000000", "#FFFF00", "#1CE6FF", "#FF34FF", "#FF4A46", "#008941", "#006FA6", "#A30059", "#FFDBE5", "#7A4900", "#0000A6", "#63FFAC", "#B79762", "#004D43", "#8FB0FF", "#997D87", "#5A0007", "#809693", "#FEFFE6", "#1B4400", "#4FC601", "#3B5DFF", "#4A3B53", "#FF2F80", "#61615A", "#BA0900", "#6B7900", "#00C2A0", "#FFAA92", "#FF90C9", "#B903AA", "#D16100", "#DDEFFF", "#000035", "#7B4F4B", "#A1C299", "#300018", "#0AA6D8", "#013349", "#00846F", "#372101", "#FFB500", "#C2FFED", "#A079BF", "#CC0744", "#C0B9B2", "#C2FF99", "#001E09", "#00489C", "#6F0062", "#0CBD66", "#EEC3FF" ]) colorMap = dict(itertools.izip(keys, values)) # add values to the source source.add(data=[str(x) for x in salStateAgg["STATECODE"]], name='statecode') source.add(data=[str(x) for x in salStateAgg["SALARYAVERAGE"]], name='salAvg') source.add(data=[str(x) for x in salStateAgg["SALARYREALTIMEAVERAGE"]], name='salRealAvg') source.add( data=[colorMap[x] for x in salStateAgg["SALARYREALTIMEAVERAGE"]], name='type_color') return source
def get_tables(self, analyzer): ''' Return a header for this analyzer and one *or more* data tables. ''' if hasattr(analyzer, 'get_analysis_table'): title, table_columns_list = analyzer.get_analysis_table() else: # Analyzer does not provide a table function. Use our generic one title, table_columns_list = __class__._get_table_generic(analyzer) # don't add empty analyzer if len(table_columns_list[0][0]) == 2: return None, None param_str = get_params_str(analyzer.params) if len(param_str) > 0: title += f' ({param_str})' elems = [] for table_columns in table_columns_list: cds = ColumnDataSource() columns = [] for i, c in enumerate(table_columns): col_name = f'col{i}' cds.add(c[2:], col_name) columns.append( TableColumn(field=col_name, title=c[0], formatter=self._get_formatter(c[1]))) # define height of column by multiplying count of rows # with ROW_HEIGHT column_height = len(table_columns[0]) * ROW_HEIGHT elems.append( DataTable( source=cds, columns=columns, index_position=None, height=column_height, width=0, # set width to 0 so there is no min_width sizing_mode='stretch_width', fit_columns=True)) return Paragraph(text=title, css_classes=['table-title']), elems
def create_elevation_chart(self): # DataFrameの値を Bokeh用セット source = ColumnDataSource(self.df) source.add(self.df.index, 'index') # チャートのツールを設定 tools = "pan,box_zoom,reset,save" # チャート内のツールチップのツールチップを設定 elevation_hover_tool = HoverTool( tooltips=[ ('日付', "@index{%F}"), ('当日獲得標高', "@Elevation"+' (m)'), ('合計獲得標高', '@Sum_elevation'+' (m)')], formatters={ '@index': 'datetime', }, mode='mouse') # figureオブジェクトを生成 # figureオブジェクトのlineプロパティに値を追加する elevation_plot = figure( plot_width=300, plot_height=300, x_axis_type="datetime", x_axis_label = "date", y_axis_label = "height(m)", title='Elevation Line_Chart', tools=[elevation_hover_tool, tools] ) elevation_plot.line( x = "index", y = "Sum_elevation", source = source, legend = "獲得標高", color = "blue", line_width = 2 ) elevation_plot.add_layout(elevation_plot.legend[0], 'below') return elevation_plot
def create_plot(N, func1, func2, color): N = 300 x = np.linspace(0, 4 * np.pi, N) y1 = np.sin(x) y2 = np.cos(x) source = ColumnDataSource() source.add(data=x, name='x') source.add(data=y1, name='y1') source.add(data=y2, name='y2') TOOLS = "pan,wheel_zoom,box_zoom,reset,save,box_select,lasso_select" s1 = plt.figure(tools=TOOLS, plot_width=350, plot_height=350) s1.scatter('x', 'y1', source=source, fill_color=color) s2 = plt.figure(tools=TOOLS, plot_width=350, plot_height=350) s2.scatter('x', 'y2', source=source, fill_color=color) p = plt.gridplot([[s1, s2]]) return p
def get_table(self, data): table = [['Name'], ['Value']] cds = ColumnDataSource() columns = [] for n, v in data.items(): table[0].append(n) table[1].append(v) for i, c in enumerate(table): col_name = f'col{i}' cds.add(c[1:], col_name) columns.append(TableColumn(field=col_name, title=c[0])) column_height = len(table[0]) * ROW_HEIGHT dtable = DataTable( source=cds, columns=columns, index_position=None, height=column_height, width=0, # set width to 0 so there is no min_width sizing_mode='stretch_width', fit_columns=True) return dtable
def create_plot(N, func1, func2, color): N = 300 x = np.linspace(0, 4*np.pi, N) y1 = np.sin(x) y2 = np.cos(x) source = ColumnDataSource() source.add(data=x, name='x') source.add(data=y1, name='y1') source.add(data=y2, name='y2') TOOLS = "pan,wheel_zoom,box_zoom,reset,save,box_select,lasso_select" s1 = plt.figure(tools=TOOLS, plot_width=350, plot_height=350) s1.scatter('x', 'y1', source=source, fill_color=color) s2 = plt.figure(tools=TOOLS, plot_width=350, plot_height=350) s2.scatter('x', 'y2', source=source, fill_color=color) p = plt.gridplot([[s1,s2]]) return p
def _make_edge_source(self, node_source=None): """ Collect Bokeh DataSource for graph edges """ G = self.graph if node_source is None: node_source = self._make_node_source() winner, loser, match_data = zip(*G.edges(data=True)) # transfer data from networkx graph edge_source = ColumnDataSource() edge_source.add(winner, 'winner') edge_source.add(loser, 'loser') for varname in ['round', 'win_score', 'los_score']: edge_source.add([m[varname] for m in match_data], varname) # collect start and end point for each match node_data = node_source.to_df()[node_source.to_df()['round']==1].set_index('team') # node_data = node_source.to_df().set_index('index') x_vals = [] y_vals = [] for w, l in zip(winner, loser): x_vals.append([node_data.loc[w]['x'], node_data.loc[l]['x']]) y_vals.append([node_data.loc[w]['y'], node_data.loc[l]['y']]) edge_source.add(x_vals, 'x_vals') edge_source.add(y_vals, 'y_vals') # calculate position of markers for winning team x_diamond = [] y_diamond = [] for xs, ys in zip(x_vals, y_vals): norm = np.sqrt((xs[1] - xs[0])**2 + (ys[1]-ys[0])**2) unit_vec_x = (xs[1] - xs[0]) / norm unit_vec_y = (ys[1] - ys[0]) / norm x_diamond.append(xs[0] + 0.2 * unit_vec_x) y_diamond.append(ys[0] + 0.2 * unit_vec_y) edge_source.add(x_diamond, 'x_diamond') edge_source.add(y_diamond, 'y_diamond') return edge_source
def plot_time_series(data, fig, fig_size=[800, 350], title="", legend="", labels=["Date", ""], linewidth=1, color="blue", linestyle="solid", alpha=1): fig.plot_width = fig_size[0] fig.plot_height = fig_size[1] source = ColumnDataSource() source.add(data.index, name="date") source.add(data.values, name="values") source.add(data.index.strftime("%Y-%m-%d %H:%M:%S"), "date_formatted") # create a line plot fig.line(source=source, x="date", y="values", legend=legend, line_color=color, line_dash=linestyle, line_alpha=alpha, line_width=linewidth) fig.title.text = title fig.title.text_font = 'Helvetica' fig.title.text_font_size = '18px' fig.title.align = 'center' fig.legend.background_fill_alpha = 0.6 fig.legend.label_text_font = 'Helvetica' fig.legend.location = 'bottom_right' fig.xaxis.axis_label = labels[0] fig.yaxis.axis_label = labels[1] hover = HoverTool(tooltips=[("Value: ", "@values"), ("Timestamp: ", "@date_formatted")]) hover.formatters = {'Timestamp': "datetime"} fig.add_tools(hover) fig.toolbar_location = "above" return fig
def _plot_unique_value(data, figsize): f = figure(x_range=list(data.index), tools="box_select, pan, reset, save") f.plot_width = figsize[0] f.plot_height = figsize[1] # Background settings f.background_fill_color = '#859dcd' f.background_fill_alpha = 0.05 # Title settings f.title.text = 'Number of Unique Values Histogram' f.title.text_font = 'Helvetica' f.title.text_font_size = '24px' f.title.align = 'center' f.title.text_font_style = "italic" # Axis settings f.xaxis.axis_label = 'Unique Values' f.xaxis.major_label_orientation = np.pi / 3 f.yaxis.axis_label = 'Frequency' f.axis.axis_label_text_font_size = '16px' f.xgrid.grid_line_color = None source = ColumnDataSource() source.add(data.index, 'features') source.add(data['nunique'], 'nunique') f.vbar(x='features', top='nunique', source=source, alpha=0.7, width=0.9, color='#3399c1') hover = HoverTool(tooltips=[('Feature', '@features'), ('# Values', '@nunique')]) f.add_tools(hover) show(f)
def _plot_word_freq(frequencies_dist, plot_n, figsize): tab_list = [] for label in frequencies_dist.keys(): freqdist = frequencies_dist[label] df = DataFrame({'word': list(freqdist.keys()), 'count': list(freqdist.values())}) df.sort_values('count', inplace=True, ascending=False) df.reset_index(inplace=True, drop=True) source = ColumnDataSource(data=df[:plot_n]) source.add(df.index[:plot_n], 'index') f = figure(x_range=list(df['word'])[:plot_n], tools="box_select, pan, reset, save") f.plot_width = figsize[0] f.plot_height = figsize[1] f.title.text = 'Word frequency for "%s"' % label f.title.text_font = 'Helvetica' f.title.text_font_size = '24px' f.title.align = 'center' f.xaxis.axis_label = 'Samples' f.yaxis.axis_label = 'Count' f.xaxis.major_label_orientation = np.pi / 2 f.line(source=source, x='word', y='count') hover = HoverTool(tooltips=[("Word", "@word"), ("Count", "@count")], mode='vline') f.add_tools(hover) tab = Panel(child=f, title=label) tab_list.append(tab) tabs = Tabs(tabs=tab_list) show(tabs)
def get_analyzers_tables( self, analyzer: bt.analyzers.Analyzer, strategy: bt.Strategy, params: Optional[bt.AutoInfoClass] ) -> (Paragraph, List[DataTable]): if hasattr(analyzer, 'get_analysis_table'): title, table_columns_list = analyzer.get_analysis_table() else: # Analyzer does not provide a table function. Use our generic one title, table_columns_list = TableGenerator._get_analysis_table_generic( analyzer) # if we have multiple strategies in the mix, add the name of the involved one if len(strategy.env.strats) > 0: title += f' ({get_strategy_label(strategy, params)})' elems: List[DataTable] = [] for table_columns in table_columns_list: cds = ColumnDataSource() columns = [] for i, c in enumerate(table_columns): col_name = f'col{i}' cds.add(c[2:], col_name) columns.append( TableColumn(field=col_name, title=c[0], formatter=TableGenerator._get_formatter(c[1]))) column_height = len(table_columns[0]) * 25 elems.append( DataTable(source=cds, columns=columns, width=self._scheme.table_width, height=column_height, row_headers=False)) return Paragraph(text=title, width=self._scheme.table_width, style={'font-size': 'large'}), elems
def plot_interactive_timeseries(df, col): # https://github.com/bokeh/bokeh/pull/3883 hover = HoverTool(tooltips=[ ("y (%s)" % col, "$y{1.11}"), ("Date", "@DateStr"), ("LastPrice", "@LastPrice{1.11}"), ("LowPrice", "@LowPrice{1.11}"), ("HighPrice", "@HighPrice{1.11}"), # ("AvgPrice", "@AvgPrice{1.11}"), ("Units", "@Units{int}"), ("Volume", "@Volume{1.11}") ]) p = figure( plot_width=bokeh.charts.defaults.width, plot_height=bokeh.charts.defaults.height, x_axis_type="datetime", title="PRES16_WTA: Date vs %s" % col) for contract, group in df.groupby('Contract'): source = ColumnDataSource(group[[x for x in group.columns if x != "AvgPrice"]]) source.add(group.Date.map(lambda x: x.strftime('%x')), 'DateStr') color = "blue" if contract == "DEM16_WTA" else ("red" if contract == "REP16_WTA" else "black") p.line(x='Date', y=col, color=color, legend=contract, line_width=2, source=source) p.add_tools(hover) p.xaxis.axis_label = "Date" p.yaxis.axis_label = col p.legend.location = "top_left" p.logo = None return p
def stackedBar(self, source, x, y, **kwargs): ''' A stacked barchart for categorical vs. categorical data. ''' if not isinstance(source, pd.core.groupby.DataFrameGroupBy): msg = "source has to be a pandas.DataFrameGroupyBy. Received " + str( type(df) ) + " Use df.groupy([" + x + "," + y + "]) to obtain valid input." raise TypeError(msg) # make sure we have a quantitative variable that is used in source.describe() cds = ColumnDataSource(source) cds.add(source.count().index.get_level_values(1), 'labels') factors = list(source[x].count().index.levels[0]) self.vbar(x=x + '_' + y, top='mycnt_count', width=1, source=cds, line_color='white') labels = LabelSet(x=x + '_' + y, y='mycnt_count', text='labels', level='glyph', text_align='center', text_font_size='6pt', y_offset=3, source=cds, render_mode='canvas') self.y_range.start = 0 self.xgrid.grid_line_color = None self.xaxis.major_label_orientation = 1.2 self.outline_line_color = None self.add_layout(labels)
cats = list(reversed(probly.keys())) palette = [cc.rainbow[i * 15] for i in range(17)] x = linspace(-20, 110, 500) source = ColumnDataSource(data=dict(x=x)) p = figure(y_range=cats, plot_width=700, x_range=(-5, 105), toolbar_location=None) for i, cat in enumerate(reversed([cats[0]])): pdf = gaussian_kde(probly[cat]) y = joy(cat, pdf(x)) source.add(y, cat) p.patch('x', cat, color=palette[i], alpha=0.6, line_color="black", source=source) source2 = ColumnDataSource(data=dict(x=x)) pdf = gaussian_kde(probly[cats[3]]) y2 = joy(cat, pdf(x), scale=30) source2.add(y2, cat) p.patch('x', cat, color=palette[(2*i) % 17], alpha=0.3, line_color="black", source=source2) # x = [1, 3, 2] # y = [(cat, 3), # (cat, 2), # (cat, 1)] # p.patch(x, y, color=palette[8], alpha=0.4, line_color="black") p.outline_line_color = None
# The plot server must be running # Go to http://localhost:5006/bokeh to view this plot import numpy as np from bokeh.plotting import * from bokeh.models import ColumnDataSource output_server("linked_brushing") N = 300 x = np.linspace(0, 4*np.pi, N) y1 = np.sin(x) y2 = np.cos(x) source = ColumnDataSource() source.add(data=x, name='x') source.add(data=y1, name='y1') source.add(data=y2, name='y2') TOOLS = "pan,wheel_zoom,box_zoom,reset,save,box_select,lasso_select" s1 = figure(tools=TOOLS, plot_width=350, plot_height=350) s1.scatter('x', 'y1', source=source) # Linked brushing in Bokeh is expressed by sharing data sources between # renderers. Note below that s2.scatter is called with the `source` # keyword argument, and supplied with the same data source from s1.scatter s2 = figure(tools=TOOLS, plot_width=350, plot_height=350) s2.scatter('x', 'y2', source=source) p = gridplot([[s1,s2]])
return list(zip([category]*len(data), scale*data)) cats = list(reversed(probly.keys())) palette = [cc.rainbow[i*15] for i in range(17)] x = linspace(-20,110, 500) source = ColumnDataSource(data=dict(x=x)) p = figure(y_range=cats, plot_width=700, x_range=(-5, 105), toolbar_location=None) for i, cat in enumerate(reversed(cats)): pdf = gaussian_kde(probly[cat]) y = ridge(cat, pdf(x)) source.add(y, cat) p.patch('x', cat, color=palette[i], alpha=0.6, line_color="black", source=source) p.outline_line_color = None p.background_fill_color = "#efefef" p.xaxis.ticker = FixedTicker(ticks=list(range(0, 101, 10))) p.xaxis.formatter = PrintfTickFormatter(format="%d%%") p.ygrid.grid_line_color = None p.xgrid.grid_line_color = "#dddddd" p.xgrid.ticker = p.xaxis[0].ticker p.axis.minor_tick_line_color = None p.axis.major_tick_line_color = None p.axis.axis_line_color = None
fig.add_tools(HoverTool(renderers=[states], tooltips=[("State", "@state"), ("Total", "@total"), ("Drunk Percent", "@perc_drunk"), ("Speeding Percent", "@perc_speeding")])) # ------------------------------------------------- from bokeh.io import output_file, show from bokeh.layouts import column from bokeh.models.widgets import Select from bokeh.palettes import Reds9 output_file("final_bokeh.html") select = Select(title="Option:", value="Default", options=["Other", "Speeding", "Drunk", "Default"]) border_source.add("white", name="color") fig = Figure(plot_width=1100, plot_height=650, x_range=(-13000000, -7000000), y_range=(2750000, 6250000), tools=["wheel_zoom", "pan"], active_scroll="wheel_zoom", webgl=True) fig.axis.visible = False STAMEN_TONER_BACKGROUND = WMTSTileSource( url='http://tile.stamen.com/toner-background/{Z}/{X}/{Y}.png', attribution=( 'Map tiles by <a href="http://stamen.com">Stamen Design</a>, ' 'under <a href="http://creativecommons.org/licenses/by/3.0">CC BY -3.0</a>.' 'Data by <a href="http://openstreetmap.org">OpenStreetMap</a>, ' 'under <a href="http://www.openstreetmap.org/copyright">ODbL</a>' )
def compute_data_source(self): source = ColumnDataSource(self.df.reset_index()[2:]) source.add(self.df[2:].LogReturns.ge(0).map(lambda x: "steelblue" if x else "red"), 'LogReturnsColor') source.add(self.df[2:].DevolLogReturns / 2., 'y_mids') return source
# example: { ..., ('user47', 'da_bjoerni', {'weight': 3}), ... } for u, v, data in _network.edges(data=True): d['xs'].append([_layout[u][0], _layout[v][0]]) d['ys'].append([_layout[u][1], _layout[v][1]]) d['alphas'].append(calc_alpha(data['weight'])) return d lines_source = ColumnDataSource(get_edges_specs(network, layout)) r_lines = plot.multi_line('xs', 'ys', line_width=1.5, alpha='alphas', color='navy', source=lines_source) centrality = networkx.algorithms.centrality.degree_centrality(network) # first element, are nodes again _, nodes_centrality = zip(*sorted(centrality.items())) nodes_source.add([7 + 10 * t / max(nodes_centrality) for t in nodes_centrality], 'centrality') partition = community.best_partition(network) p_, nodes_community = zip(*sorted(partition.items())) nodes_source.add(nodes_community, 'community') community_colors = ['#e41a1c','#377eb8','#4daf4a','#984ea3','#ff7f00','#ffff33','#a65628', '#b3cde3','#ccebc5','#decbe4','#fed9a6','#ffffcc','#e5d8bd','#fddaec','#1b9e77','#d95f02','#7570b3','#e7298a','#66a61e','#e6ab02','#a6761d','#666666'] nodes_source.add([community_colors[t % len(community_colors)] for t in nodes_community], 'community_color') r_circles.glyph.size = 'centrality' r_circles.glyph.fill_color = 'community_color' def remove_node(): idx = nodes_source.selected['1d']['indices'][0]
def create_sparkline(obj): sparkline_toolset = "box_select" # Generate a figure container plot = plotting.figure( height=150, width=PLOTS_WIDTH, tools=sparkline_toolset, x_axis_location="above", y_axis_label='Adj. Close Price', # title=obj.selected_symbol, x_axis_type="datetime", toolbar_location=None, outline_line_color=None, name="small_plot" ) plot.min_border_bottom = plot.min_border_top = 0 plot.background_fill = '#EEEEEE' plot.border_fill = "white" # Create source and glyphs for the P&D orange boxes psource = ColumnDataSource(obj.intervals_source.data) glyph = Quad(top='values', bottom='bottom', left='start', right='end', fill_color='orange', fill_alpha=0.3, line_alpha=0) empty_glyph = Quad(top='values', bottom='bottom', left='start', right='end', fill_color='orange', fill_alpha=0.3, line_alpha=0) plot.add_glyph( psource, glyph, selection_glyph=glyph, nonselection_glyph=empty_glyph ) # psource.tags = ['pump_and_dumps'] # Create source and glyphs for the light blue boxes showed # when an interval is selected selection_source = ColumnDataSource() for k in ['end', 'values', 'start', 'bottom']: selection_source.add([], k) plot.quad(top='values', bottom='bottom', left='start', right='end', source=selection_source, color='#c6dbef', fill_alpha=0.5) for k in ['scatter', 'line']: plot = factories.makers[k]( 'dt', ['price'], source=obj.source, colors=['#666666'], p=plot ) obj.source.tags = ['min_source'] # Customize select tool select_tool = plot.select(dict(type=BoxSelectTool)) select_tool.dimensions = ['width'] zoom_tool = plot.select(dict(type=BoxZoomTool)) zoom_tool.dimensions = ['width'] maxval = max(obj.source.data['price']) objs = { # TODO: PASSING A PLOT OR ITS RANGE TO A CB MAKES IT BREAK EVT TAPTOOL!!!!! 'spark_source': obj.source, 'selection_source': selection_source, 'evts_source': obj._plugins[0].evts_source, 'spam_source': obj.spam_source, 'sec_source': obj.sec_source, } callback = Callback(args=objs, code=callbacks.small_selection_plot % maxval) obj.source.callback = callback plot.ygrid.grid_line_color = None style_axis(plot) plot.tags = ['small_plot'] add_evt_plugins(obj, plot, copy_source=True) return plot
palette = [cc.rainbow[i * 15] for i in range(len(cats))] bins = np.linspace(0., 1., N_HIST) delta = (bins[1] - bins[0]) center = (bins[:-1] + bins[1:]) / 2 xr = np.linspace(-0.5, 1.5, 100) plots = [] for ld in lang_dirs: p = figure(x_range=(0., 1.), y_range=cats, plot_width=400, plot_height=500, toolbar_location=None, title='Language: %s' % ld) nframes = stats_store[ld][Stats.len] * 3600. / 10e-3 h = stats_store[ld][Stats.hist] / nframes source = ColumnDataSource(data=dict(x=xr)) for at_id, at in enumerate(ATTRIBUTES_CLS[attrib_type]): pdf = gaussian_kde(h[at_id, :]) y = joy(at, pdf(xr), scale=0.2) source.add(y, at) p.patch('x', at, color=palette[at_id], alpha=0.6, line_color="black", source=source) p.y_range.range_padding = 0.12 plots.append(p) bio.show(bl.column(*plots)) # === # Violin Figures # === fig, axs = plt.subplots(len(lang_dirs), 1, figsize=(10, 30), facecolor='w', edgecolor='k') fig.subplots_adjust(hspace=0.01, wspace=.5) axs = axs.ravel() xp = np.linspace(0., 1.1, 100) for i, ld in enumerate(lang_dirs):
from bokeh.models import ColumnDataSource from bokeh.plotting import * import numpy as np output_file("static_plot.html") N = 300 x = np.linspace(0, 4 * np.pi, N) y1 = np.sin(x) y2 = np.cos(x) source = ColumnDataSource() source.add(data=x, name="x") source.add(data=y1, name="y1") source.add(data=y2, name="y2") TOOLS = "pan,wheel_zoom,box_zoom,reset,save,box_select,lasso_select" s1 = figure(tools=TOOLS, plot_width=350, plot_height=350) s1.scatter("x", "y1", source=source) s2 = figure(tools=TOOLS, plot_width=350, plot_height=350) s2.scatter("x", "y2", source=source) p = gridplot([[s1, s2]]) show(p)