我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用bokeh.plotting.ColumnDataSource()。
def plot_bokeh(df,sublist,filename): lenlist=[0] df_sub = df[df['cuisine']==sublist[0]] lenlist.append(df_sub.shape[0]) for cuisine in sublist[1:]: temp = df[df['cuisine']==cuisine] df_sub = pd.concat([df_sub, temp],axis=0,ignore_index=True) lenlist.append(df_sub.shape[0]) df_X = df_sub.drop(['cuisine','recipeName'],axis=1) print df_X.shape, lenlist dist = squareform(pdist(df_X, metric='cosine')) tsne = TSNE(metric='precomputed').fit_transform(dist) #cannot use seaborn palette for bokeh palette =['red','green','blue','yellow'] colors =[] for i in range(len(sublist)): for j in range(lenlist[i+1]-lenlist[i]): colors.append(palette[i]) #plot with boken output_file(filename) source = ColumnDataSource( data=dict(x=tsne[:,0],y=tsne[:,1], cuisine = df_sub['cuisine'], recipe = df_sub['recipeName'])) hover = HoverTool(tooltips=[ ("cuisine", "@cuisine"), ("recipe", "@recipe")]) p = figure(plot_width=1000, plot_height=1000, tools=[hover], title="flavor clustering") p.circle('x', 'y', size=10, source=source,fill_color=colors) show(p)
def update(): # Compute new y values: y y = np.sin(x) + np.random.random(N) # Update the ColumnDataSource data dictionary source.data = {'x': x, 'y': y} # Add the update callback to the button
def __init__(self, simul, probes, line_kwargs={}, fig_kwargs={}, default_fig_kwargs={"width": 600, "height": 400}, default_line_kwargs={}, notebook=True): from bokeh.io import push_notebook, output_notebook from bokeh.plotting import figure, show, ColumnDataSource from bokeh.layouts import Column if notebook: output_notebook() setattr(self, '_push', push_notebook) self._datasource = ColumnDataSource(dict(t=[simul.t], **{name: [probe(simul.t, simul.fields)] for name, probe in probes.items()})) figs = {} for name, probe in probes.items(): fig_config = default_fig_kwargs.copy() fig_config.update(fig_kwargs.get(name, {})) line_config = default_line_kwargs.copy() line_config.update(line_kwargs.get(name, {})) figs[name] = figure(**fig_config, title=name) figs[name].line('t', name, source=self._datasource, **line_config) self._handler = show(Column(*[figs[name] for name in probes]), notebook_handle=True) self._probes = probes
def __init__(self, epoch_freq=1, y_range=(0, 4.5), fig=None, handle=None, update_thresh_s=0.65, w=400, h=300, nepochs=1.0, total_batches=10.0, train_source=None, val_source=None, history=10): self.update_thresh_s = update_thresh_s self.w = w self.h = h self.nepochs = nepochs self.total = total_batches self.last_update = 0 self.epoch = -1 self.history = history self.cost_history = deque(maxlen=history) if handle is None: output_notebook() self.handle = None else: self.handle = handle if fig is None: self.fig = figure(name="cost", y_axis_label="Cost", x_range=(0, self.nepochs), y_range=y_range, x_axis_label="Epoch", plot_width=self.w, plot_height=self.h) else: self.fig = fig if train_source is None: self.train_source = ColumnDataSource(data=dict(x=[], y=[])) else: self.train_source = train_source self.train_source.data = dict(x=[], y=[]) self.train_cost = self.fig.line('x', 'y', source=self.train_source) if val_source is None: self.val_source = ColumnDataSource(data=dict(x=[], y=[])) else: self.val_source = val_source self.val_source.data = dict(x=[], y=[]) self.val_cost = self.fig.line('x', 'y', source=self.val_source, color='red')
def scatterplot_vis(df, **kwargs): plot_width = kwargs.get('plot_width',300) plot_height = kwargs.get('plot_height',300) size = kwargs.get('size',10) hover = HoverTool( tooltips=""" <div> <div> <img src="@img_filepath" height="200" alt="@img_filepath" width="200" style="float: left; margin: 0px 15px 15px 0px;" border="2" ></img> </div> </div> """ ) p = figure(plot_width=plot_width, plot_height=plot_height, toolbar_location = 'right', tools='pan,box_zoom,wheel_zoom,reset,resize') p.add_tools(hover) df['label_color'] = df['label'].apply(lambda x: COLOR_PALETTE.as_hex()[int(x)]) source = ColumnDataSource(df) circles = p.circle('x', 'y', size=size, source=source) circles.glyph.fill_color = 'label_color' output_notebook() show(p)
def scatterplot_text(df, **kwargs): plot_width = kwargs.get('plot_width',300) plot_height = kwargs.get('plot_height',300) size = kwargs.get('size',10) hover = HoverTool( tooltips=""" <div> <div> <p> @text </p> </div> </div> """ ) p = figure(plot_width=plot_width, plot_height=plot_height, toolbar_location = 'right', tools='pan,box_zoom,wheel_zoom,reset,resize') p.add_tools(hover) df['label_color'] = df['label'].apply(lambda x: COLOR_PALETTE.as_hex()[int(x)]) source = ColumnDataSource(df) circles = p.circle('x', 'y', size=size, source=source) circles.glyph.fill_color = 'label_color' output_notebook() show(p)
def make_plot(results, title, xlabel, ylabel, baseline, ycolname, yaxis_format): p = plotting.figure(plot_width=WIDTH, plot_height=250, title=title, x_axis_label=xlabel, y_axis_label=ylabel, toolbar_location="above", tools='box_zoom,reset') legend_items = [] baseline_times = [t * 1e6 for t in results[baseline]['times']] for i, (impl_name, impl_data) in enumerate(results.items()): color = IMPL_COLORS[i % len(IMPL_COLORS)] style = IMPL_STYLES[i % len(IMPL_STYLES)] data = dict(x=impl_data['x']) # convert to microseconds data['times'] = [t * 1e6 for t in impl_data['times']] data['name'] = [impl_name] * len(data['x']) data['speedup'] = [b/t for (t,b) in zip(data['times'], baseline_times)] # not this is items/sec data['throughput'] = [items/t for (t, items) in zip(impl_data['times'], impl_data['x'])] source = plotting.ColumnDataSource(data=data) line = p.line('x', ycolname, source=source, line_width=2, line_color=color, line_dash=style) marker = p.circle('x', ycolname, source=source, size=10, fill_color=color) legend_items.append( (impl_name, [line, marker]) ) hover = HoverTool( tooltips=[ ('implementation', '@name'), ('x', '@x{%1.0e}'), ('time per call', '@times{%1.1e} usec'), ('speedup', '@{speedup}{%1.1f}x'), ('throughput', '@{throughput}{%1.1e}'), ], formatters={ 'x': 'printf', 'times': 'printf', 'speedup': 'printf', 'throughput': 'printf', } ) p.add_tools(hover) p.xaxis[0].formatter = PrintfTickFormatter(format='%1.0e') p.yaxis[0].formatter = PrintfTickFormatter(format=yaxis_format) legend = Legend(items=legend_items, location=(0, -30)) p.add_layout(legend, 'right') return p
def __init__(self, simul, keys=None, line_kwargs={}, fig_kwargs={}, default_fig_kwargs={"width": 600, "height": 400}, default_line_kwargs={}, notebook=True, stack=False): from bokeh.io import push_notebook, output_notebook from bokeh.plotting import figure, show, ColumnDataSource from bokeh.layouts import Column if notebook: output_notebook() setattr(self, '_push', push_notebook) keys = keys if keys else [ key for key in simul.fields.keys() if key != 'x'] self._datafunc = {'x': lambda t, fields, key: fields.x} for key in keys: if isinstance(key, str): self._datafunc[key] = lambda t, fields, key: fields[key] if isinstance(key, (tuple, list)): self._datafunc[key[0]] = key[1] self._datasource = ColumnDataSource({key: func(simul.t, simul.fields, key) for (key, func) in self._datafunc.items()}) self.keys = list(self._datafunc.keys()) self.keys.remove("x") if stack: fig = figure(**default_fig_kwargs) for key in self.keys: line_config = default_line_kwargs.copy() line_config.update(line_kwargs.get(key, {})) fig.line('x', key, source=self._datasource, **line_kwargs.get(key, {})) self._handler = show(fig, notebook_handle=True) return else: figs = {} for key in self.keys: fig_config = default_fig_kwargs.copy() fig_config.update(fig_kwargs.get(key, {})) line_config = default_line_kwargs.copy() line_config.update(line_kwargs.get(key, {})) figs[key] = figure(**fig_config, title=key) figs[key].line('x', key, source=self._datasource, **line_config) self._handler = show(Column(*[figs[key] for key in self._datafunc.keys() if key != 'x']), notebook_handle=True)
def scatter_with_hover(x, y, in_notebook=True, show_plt=True, fig=None, name=None, marker='o', fig_width=500, fig_height=500, x_label=None, y_label=None, title=None, color="blue"): """ Plots an interactive scatter plot of `x` vs `y` using bokeh, with automatic tooltips showing columns from `df`. Modified from: http://blog.rtwilson.com/bokeh-plots-with-dataframe-based-tooltips/ Args: x (numpy.ndarray): The data for the x-axis. y (numpy.ndarray): The data for the y-axis. fig (bokeh.plotting.Figure, optional): Figure on which to plot (if not given then a new figure will be created) name (str, optional): Series name to give to the scattered data marker (str, optional): Name of marker to use for scatter plot Returns: fig (bokeh.plotting.Figure): Figure (the same as given, or the newly created figure) if show is False """ # Make it so it works for ipython. if in_notebook: #pragma: no cover output_notebook() # insert the correct hover identifier. hover = HoverTool(tooltips=[("entry#", "@label"),]) # If we haven't been given a Figure obj then create it with default # size etc. if fig is None: # if title is None: # fig = figure(width=fig_width, height=fig_height, tools=['box_zoom', 'reset',hover]) # else: fig = figure(width=fig_width, height=fig_height, tools=['box_zoom', 'reset',hover],title=title) # We're getting data from the given dataframe source = ColumnDataSource(data=dict(x=x,y=y,label=range(1,len(x)+1))) # Actually do the scatter plot - the easy bit # (other keyword arguments will be passed to this function) fig.scatter('x', 'y', source=source, marker=marker,color=color,name=name) if x_label is not None: fig.xaxis.axis_label = x_label if y_label is not None: fig.yaxis.axis_label = y_label if show_plt: # pragma: no cover show(fig) else: return(fig)