def lineChart(df, xlabel, vFields, color=None, clustered=None, title=None): ylabel = ','.join(v for v in vFields) x = list(df[xlabel].values) if df[xlabel].dtype == object: p = figure(y_axis_label=ylabel, x_axis_label=xlabel, title=title, x_range=x, **self.get_common_figure_options()) else: p = figure(y_axis_label=ylabel, x_axis_label=xlabel, title=title, **self.get_common_figure_options()) if clustered is not None: colors = self.colorPalette(len(df[clustered].unique())) if color is None else color df[clustered] = df[clustered].astype(str) for j,c in enumerate(list(df[clustered].unique())) if clustered else enumerate([None]): df2 = df[df[clustered] == c] if c else df df2 = df2.drop(clustered, axis=1) if c else df2 for i,v in enumerate(vFields): y = list(df2[v].values) l = v if self.isSubplot() else c p.line(x, y, line_width=2, color=colors[i] if self.isSubplot() else colors[j], legend=l if self.showLegend() else None) else: colors = self.colorPalette(len(vFields)) if color is None else color for i,v in enumerate(vFields): y = list(df[v].values) p.line(x, y, line_width=2, color=colors[i], legend=v if self.showLegend() else None) p.legend.location = "top_left" hover = HoverTool() hover.tooltips = [(xlabel, '@x'), (ylabel, '@y{0.00}'), ('x', '$x'), ('y', '$y')] p.add_tools(hover) return p
def __init__(self, **kwargs): data = self.processing_update({'processing': {}, 'ncores': {}}) self.source = ColumnDataSource(data) x_range = Range1d(-1, 1) fig = figure( title='Processing and Pending', tools='resize', x_range=x_range, id='bk-processing-stacks-plot', **kwargs) fig.quad(source=self.source, left=0, right='right', color=Spectral9[0], top='top', bottom='bottom') fig.xaxis.minor_tick_line_alpha = 0 fig.yaxis.visible = False fig.ygrid.visible = False hover = HoverTool() fig.add_tools(hover) hover = fig.select(HoverTool) hover.tooltips = """ <div> <span style="font-size: 14px; font-weight: bold;">Host:</span> <span style="font-size: 10px; font-family: Monaco, monospace;">@name</span> </div> <div> <span style="font-size: 14px; font-weight: bold;">Processing:</span> <span style="font-size: 10px; font-family: Monaco, monospace;">@processing</span> </div> """ hover.point_policy = 'follow_mouse' self.root = fig
def nbytes_plot(**kwargs): data = {"name": [], "left": [], "right": [], "center": [], "color": [], "percent": [], "MB": [], "text": []} source = ColumnDataSource(data) fig = figure(title="Memory Use", tools="", toolbar_location=None, id="bk-nbytes-plot", **kwargs) fig.quad(source=source, top=1, bottom=0, left="left", right="right", color="color", alpha=1) fig.grid.grid_line_color = None fig.grid.grid_line_color = None fig.axis.visible = None fig.outline_line_color = None hover = HoverTool() fig.add_tools(hover) hover = fig.select(HoverTool) hover.tooltips = """ <div> <span style="font-size: 14px; font-weight: bold;">Name:</span> <span style="font-size: 10px; font-family: Monaco, monospace;">@name</span> </div> <div> <span style="font-size: 14px; font-weight: bold;">Percent:</span> <span style="font-size: 10px; font-family: Monaco, monospace;">@percent</span> </div> <div> <span style="font-size: 14px; font-weight: bold;">MB:</span> <span style="font-size: 10px; font-family: Monaco, monospace;">@MB</span> </div> """ hover.point_policy = "follow_mouse" return source, fig
def data_usage_plot(df): def create_data_source(df): return ColumnDataSource(data=dict(filesystem=df['filesystem'],total_size=df['total_size'],used=df['used'],available=df['available'],use_percent=df['use_percent'],mounted=['mounted'],submittime=[x.strftime("%Y-%m-%d") for x in df['submittime']])) hover = HoverTool(names=['points']) TOOLS = [BoxZoomTool(),PanTool(),ResetTool(),WheelZoomTool(),hover] Colors = ['red','navy','olive','firebrick','lightskyblue','yellowgreen','lightcoral','yellow', 'green','blue','gold'] # Change percents to format bokeh can use fp_list = [] for row in df['use_percent']: fp_list.append(float(row.replace('%',''))) df['f_percent'] = fp_list # Begin Plotting x_max = datetime.now() p = figure(height=500, width=1000, x_axis_type="datetime", x_range=((x_max-timedelta(14)),x_max), y_axis_label='Percent Full', tools=TOOLS, title='Data Usage by Filesystem') # Points with hover info p.scatter(x=df['submittime'], y=df['f_percent'], name='points', source=create_data_source(df), color='black', size=6) df = df.groupby(by = ['filesystem']) # Connecting lines count = 0 for filesystem, group in df: count += 1 p.line(x=group['submittime'] ,y=group['f_percent'], color=Colors[count], legend=str(filesystem), line_width=3) # Formating p.legend.orientation = "top_left" hover.point_policy = "follow_mouse" hover.tooltips = [("Filesystem", "@filesystem"),("Size","@total_size"),("Available","@available"),("Percent Used","@use_percent"),("Time","@submittime")] return p
def createBokehChart(self): keyFields = self.getKeyFields() valueFields = self.getValueFields() color = self.options.get("color") xlabel = keyFields[0] ylabel = valueFields[0] wpdf = self.getWorkingPandasDataFrame().copy() colors = self.colorPalette(None if color is None else len(wpdf[color].unique())) p = figure(y_axis_label=ylabel, x_axis_label=xlabel) for i,c in enumerate(list(wpdf[color].unique())) if color else enumerate([None]): wpdf2 = wpdf[wpdf[color] == c] if c else wpdf p.circle(list(wpdf2[xlabel]), list(wpdf2[ylabel]), color=colors[i], legend=str(c) if c and self.showLegend() else None, fill_alpha=0.5, size=8) p.xaxis.axis_label = xlabel p.yaxis.axis_label = ylabel p.legend.location = "top_left" hover = HoverTool() hover.tooltips = [(xlabel, '@x'), (ylabel, '@y')] p.add_tools(hover) return p
def nbytes_plot(**kwargs): data = {'name': [], 'left': [], 'right': [], 'center': [], 'color': [], 'percent': [], 'MB': [], 'text': []} source = ColumnDataSource(data) fig = figure(title='Memory Use', tools='', toolbar_location=None, **kwargs) fig.quad(source=source, top=1, bottom=0, left='left', right='right', color='color', alpha=0.8) fig.text(source=source, x='center', y=0.5, text='text', text_baseline='middle', text_align='center') fig.grid.grid_line_color = None fig.grid.grid_line_color = None fig.axis.visible = None fig.outline_line_color = None hover = HoverTool() fig.add_tools(hover) hover = fig.select(HoverTool) hover.tooltips = """ <div> <span style="font-size: 14px; font-weight: bold;">Name:</span> <span style="font-size: 10px; font-family: Monaco, monospace;">@name</span> </div> <div> <span style="font-size: 14px; font-weight: bold;">Percent:</span> <span style="font-size: 10px; font-family: Monaco, monospace;">@percent</span> </div> <div> <span style="font-size: 14px; font-weight: bold;">MB:</span> <span style="font-size: 10px; font-family: Monaco, monospace;">@MB</span> </div> """ hover.point_policy = 'follow_mouse' return source, fig
def tagsDistributionScatterPlot(NbTags, dates, plotname='Tags Distribution Plot'): output_file(plotname + ".html") counts = {} glyphs = {} desc = {} hover = HoverTool() plot = figure(plot_width=800, plot_height=800, x_axis_type="datetime", x_axis_label='Date', y_axis_label='Number of tags', tools=[hover]) for name in NbTags.keys(): desc[name] = [] for date in dates[name]: desc[name].append(date_tools.datetimeToString(date, "%Y-%m-%d")) counts[name] = plot.circle(dates[name], NbTags[name], legend="Number of events with y tags", source=ColumnDataSource( data=dict( desc=desc[name] ) )) glyphs[name] = counts[name].glyph glyphs[name].size = int(name) * 2 hover.tooltips = [("date", "@desc")] if int(name) != 0: glyphs[name].fill_alpha = 1/int(name) show(plot)
def __init__(self, scheduler, **kwargs): with log_errors(): self.scheduler = scheduler self.source = ColumnDataSource({'occupancy': [0, 0], 'worker': ['a', 'b'], 'x': [0.0, 0.1], 'y': [1, 2], 'ms': [1, 2]}) fig = figure(title='Occupancy', tools='resize', id='bk-occupancy-plot', x_axis_type='datetime', **kwargs) fig.rect(source=self.source, x='x', width='ms', y='y', height=1, color='blue') fig.xaxis.minor_tick_line_alpha = 0 fig.yaxis.visible = False fig.ygrid.visible = False # fig.xaxis[0].formatter = NumeralTickFormatter(format='0.0s') fig.x_range.start = 0 hover = HoverTool() hover.tooltips = "@worker : @occupancy s" hover.point_policy = 'follow_mouse' fig.add_tools(hover) self.root = fig
def plot_lines(df, fig, x, y, group): legends = [] groups = df.groupby(by=[group]) numlines = len(groups) colors=Spectral11[0:numlines] for i, (key, grp) in enumerate(groups): grp = grp.sort_values(by=x,axis=0) name = str(key) source = ColumnDataSource(data=grp) line = fig.line(x, y, source=source, line_width=4, line_color=colors[i]) point = fig.circle(x, y, source=source, name=name, size=8, fill_color=colors[i]) legends.append((name, [line])) hover = HoverTool(names=[name]) hover.tooltips = [(c, '@' + c) for c in grp.columns] hover.tooltips.append(('index', '$index')) fig.add_tools(hover) # place a legend outside the plot area # http://bokeh.pydata.org/en/dev/docs/user_guide/styling.html#outside-the-plot-area legend = Legend(legends=legends, location=(0, -30), name="foppo") fig.add_layout(legend, 'right') return fig
def make_plot(yscale='linear'): # configure the tools width_zoom={'dimensions':['width']} tools = [tool(**width_zoom) for tool in [BoxZoomTool, WheelZoomTool]] hover_pos = HoverTool( names=['buy','sell'], tooltips=[ ('Position','@pos'), ('Date','@date'), ('Price','@price') ] ) hover_pos.name = 'Postions' tools.extend([PanTool(), hover_pos, ResetTool(), CrosshairTool()]) # prepare plot p = Figure(plot_height=600, plot_width=800, title="Moving Average Positions", x_axis_type='datetime', y_axis_type=yscale, tools=tools) p.line(x='date', y='price', alpha=0.3, color='Black', line_width=2, source=source, legend='Close', name='price') p.line(x='date', y='mav_short', color='DarkBlue', line_width=2, source=source, legend='Short MAV') p.line(x='date', y='mav_long', color='FireBrick', line_width=2, source=source, legend='Long MAV') p.inverted_triangle(x='x', y='y', color='Crimson', size=20, alpha=0.7, source=sell, legend='Sell', name='sell') p.triangle(x='x', y='y', color='ForestGreen', size=20, alpha=0.7, source=buy, legend='Buy', name='buy') return p
def task_stream_plot(sizing_mode="scale_width", **kwargs): data = { "start": [], "duration": [], "key": [], "name": [], "color": [], "worker": [], "y": [], "worker_thread": [], "alpha": [], } source = ColumnDataSource(data) x_range = DataRange1d(range_padding=0) fig = figure( x_axis_type="datetime", title="Task stream", tools="xwheel_zoom,xpan,reset,box_zoom", toolbar_location="above", sizing_mode=sizing_mode, x_range=x_range, id="bk-task-stream-plot", **kwargs ) fig.rect( x="start", y="y", width="duration", height=0.8, fill_color="color", line_color="color", line_alpha=0.6, alpha="alpha", line_width=3, source=source, ) fig.xaxis.axis_label = "Time" fig.yaxis.axis_label = "Worker Core" fig.ygrid.grid_line_alpha = 0.4 fig.xgrid.grid_line_color = None fig.min_border_right = 35 fig.yaxis[0].ticker.num_minor_ticks = 0 hover = HoverTool() fig.add_tools(hover) hover = fig.select(HoverTool) hover.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> """ hover.point_policy = "follow_mouse" return source, fig
def worker_table_plot(**kwargs): """ Column data source and plot for host table """ with log_errors(): # names = ['host', 'cpu', 'memory_percent', 'memory', 'cores', 'processes', # 'processing', 'latency', 'last-seen', 'disk-read', 'disk-write', # 'network-send', 'network-recv'] names = ['processes', 'disk-read', 'cores', 'cpu', 'disk-write', 'memory', 'last-seen', 'memory_percent', 'host'] source = ColumnDataSource({k: [] for k in names}) columns = {name: TableColumn(field=name, title=name.replace('_percent', ' %')) for name in names} cnames = ['host', 'cores', 'processes', 'memory', 'cpu', 'memory_percent'] formatters = {'cpu': NumberFormatter(format='0.0 %'), 'memory_percent': NumberFormatter(format='0.0 %'), 'memory': NumberFormatter(format='0 b'), 'latency': NumberFormatter(format='0.00000'), 'last-seen': NumberFormatter(format='0.000'), 'disk-read': NumberFormatter(format='0 b'), 'disk-write': NumberFormatter(format='0 b'), 'net-send': NumberFormatter(format='0 b'), 'net-recv': NumberFormatter(format='0 b')} table = DataTable(source=source, columns=[columns[n] for n in cnames], **kwargs) for name in cnames: if name in formatters: table.columns[cnames.index(name)].formatter = formatters[name] x_range = Range1d(0, 1) y_range = Range1d(-.1, .1) mem_plot = figure(title="Memory Usage (%)", tools='box_select', height=90, width=600, x_range=x_range, y_range=y_range, toolbar_location=None) mem_plot.circle(source=source, x='memory_percent', y=0, size=10, alpha=0.5) mem_plot.yaxis.visible = False mem_plot.xaxis.minor_tick_line_width = 0 mem_plot.ygrid.visible = False mem_plot.xaxis.minor_tick_line_alpha = 0 hover = HoverTool() mem_plot.add_tools(hover) hover = mem_plot.select(HoverTool) hover.tooltips = """ <div> <span style="font-size: 10px; font-family: Monaco, monospace;">@host: </span> <span style="font-size: 10px; font-family: Monaco, monospace;">@memory_percent</span> </div> """ hover.point_policy = 'follow_mouse' return source, [mem_plot, table]
def progress_plot(height=300, width=800, **kwargs): from ..diagnostics.progress_stream import progress_quads data = progress_quads({'all': {}, 'in_memory': {}, 'erred': {}, 'released': {}}) source = ColumnDataSource(data) fig = figure(width=width, height=height, tools=['resize'], responsive=True, **kwargs) fig.quad(source=source, top='top', bottom='bottom', left=0, right=1, color='#aaaaaa', alpha=0.2) fig.quad(source=source, top='top', bottom='bottom', left=0, right='released_right', color='#0000FF', alpha=0.4) fig.quad(source=source, top='top', bottom='bottom', left='released_right', right='in_memory_right', color='#0000FF', alpha=0.8) fig.quad(source=source, top='top', bottom='bottom', left='erred_left', right=1, color='#000000', alpha=0.3) fig.text(source=source, text='fraction', y='center', x=-0.01, text_align='right', text_baseline='middle') fig.text(source=source, text='name', y='center', x=1.01, text_align='left', text_baseline='middle') fig.scatter(x=[-0.2, 1.4], y=[0, 5], alpha=0) fig.xgrid.grid_line_color = None fig.ygrid.grid_line_color = None fig.axis.visible = None fig.min_border_left = 0 fig.min_border_right = 10 fig.min_border_top = 0 fig.min_border_bottom = 0 fig.outline_line_color = None hover = HoverTool() fig.add_tools(hover) hover = fig.select(HoverTool) hover.tooltips = """ <div> <span style="font-size: 14px; font-weight: bold;">Name:</span> <span style="font-size: 10px; font-family: Monaco, monospace;">@name</span> </div> <div> <span style="font-size: 14px; font-weight: bold;">All:</span> <span style="font-size: 10px; font-family: Monaco, monospace;">@all</span> </div> <div> <span style="font-size: 14px; font-weight: bold;">In Memory:</span> <span style="font-size: 10px; font-family: Monaco, monospace;">@in_memory</span> </div> <div> <span style="font-size: 14px; font-weight: bold;">Erred:</span> <span style="font-size: 10px; font-family: Monaco, monospace;">@erred</span> </div> """ hover.point_policy = 'follow_mouse' return source, fig
def progress_plot(**kwargs): from ..diagnostics.progress_stream import progress_quads data = progress_quads({'all': {}, 'in_memory': {}, 'erred': {}, 'released': {}}) x_range = Range1d(-0.5, 1.5) y_range = Range1d(5.1, -0.1) source = ColumnDataSource(data) fig = figure(tools='', toolbar_location=None, y_range=y_range, x_range=x_range, **kwargs) fig.quad(source=source, top='top', bottom='bottom', left=0, right=1, color='#aaaaaa', alpha=0.2) fig.quad(source=source, top='top', bottom='bottom', left=0, right='released_right', color=Spectral9[0], alpha=0.4) fig.quad(source=source, top='top', bottom='bottom', left='released_right', right='in_memory_right', color=Spectral9[0], alpha=0.8) fig.quad(source=source, top='top', bottom='bottom', left='erred_left', right=1, color='#000000', alpha=0.3) fig.text(source=source, text='fraction', y='center', x=-0.01, text_align='right', text_baseline='middle') fig.text(source=source, text='name', y='center', x=1.01, text_align='left', text_baseline='middle') fig.xgrid.grid_line_color = None fig.ygrid.grid_line_color = None fig.axis.visible = None fig.outline_line_color = None hover = HoverTool() fig.add_tools(hover) hover = fig.select(HoverTool) hover.tooltips = """ <div> <span style="font-size: 14px; font-weight: bold;">Name:</span> <span style="font-size: 10px; font-family: Monaco, monospace;">@name</span> </div> <div> <span style="font-size: 14px; font-weight: bold;">All:</span> <span style="font-size: 10px; font-family: Monaco, monospace;">@all</span> </div> <div> <span style="font-size: 14px; font-weight: bold;">In Memory:</span> <span style="font-size: 10px; font-family: Monaco, monospace;">@in_memory</span> </div> <div> <span style="font-size: 14px; font-weight: bold;">Erred:</span> <span style="font-size: 10px; font-family: Monaco, monospace;">@erred</span> </div> """ hover.point_policy = 'follow_mouse' return source, fig
def _draw_contigCirclePlot(self): hover = HoverTool(tooltips=[('Length', '@contigs')]) hover.point_policy = "follow_mouse" plot = figure(x_axis_type=None, y_axis_type=None, tools=[hover], title='Contig lengths') plot.annular_wedge(x=0, y=0, inner_radius=0.5, outer_radius=0.7, start_angle='start', end_angle='stop', color='colors', alpha=0.9, source=self.contig_dist_src) plot.yaxis.axis_label_text_font_size = '14pt' plot.xaxis.axis_label_text_font_size = '14pt' plot.yaxis.major_label_text_font_size = '14pt' plot.xaxis.major_label_text_font_size = '14pt' plot.title.text_font_size = '16pt' return plot
def progress_plot(**kwargs): with log_errors(): from ..diagnostics.progress_stream import progress_quads data = progress_quads({"all": {}, "memory": {}, "erred": {}, "released": {}}) y_range = Range1d(-8, 0) source = ColumnDataSource(data) fig = figure(tools="", toolbar_location=None, y_range=y_range, id="bk-progress-plot", **kwargs) fig.quad(source=source, top="top", bottom="bottom", left="left", right="right", color="#aaaaaa", alpha=0.2) fig.quad(source=source, top="top", bottom="bottom", left="left", right="released-loc", color="color", alpha=0.6) fig.quad( source=source, top="top", bottom="bottom", left="released-loc", right="memory-loc", color="color", alpha=1 ) fig.quad( source=source, top="top", bottom="bottom", left="erred-loc", right="erred-loc", color="#000000", alpha=0.3 ) fig.text(source=source, text="show-name", y="bottom", x="left", x_offset=5, text_font_size="10pt") fig.text( source=source, text="done", y="bottom", x="right", x_offset=-5, text_align="right", text_font_size="10pt" ) fig.xaxis.visible = False fig.yaxis.visible = False fig.grid.grid_line_alpha = 0 hover = HoverTool() fig.add_tools(hover) hover = fig.select(HoverTool) hover.tooltips = """ <div> <span style="font-size: 14px; font-weight: bold;">Name:</span> <span style="font-size: 10px; font-family: Monaco, monospace;">@name</span> </div> <div> <span style="font-size: 14px; font-weight: bold;">All:</span> <span style="font-size: 10px; font-family: Monaco, monospace;">@all</span> </div> <div> <span style="font-size: 14px; font-weight: bold;">Memory:</span> <span style="font-size: 10px; font-family: Monaco, monospace;">@memory</span> </div> <div> <span style="font-size: 14px; font-weight: bold;">Erred:</span> <span style="font-size: 10px; font-family: Monaco, monospace;">@erred</span> </div> """ hover.point_policy = "follow_mouse" return source, fig
def progress_plot(**kwargs): with log_errors(): from ..diagnostics.progress_stream import progress_quads data = progress_quads({'all': {}, 'memory': {}, 'erred': {}, 'released': {}}) y_range = Range1d(-8, 0) source = ColumnDataSource(data) fig = figure(tools='', toolbar_location=None, y_range=y_range, **kwargs) fig.quad(source=source, top='top', bottom='bottom', left='left', right='right', color='#aaaaaa', alpha=0.2) fig.quad(source=source, top='top', bottom='bottom', left='left', right='released-loc', color=Spectral9[0], alpha=0.4) fig.quad(source=source, top='top', bottom='bottom', left='released-loc', right='memory-loc', color=Spectral9[0], alpha=0.8) fig.quad(source=source, top='top', bottom='bottom', left='erred-loc', right='erred-loc', color='#000000', alpha=0.3) fig.text(source=source, text='show-name', y='bottom', x='left', x_offset=5, text_font_size='10pt') fig.text(source=source, text='done', y='bottom', x='right', x_offset=-5, text_align='right', text_font_size='10pt') fig.xaxis.visible = False fig.yaxis.visible = False fig.grid.grid_line_alpha = 0 hover = HoverTool() fig.add_tools(hover) hover = fig.select(HoverTool) hover.tooltips = """ <div> <span style="font-size: 14px; font-weight: bold;">Name:</span> <span style="font-size: 10px; font-family: Monaco, monospace;">@name</span> </div> <div> <span style="font-size: 14px; font-weight: bold;">All:</span> <span style="font-size: 10px; font-family: Monaco, monospace;">@all</span> </div> <div> <span style="font-size: 14px; font-weight: bold;">Memory:</span> <span style="font-size: 10px; font-family: Monaco, monospace;">@memory</span> </div> <div> <span style="font-size: 14px; font-weight: bold;">Erred:</span> <span style="font-size: 10px; font-family: Monaco, monospace;">@erred</span> </div> """ hover.point_policy = 'follow_mouse' return source, fig
def histogram(df, vField, color=None, clustered=None): colors = self.colorPalette(len(df.index)) if color is None else color p = figure(y_axis_label='Frequency', x_axis_label=vField) for j,c in enumerate(list(df[clustered].unique())) if clustered else enumerate([None]): df2 = df[df[clustered] == c] if c else df hist, edges = np.histogram(list(df2[vField]), density=True, bins=binsize) p.quad(top=hist, bottom=0, left=edges[:-1], right=edges[1:], fill_color=colors[j], line_color="#cccccc", fill_alpha=0.8 if clustered else 1, legend=str(c) if clustered else None) p.y_range.start=0 p.legend.location = "top_left" hover = HoverTool() hover.tooltips = [('Frequency', '@top{0.00}'), ('Interval', '@left - @right')] p.add_tools(hover) return p
def show_points(pointsList, w=400, h=400): # Ensure it's a list going in if type(pointsList) is not list: pointsList = [pointsList] # Create the hover tool hover = HoverTool(tooltips=[("x", "@x"), ("y", "@y")]) hover.line_policy = "nearest" # Make the figure fig = figure(plot_width=w, plot_height=h) fig.add_tools(hover) colors = ['red', 'orange', 'yellow', 'green', 'blue', 'purple', 'indigo', 'violet'] # Plot all the points for i, points in enumerate(pointsList): # Grab the color color = colors[i % len(colors)] # Plot the vertices fig.x(x=points[:, 0], y=points[:, 1], size=6, line_width=2, line_color=color) # Plot the lines x, y = [np.hstack([points[:, z], points[0, z]]) for z in [0, 1]] fig.line(x=x, y=y, line_width=2, line_color=color) # Plot the point index next to the point fig.text(x=points[:, 0], y=points[:, 1], text=range(points.shape[0])) # Show the plot show(fig)
def task_stream_plot(height=400, width=800, follow_interval=5000, **kwargs): data = {'start': [], 'duration': [], 'key': [], 'name': [], 'color': [], 'worker': [], 'y': [], 'worker_thread': []} source = ColumnDataSource(data) if follow_interval: x_range = DataRange1d(follow='end', follow_interval=follow_interval, range_padding=0) else: x_range = None fig = figure(width=width, height=height, x_axis_type='datetime', tools=['xwheel_zoom', 'xpan', 'reset', 'resize', 'box_zoom'], responsive=True, x_range=x_range, **kwargs) fig.rect(x='start', width='duration', y='y', height=0.9, fill_color='color', line_color='gray', source=source) if x_range: fig.circle(x=[1, 2], y=[1, 2], alpha=0.0) fig.xaxis.axis_label = 'Time' fig.yaxis.axis_label = 'Worker Core' fig.min_border_right = 10 fig.ygrid.grid_line_alpha = 0.4 fig.xgrid.grid_line_alpha = 0.0 hover = HoverTool() fig.add_tools(hover) hover = fig.select(HoverTool) hover.tooltips = """ <div> <span style="font-size: 14px; font-weight: bold;">Key:</span> <span style="font-size: 10px; font-family: Monaco, monospace;">@name</span> </div> <div> <span style="font-size: 14px; font-weight: bold;">Duration:</span> <span style="font-size: 10px; font-family: Monaco, monospace;">@duration</span> </div> """ hover.point_policy = 'follow_mouse' return source, fig
def create_plot(ridb) : or_counties = { code: county for code, county in counties.items() if county["state"] == "or" } county_xs = [county["lons"] for county in or_counties.values()] county_ys = [county["lats"] for county in or_counties.values()] county_names = [county['name'] for county in or_counties.values()] lat = ridb['FacilityLatitude'] lon = ridb['FacilityLongitude'] source = ColumnDataSource(data=dict( x=county_xs, y=county_ys, name=ridb['FacilityName'] )) TOOLS="pan,wheel_zoom,box_zoom,reset,save" ridb = ridb.assign(Secret = "TRILLIUM") df_source = ColumnDataSource(data=ridb[['FacilityName','FacilityPhone','Secret']]) p = figure(title="Oregon Counties", tools=TOOLS) p.circle(lon, lat, size=8, color='navy', alpha=1, source=df_source, name='geo_points') p.patches('x', 'y', source=source, fill_color='green', fill_alpha=0.7, line_color="black", line_width=0.5) hover = HoverTool(names=['geo_points']) hover.tooltips = [(c, '@' + c) for c in ridb[['FacilityName','FacilityPhone','Secret']].columns] p.add_tools(hover) return p
def task_stream_plot(sizing_mode='scale_width', **kwargs): data = {'start': [], 'duration': [], 'key': [], 'name': [], 'color': [], 'worker': [], 'y': [], 'worker_thread': [], 'alpha': []} source = ColumnDataSource(data) x_range = DataRange1d(range_padding=0) fig = figure( x_axis_type='datetime', title="Task stream", tools='xwheel_zoom,xpan,reset,box_zoom', toolbar_location='above', sizing_mode=sizing_mode, x_range=x_range, **kwargs ) fig.rect( x='start', y='y', width='duration', height=0.8, fill_color='color', line_color='color', line_alpha=0.6, alpha='alpha', line_width=3, source=source ) fig.xaxis.axis_label = 'Time' fig.yaxis.axis_label = 'Worker Core' fig.ygrid.grid_line_alpha = 0.4 fig.xgrid.grid_line_color = None fig.min_border_right = 35 fig.yaxis[0].ticker.num_minor_ticks = 0 hover = HoverTool() fig.add_tools(hover) hover = fig.select(HoverTool) hover.tooltips = """ <div> <span style="font-size: 14px; font-weight: bold;">Key:</span> <span style="font-size: 10px; font-family: Monaco, monospace;">@name</span> </div> <div> <span style="font-size: 14px; font-weight: bold;">Duration:</span> <span style="font-size: 10px; font-family: Monaco, monospace;">@duration</span> </div> """ hover.point_policy = 'follow_mouse' return source, fig
def create_plot(ridb, list_to_display) : ridb_show = ridb[list_to_display] or_counties = { code: county for code, county in counties.items() if county["state"] == "or" } county_xs = [county["lons"] for county in or_counties.values()] county_ys = [county["lats"] for county in or_counties.values()] county_names = [county['name'] for county in or_counties.values()] lat = ridb['FacilityLatitude'] lon = ridb['FacilityLongitude'] source = ColumnDataSource(data=dict( x=county_xs, y=county_ys, )) TOOLS="pan,wheel_zoom,box_zoom,reset,save" df_source = ColumnDataSource(data=ridb_show) p = figure(title="Camping in Oregon!", tools=TOOLS) p.circle(lon, lat, size=8, color='navy', alpha=1, source=df_source, name='geo_points') p.patches('x', 'y', source=source, fill_color='green', fill_alpha=0.7, line_color="black", line_width=0.5) hover = HoverTool(names=['geo_points']) for c in ridb_show.columns: print(c) hover.tooltips = [(c, '@' + c) for c in ridb_show.columns] p.add_tools(hover) return p
def stackedBar(df, xlabel, vFields, color=None, clustered=False, title=None): ylabel = ','.join(v for v in vFields) ystart = 0 if clustered: factors=list(df.index) l = [ bk_value(a) for a in list(df.index) ] data = {'pd_stacked_col': list(df.columns.values)} for x in list(df.index): data[x] = list(df[x:x].values[0]) ystart = min(ystart, min(data[x])) else: factors=vFields l = [ bk_value(a) for a in vFields ] data = {'pd_stacked_col': list(df[xlabel].values)} for v in vFields: data[v] = list(df[v].values) ystart = min(ystart, min(data[v])) src = ColumnDataSource(data) colors = self.colorPalette(len(factors)) if color is None else color p = figure(x_range=data['pd_stacked_col'], y_axis_label=ylabel, x_axis_label=xlabel, title=title) p.vbar_stack(factors, x='pd_stacked_col', width=0.9, source=src, legend=l if self.showLegend() else None, color=colors) p.y_range.start = ystart p.axis.minor_tick_line_color = None p.outline_line_color = None p.x_range.range_padding = 0.1 p.xaxis.major_label_orientation = 1 p.xgrid.grid_line_color = None p.legend.location = "top_left" hover = HoverTool() hover.tooltips = [(d if d is not 'pd_stacked_col' else xlabel, '@' + d + '{0.00}') for d in data] p.add_tools(hover) return p
def make_analysis_number(ages, age_errors, runids): hover = HoverTool() hover.tooltips = [('RunID', '@runids'), ('Age', '@ages')] ys = range(len(ages)) source = ColumnDataSource( data={'ages': ages, 'ys': ys, 'runids': runids}) fig = figure(plot_height=200, tools=[hover, ]) fig.circle('ages', 'ys', source=source, size=5, color="navy", alpha=0.5) fig.xaxis.visible = None err_xs = [] err_ys = [] for x, xerr, y in zip(ages, age_errors, ys): err_xs.append((x - xerr, x + xerr)) err_ys.append((y, y)) fig.multi_line(err_xs, err_ys) fig.logo = None fig.toolbar_location = None return _make_bokeh_components(fig)
def groupedBar(df, xlabel, vFields, color=None, clustered=False, title=None): ylabel = ','.join(v for v in vFields) if clustered: factors=list(df.index) x = [ (b, a) for b in list(df.columns.values) for a in list(df.index) ] l = [ (a) for b in list(df.columns.values) for a in list(df.index) ] counts = sum(zip(df.at[a,b] for b in list(df.columns.values) for a in list(df.index)), ()) else: factors=vFields x = [ (b,a) for b in list(df[xlabel].values) for a in vFields ] l = [ (a) for b in list(df[xlabel].values) for a in vFields ] counts = [ df[df[xlabel] == b][a].values[0] for b in list(df[xlabel].values) for a in vFields ] src = ColumnDataSource(data=dict(x=x, counts=counts, l=l)) colors = self.colorPalette(len(factors)) if color is None else color p = figure(x_range=FactorRange(*x), y_axis_label=ylabel, x_axis_label=xlabel, title=title) p.vbar(x='x', top='counts', width=0.925, source=src, legend='l' if self.showLegend() else None, color=factor_cmap('x', palette=colors, factors=factors, start=1, end=2)) p.y_range.start = 0 if not counts else min(0, min(counts)) p.axis.minor_tick_line_color = None p.outline_line_color = None p.x_range.range_padding = 0.1 p.xaxis.major_label_orientation = 1 p.xaxis.major_label_text_font_size = "0px" p.xaxis.major_label_text_color = None p.xaxis.major_tick_line_color = None p.xgrid.grid_line_color = None p.legend.location = "top_left" hover = HoverTool() hover.tooltips = [(xlabel, '@x'), (ylabel, '@counts{0.00}')] p.add_tools(hover) return p
def plot_discrete_feature(df, discrete): # get the answers to our question answers = df[discrete].unique().tolist() wngroup = df["WN"].unique().tolist() wngroup = ['Agree', 'Disagree'] # get the relative percentage of each group stats_agree = [] stats_noagree = [] totals = [] wn_totals = [] nwn_totals = [] for a in answers: # how many people answered this way? total = len(df.loc[df[discrete] == a]) # how many people who answered this way also agree w/ WN? wn_total = len(df.loc[(df[discrete] == a) & (df["WN"] == 'Agree with White Nationalists')]) # what percentage of people who answered this way agree w/ WN? wn_per = wn_total / total # hold our stats for bokeh format stats_agree.append(wn_per) stats_noagree.append(1 - wn_per) totals.append(total) wn_totals.append(wn_total) nwn_totals.append(total - wn_total) # put data in Bokeh format source = ColumnDataSource( data={ 'answers': answers, 'Agree': stats_agree, 'Disagree': stats_noagree, 'totals': totals, 'wntotals': wn_totals, 'nwntotals': nwn_totals, }) # use data for tooltips hover = HoverTool(tooltips=[( "Group", "@totals people said \"@answers\"" ), ("Agree with White Nationalists", "@Agree{0.00%} (@wntotals people)" ), ("Do Not Agree", "@Disagree{0.00%} (@nwntotals people)")]) # create figure p = figure(x_range=answers, width=800, title=discrete, tools=['pan', 'box_zoom', 'reset', hover]) # plot stacked bars p.vbar_stack(wngroup, x='answers', width=0.9, color=["#c13633", "#346ac1"], source=source, legend=['Agree with White Nationalists', 'Do Not Agree'], name=wngroup) # format our axes p.xaxis.axis_label = discrete p.yaxis.axis_label = 'Percent of Respondents who Agree with White Nationalists' p.yaxis.formatter = NumeralTickFormatter(format="0.00%") # output our file #output_file("bar_stacked.html") #show(p) return (p)
def plotOutlierHistogram(dataframe, maxOutliers, func, statisticalOutlierThreshold, userLatencyThreshold, averageDuration, maxDuration): global pixelsForTitle global pixelsPerHeightUnit global plotWidth global timeUnitString cds = ColumnDataSource(dataframe) figureTitle = "Occurrences of " + func + " that took longer than " \ + statisticalOutlierThreshold + "." hover = HoverTool( tooltips=[("interval start", "@lowerbound{0,0}"), ("interval end", "@upperbound{0,0}")]) TOOLS = [hover, "tap, reset"] p = figure(title = figureTitle, plot_width = plotWidth, plot_height = min(500, (max(5, (maxOutliers + 1)) \ * pixelsPerHeightUnit + \ pixelsForTitle)), x_axis_label = "Execution timeline (" + timeUnitString + ")", y_axis_label = "Number of outliers", tools = TOOLS, toolbar_location="above") y_ticker_max = p.plot_height // pixelsPerHeightUnit y_ticker_step = max(1, (maxOutliers + 1) // y_ticker_max) y_upper_bound = (maxOutliers // y_ticker_step + 2) * y_ticker_step p.yaxis.ticker = FixedTicker( ticks=list(range(0, y_upper_bound, y_ticker_step))) p.ygrid.ticker = FixedTicker( ticks=list(range(0, y_upper_bound, y_ticker_step))) p.xaxis.formatter = NumeralTickFormatter(format="0,") p.y_range = Range1d(0, y_upper_bound) p.quad(left='lowerbound', right='upperbound', bottom='bottom', top='height', color=funcToColor[func], source=cds, nonselection_fill_color=funcToColor[func], nonselection_fill_alpha=1.0, line_color="lightgrey", selection_fill_color=funcToColor[func], selection_line_color="grey") p.x(x='markerX', y='markerY', size='markersize', color='navy', line_width=1, source=cds) # Add an annotation to the chart # y_max = dataframe['height'].max() text = "Average duration: " + '{0:,.0f}'.format(averageDuration) + " " + \ timeUnitString + \ ". Maximum duration: " + '{0:,.0f}'.format(maxDuration) + " " + \ timeUnitString + ". " if (userLatencyThreshold is not None): text = text + \ "An \'x\' shows intervals with operations exceeding " + \ "a user-defined threshold of " + \ userLatencyThreshold + "." mytext = Label(x=0, y=y_upper_bound - y_ticker_step, text=text, text_color="grey", text_font="helvetica", text_font_size="10pt", text_font_style="italic") p.add_layout(mytext) url = "@bucketfiles" taptool = p.select(type=TapTool) taptool.callback = OpenURL(url=url) return p
def add_plant_capacity( canvas, scenario, resources, disaggregation=None, min_capacity=1, size_factor=1, alpha=0.5, ): """Adds renewables capacity to a plot. :param bokeh.plotting.figure.Figure canvas: canvas to plot capacities onto. :param powersimdata.scenario.scenario.Scenario scenario: scenario instance. :param iterable resources: which types of resources to plot. :param tuple figsize: size of the bokeh figure (in pixels). :param tuple x_range: x range to zoom plot to (EPSG:3857). :param tuple y_range: y range to zoom plot to (EPSG:3857). :param str disaggregation: method used to disaggregate plants: if "new_vs_existing_plants": separates plants into added vs. existing. if None, no disaggregation. :param float/int min_capacity: minimum bus capacity (MW) for markers to be plotted. :param float/int size_factor: scale size of glyphs. :param float/int alpha: opacity of circles (between 0 and 1). :raises ValueError: if ``disaggregation`` is not 'new_vs_existing_plants' or None. :return: (*bokeh.plotting.figure.Figure*) -- map with color-coded upgrades. """ ct = scenario.get_ct() grid = scenario.get_grid() grid.plant["lat"] = grid.plant["lat"].round(3) grid.plant["lon"] = grid.plant["lon"].round(3) grid.plant = project_bus(grid.plant.query("type in @resources")) type_colors = grid.model_immutables.plants["type2color"] xy_capacity = {} if disaggregation is None: grouped_capacities = grid.plant.groupby(["x", "y", "type"]).sum().Pmax grouped_capacities = grouped_capacities.reset_index() xy_capacity["all"] = grouped_capacities.query("Pmax > 0") elif disaggregation == "new_vs_existing_plants": if "new_plant" in ct.keys(): num_new_plants = len(ct["new_plant"]) scaled_plants = grid.plant.iloc[:-num_new_plants] new_plants = grid.plant.iloc[-num_new_plants:] grouped_new_capacities = new_plants.groupby(["x", "y", "type"]).sum().Pmax grouped_new_capacities = grouped_new_capacities.reset_index() xy_capacity["new"] = grouped_new_capacities.query("Pmax > 0") else: scaled_plants = grid.plant new_plants = pd.DataFrame(columns=grid.plant.columns) xy_capacity["new"] = pd.DataFrame( columns=["x", "y", "type", "Pmax"]) grouped_capacities = scaled_plants.groupby(["x", "y", "type"]).sum().Pmax grouped_capacities = grouped_capacities.reset_index() xy_capacity["existing"] = grouped_capacities.query("Pmax > 0") else: raise ValueError(f"Unknown disaggregation method: {disaggregation}") # capacity circles renderers = [] for tranche, plants in xy_capacity.items(): for resource in sorted(resources): if disaggregation is None: legend_label = f"{resource} capacity" elif disaggregation == "new_vs_existing_plants": legend_label = f"{resource} capacity of {tranche} plants" if resource not in plants.type.unique(): print(f"no {resource} plants for grouping: {tranche}") continue matching_plants = plants.query("type == @resource") data = { "x": matching_plants["x"], "y": matching_plants["y"], "capacity": matching_plants["Pmax"], "radius": matching_plants["Pmax"]**0.5 * size_factor, } circle = canvas.circle( "x", "y", color=mcolors.to_hex(type_colors[resource]), alpha=0.8, size="radius", source=ColumnDataSource(data), legend_label=legend_label, ) renderers.append(circle) hover = HoverTool( tooltips=[ ("Capacity (MW)", "@capacity"), ], renderers=renderers, ) canvas.add_tools(hover) return canvas
x['val'] = x['value'] x['value'] = x['value'] / x['value'].sum() * 360.0 x['end'] = x['value'].expanding(1).sum() x['start'] = x['end'].shift(1) x['start'][0] = 0 r = [] p = figure(plot_height=450, plot_width=800, title="Relationships between victims and suspects 2013-2018", toolbar_location=None, tools="") for i in range(len(x)): r1 = p.wedge(x=0, y=1, radius=0.5,start_angle=x.iloc[i]['start'], end_angle=x.iloc[i]['end'],\ start_angle_units='deg', end_angle_units = 'deg', fill_color=Category20c[20][i],\ legend = x.iloc[i]['country']) relation = x.iloc[i]['country'] times_occured = x.iloc[i]['val'] hover = HoverTool(tooltips=[("Relation", "%s" % relation), ("Times Occured", "%s" % times_occured)], renderers=[r1]) p.add_tools(hover) p.xaxis.axis_label = None p.yaxis.axis_label = None p.yaxis.visible = False p.xaxis.visible = False p.xgrid.grid_line_color = None p.ygrid.grid_line_color = None show(p)
#Sets up ColumnDataSource for Bokeh bokeh_covid_data = ColumnDataSource(data=covid_data) #Plots data on the map m.circle(name='Cases', x='Longitude', y='Latitude', size='Case_Size', fill_color=(255, 125, 0, .7), line_color=(0, 0, 0, 1), source=bokeh_covid_data) m.circle(name='Deaths', x='Longitude', y='Latitude', size='Death_Size', fill_color=(255, 0, 0, .7), line_color=(0, 0, 0, 1), source=bokeh_covid_data) m.add_tools( HoverTool( tooltips=[('Cases', '@Confirmed{0,0}'), ('Deaths', '@Deaths{0,0}'), ('Country', '@Country'), ('Province', '@Province'), ('Country Population', '@Population{0,0}'), ('Last Updated', '@Last_Updated{%B %d, %Y}')], #Format 'Dates' column as a date formatters={'@Last_Updated': 'datetime'}, names=['Cases'])) show(m)
y='y', fill_alpha=0.8, source=source, color=dict(field='region', transform=color_mapper), legend='region') # Set the legend.location attribute of the plot to 'top_right' plot.legend.location = 'top_right' # Set the x-axis label plot.xaxis.axis_label = 'feritility' # Set the y-axis label plot.yaxis.axis_label = 'life expectancy' #add hoover tool to plot hover = HoverTool(tooltips=[('Country', '@Country')]) plot.add_tools(hover) # Make a slider object: slider slider = Slider(start=1970, end=2010, step=1, value=1970, title='Year') # Define the callback function: update_plot def update_plot(attr, old, new): # set the `yr` name to `slider.value` and `source.final_df = new_final_df` yr = slider.value # x=x_select.value # y=y_select.value #label axes of plot # plot.xaxis.axis_label = x # plot.yaxis.axis_label = y
# Add patch renderer to figure neighborhoods = p.patches('xs', 'ys', source=geosource, fill_color={ 'field': 'Address', 'transform': color_mapper }, line_color="gray", line_width=0.25, fill_alpha=1) # Create hover tool p.add_tools( HoverTool(renderers=[neighborhoods], tooltips=[('Neighborhood', '@pri_neigh'), ('No. of Housing Units', '@Address')])) #remove axes, axis labels, and grid lines p.xaxis.major_tick_line_color = None p.xaxis.minor_tick_line_color = None p.yaxis.major_tick_line_color = None p.yaxis.minor_tick_line_color = None p.xaxis.major_label_text_font_size = '0pt' p.yaxis.major_label_text_font_size = '0pt' p.xgrid.grid_line_color = None p.ygrid.grid_line_color = None # Specify layout p.add_layout(color_bar, 'below')
# Modify legend location p.legend.location = "top_right" p.legend.orientation = "vertical" # Insert a circle on top of the Central Railway Station (coords in EurefFIN-TM35FIN) station_x = 385752.214 station_y = 6672143.803 circle = p.circle(x=[station_x], y=[station_y], name="point", size=6, color="red") # Add two separate hover tools for the data phover = HoverTool(renderers=[circle]) phover.tooltips = [("Destination", "Railway Station")] ghover = HoverTool(renderers=[grid]) ghover.tooltips = [ ("YKR-ID", "@from_id"), ] #,("PT time", "@pt_r_tt") p.add_tools(ghover) p.add_tools(phover) # Output filepath to HTML output_file = r"C:\Users\oyeda\Desktop\AUTOGIS\FINAL_ASSIGNMENT\visualise\YKR_grid_values.html" # Save the map save(p, output_file)
x_axis_type='datetime', title='steps per {} second'.format( global_vals.data_produce_duration)) fig_steps.xaxis.axis_label = 'time' # axis label fig_steps.yaxis.axis_label = 'steps' fig_steps.background_fill_color = 'beige' # the color of background is 'beige' fig_steps.background_fill_alpha = 0.5 source_steps = ColumnDataSource(data=dict( x=[pd.to_datetime(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))], y=[0])) # data source # line figure, the data of x axis come from key x of dict 'source_steps' fig_steps.line(x='x', y='y', alpha=0.5, legend='steps', source=source_steps) steps_glyph = glyphs.Circle(x='x', y='y', size=3, fill_color='white') steps_add = fig_steps.add_glyph(source_or_glyph=source_steps, glyph=steps_glyph) hover = HoverTool(tooltips=tooltips_steps, renderers=[steps_add]) fig_steps.legend.location = 'bottom_left' fig_steps.add_tools(hover) # heart rate figure fig_heart = figure(plot_width=1000, plot_height=300, x_axis_type='datetime', title='heart rate per {} second'.format( global_vals.data_produce_duration)) fig_heart.xaxis.axis_label = 'time' # axis label fig_heart.yaxis.axis_label = 'heart rate' fig_heart.background_fill_color = 'beige' # the color of background is 'beige' fig_heart.background_fill_alpha = 0.5 source_heart = ColumnDataSource(data=dict(time=[ pd.to_datetime(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))
<span style="font-size: 12px; font-weight: bold; color: #303030;">RA: </span> <span style="font-size: 13px; color: #515151;">@ra</span> </div> <div> <span style="font-size: 12px; font-weight: bold; color: #303030;">DEC: </span> <span style="font-size: 13px; color: #515151;">@dec</span> </div> </div> """ url = "http://legacysurvey.org/viewer?ra=@ra&dec=@dec&zoom=16&layer=decals-dr5" c1, c2 = int(selected_spectrograph) * 500, (int(selected_spectrograph) + 1) * 500 qlf_fiberid = np.arange(0, 5000)[c1:c2] hover = HoverTool(tooltips=skc_tooltips) # sky continuum per sky fiber averaged over two continuum regions, # 'n' is number of sky fibers skycont = skycont sky = skycont['SKYCONT_FIBER'] skyfibers = skycont['SKYFIBERID'] ra = [skycont['RA'][c1:c2][i] for i in skyfibers] dec = [skycont['DEC'][c1:c2][i] for i in skyfibers] ra_not, dec_not = [], [] for i in range(500): if i not in skyfibers: ra_not.append(skycont['RA'][c1:c2][i]) dec_not.append(skycont['DEC'][c1:c2][i])
x='fertility', y='life', size='population', source=source, fill_color={ 'field': 'region', 'transform': color_mapper }, fill_alpha=0.8, line_color='#7c7e71', line_width=0.5, line_alpha=0.5, legend=field('region'), ) plot.add_tools( HoverTool(tooltips="@index", show_arrow=False, point_policy='follow_mouse')) def animate_update(): year = slider.value + 1 if year > years[-1]: year = years[0] slider.value = year def slider_update(attrname, old, new): year = slider.value label.text = str(year) source.data = data[year]
def plot_merged_benchmark(savefile, benchmarks, title, xaxis_type, yaxis_type, save_prefix=""): # Sort the benchmarks by device name bmarks_sorted = sorted(benchmarks, key=lambda k: k['extra_data']['AF_DEVICE']) benchmarks = bmarks_sorted # configure the colors colors = unique_colors() assigned_colors = dict() # configure the hover box hover = HoverTool( tooltips = [ ("Device", "@device"), ("Benchmark", "@benchmark"), ("Backend", "@platform"), ("OS", "@os"), ("(x,y)", "(@x,@y)") ]) # configure the plot title and axis labels, use CDN for the data source #bplt.output_file(save_prefix + savefile + ".html", title=title, mode='cdn') bplt.output_file(save_prefix + savefile + ".html", title=title) plot = bplt.figure(title=title, tools=[hover,'save,box_zoom,resize,reset']) xlabel = "" ylabel = "" legend_location = "top_right" # plot images/second vs. data size scatter_renderers = list() for benchmark in benchmarks: bmark_name = benchmark['benchmark_name'] # Look up the color device = benchmark['extra_data']['AF_DEVICE'] platform = benchmark['extra_data']['AF_PLATFORM'] operating_system = benchmark['extra_data']['AF_OS'] # key = device key = bmark_name + device + platform if key in assigned_colors: color = assigned_colors[key] else: color = colors.next() assigned_colors[key] = color # extract benchmarks x,xlabel,legend_location = format_data(benchmark, xaxis_type) y,ylabel,legend_location = format_data(benchmark, yaxis_type) # get the device name, override if necessary if 'AF_LABEL' in benchmark['extra_data'].keys(): device = benchmark['extra_data']['AF_LABEL'] source = bplt.ColumnDataSource( data = dict(x=x,y=y, device=[device]*len(x), benchmark=[bmark_name]*len(x), platform=[platform]*len(x), os=[operating_system]*len(x), )) # Generate the legend, automatically add the platform if needed # legend = device legend = device + " (" + platform + ") " + bmark_name # generate the plot plot.line(x,y, legend=legend, color=color, line_width=2) sr = plot.scatter('x', 'y', source=source, legend=legend, color=color, fill_color="white", size=8) scatter_renderers.append(sr) hover = plot.select(HoverTool) hover.renderers = scatter_renderers plot.xaxis.axis_label = xlabel plot.yaxis.axis_label = ylabel plot.legend.location = legend_location # save the plot bplt.save(plot)
def process_file(self): potiron_path = potiron.potiron_path lentwo = False ck = self.red.sismember('CK', 'YES') for v in self.fieldvalues: # if any field value is in format 'value-protocol' (or 'value-all'), it means we want to display each protocol separatly if len(v.split('-')) >= 2: lentwo = True # Using the format 'value-protocol' is not possible if combined keys are not deployed in the current redis database if lentwo and not ck: sys.stderr.write( 'Combined keys are not used in this redis dataset') sys.exit(1) if not self.outputdir.endswith('/'): self.outputdir = "{}/".format(self.outputdir) if not os.path.exists(self.outputdir): os.makedirs(self.outputdir) # Definition of the protocols currently present in our dataset protocols = self.red.smembers('PROTOCOLS') # Define the strings used for legends, titles, etc. concerning fields field_string, field_in_file_name = field2string( self.field, potiron_path) field_data = create_dict(self.field, potiron_path) all_proto = False for fv in self.fieldvalues: v = fv.split('-') # If we want to display the values for all the procotols, we display the sum of all of them as well if len(v) >= 2 and (v[1] == '*' or v[1] == 'all'): all_proto = True self.fieldvalues.append(v[0]) # Creation of the figure and the tools used on it namefile = self.output_name(field_in_file_name, lentwo, all_proto) # As displaying values for all the protocols may generate a lot of lines in the plot, # We help users showing them the protocol when they have there cursor in the line if all_proto: hover = HoverTool(tooltips=[('count', '@y'), ('protocol', '@prot')]) else: hover = HoverTool(tooltips=[('count', '@y')]) taptool = TapTool() TOOLS = [ hover, PanTool(), BoxZoomTool(), WheelZoomTool(), taptool, SaveTool(), ResetTool() ] p = figure(width=self.plot_width, height=self.plot_height, tools=TOOLS) # Definition of some variables which will be used and modified with the iterations at_least_one = False days = calendar.monthrange(int(self.date[0:4]), int(self.date[4:6]))[1] maxVal = 0 minVal = sys.maxsize maxDay = 0 vlength = len(self.fieldvalues) actual_values = [] nbLine = 0 # day_string = "@x" for v in range(vlength): # For each selected field or occurrence value = self.fieldvalues[v].split('-') actual_field = value[0] if len(value) >= 2: # If we specified a or all protocol(s) protocol = value[1] if protocol == "*" or protocol == "all": for prot in protocols: score = [] dayValue = [] proto = prot.decode() exists = False keys = self.red.keys("{}:{}:{}*:{}".format( self.source, proto, self.date, self.field)) for k in sorted(keys): redisKey = k.decode() day = redisKey.split(':')[2][-2:] countValue = self.red.zscore( redisKey, actual_field) if countValue is not None: exists = True score.append(countValue) else: score.append(0) dayValue.append(day) if exists: at_least_one = True # We define the color of the line, draw it color = palette[nbLine % 10] protos = [] for x in dayValue: protos.append("{}_{}".format(x, proto)) prots = [proto] * len(score) sourceplot = ColumnDataSource( data=dict(x=dayValue, y=score, protocol=protos, prot=prots)) leg = def_legend(actual_field, proto, self.field, field_string, field_data) p.line(x='x', y='y', legend=leg, line_color=color, line_width=2, source=sourceplot) c = p.scatter(x='x', y='y', legend=leg, size=10, color=color, alpha=0.1, source=sourceplot) taptool.renderers.append( c) # In order to have the interaction on click nbLine += 1 maxScore = max( score) # Update the min and max scores scaling if maxVal < maxScore: # in order to define the lower and upper maxVal = maxScore # limits for the graph minScore = min(score) if minVal > minScore: minVal = minScore # Definition of the last day for which there is data to display if int(dayValue[-1]) > maxDay: maxDay = int(dayValue[-1]) actual_value = "{}-{}".format( actual_field, protocol) actual_values.append(actual_value) else: score = [] dayValue = [] exists = False keys = self.red.keys("{}:{}:{}*:{}".format( self.source, protocol, self.date, self.field)) for k in sorted(keys): redisKey = k.decode() day = redisKey.split(':')[2][-2:] countValue = self.red.zscore(redisKey, actual_field) if countValue is not None: exists = True score.append(countValue) else: score.append(0) dayValue.append(day) if exists: # If at least one occurrence for the current value of field has been found at_least_one = True # We define the color of the line, draw it color = palette[nbLine % 10] leg = def_legend(actual_field, protocol, self.field, field_string, field_data) p.line(x=dayValue, y=score, legend=leg, line_color=color, line_width=2) c = p.scatter(x=dayValue, y=score, legend=leg, size=10, color=color, alpha=0.1) taptool.renderers.append( c) # In order to have the interaction on click nbLine += 1 maxScore = max( score) # Update the min and max scores scaling if maxVal < maxScore: # in order to define the lower and upper maxVal = maxScore # limits for the graph minScore = min(score) if minVal > minScore: minVal = minScore # Definition of the last day for which there is data to display if int(dayValue[-1]) > maxDay: maxDay = int(dayValue[-1]) actual_value = "{}-{}".format(actual_field, protocol) actual_values.append(actual_value) else: # on the other case, we don't split informations for each protocol score = [] dayValue = [] exists = False # If combined keys are used, we must by the way take data from all the keys (i.e for each protocol) if ck: for d in range( 1, days + 1): # For each day with data stored in redis exists_day = False day = format(d, '02d') countValue = 0 keys = self.red.keys("{}:*:{}{}:{}".format( self.source, self.date, day, self.field)) for k in keys: redisKey = k.decode() tmpscore = self.red.zscore(redisKey, actual_field) countValue += tmpscore if tmpscore is not None else 0 exists_day = True if exists_day: if countValue > 0: exists = True score.append(countValue) dayValue.append(day) else: # When combined keys are not used, we only need to read the scores for each day keys = self.red.keys("{}:{}*:{}".format( self.source, self.date, self.field)) for k in sorted(keys): redisKey = k.decode() day = redisKey.split(':')[2][-2:] countValue = self.red.zscore(redisKey, actual_field) if countValue is not None: exists = True score.append(countValue) else: score.append(0) dayValue.append(day) if exists: # If at least one occurrence for the current value of field has been found at_least_one = True # We define the color of the line, draw it color = palette[nbLine % 10] leg = def_legend(actual_field, None, self.field, field_string, field_data) if all_proto: protos = [] for x in dayValue: protos.append(x) prots = ['all protocols'] * len(score) sourceplot = ColumnDataSource(data=dict( x=dayValue, y=score, protocol=protos, prot=prots)) p.line(x='x', y='y', legend=leg, line_color=color, line_width=2, source=sourceplot) c = p.scatter(x='x', y='y', legend=leg, size=10, color=color, alpha=0.1, source=sourceplot) else: p.line(x=dayValue, y=score, legend=leg, line_color=color, line_width=2) c = p.scatter(x=dayValue, y=score, legend=leg, size=10, color=color, alpha=0.1) taptool.renderers.append( c) # In order to have the interaction on click nbLine += 1 maxScore = max( score) # Update the min and max scores scaling if maxVal < maxScore: # in order to define the lower and upper maxVal = maxScore # limits for the graph minScore = min(score) if minVal > minScore: minVal = minScore # Definition of the last day for which there is data to display if int(dayValue[-1]) > maxDay: maxDay = int(dayValue[-1]) actual_value = "{}".format(actual_field) actual_values.append(actual_value) if at_least_one: # If at least one value has been found in redis with our selection if lentwo: # Defines the name of the files to call with a click on a point in the plot taptool.callback = OpenURL( url="{}_{}_with-protocols_{}-{}[email protected]".format( self.source, field_in_file_name, self.date[0:4], self.date[4:6])) else: taptool.callback = OpenURL(url="{}_{}_{}-{}[email protected]".format( self.source, field_in_file_name, self.date[0:4], self.date[4:6])) output_file("{}.html".format(namefile), title=namefile.split("/")[-1]) # Definition of some parameters of the graph fieldvalues_string = plot_annotation(self.field, potiron_path, actual_values, field_string, field_data) p.title.text = "Number of {} {}seen each day in {} {}".format( field_string, fieldvalues_string, potiron.year[self.date[4:6]], self.date[0:4]) p.yaxis[0].formatter = BasicTickFormatter(use_scientific=False) p.xaxis.axis_label = "Days" p.yaxis.axis_label = "Count" p.legend.location = "top_left" p.legend.click_policy = "hide" # Definition of some parameters for the logo with Image.open(self.logofile) as im: im_width, im_height = im.size xdr = maxDay + 1 upper_space = 10 if nbLine > 2: upper_space *= (nbLine / 2) ydrmax = maxVal + maxVal * upper_space / 100 ydrmin = minVal - maxVal * 5 / 100 p.x_range = Range1d(0, xdr) p.y_range = Range1d(ydrmin, ydrmax) height = (ydrmax - ydrmin) / self.logo_y_scale width = xdr / ((self.logo_y_scale * im_height * self.plot_width) / (im_width * self.plot_height)) p.image_url(url=[self.logofile], x=[xdr], y=[ydrmax - ydrmax * 2 / 100], w=[width], h=[height], anchor="top_right") # Process the graph save(p) if self.links: export_csv = export_csv_all_days_per_month.Export_Csv( self.red, self.source, self.date, self.field, 10, ['-1'], self.outputdir, True, True, self.logofile, ck, lentwo) ck = True if red.sismember('CK', 'YES') else False export_csv.process_all_files() else: print("There is no such value for a {} you specified: {}".format( field_string, self.fieldvalues))
# format our points based on group colormap = {'Do Not Agree': 'blue', 'Agree with White Nationalists': 'red'} colors = [colormap[x] for x in df['WN']] sizemap = {'Do Not Agree': 5, 'Agree with White Nationalists': 10} sizes = [sizemap[x] for x in df['WN']] # format our data from the dataframe source = ColumnDataSource(data=dict( x=df["Age"], y=df["Income"], desc=df["WN"], colors=colors, sizes=sizes)) # format our hover tooltips hover = HoverTool(tooltips=[ ("Age", "@x"), ("Income", "@y{$0,0.00}"), ("Opinion", "@desc"), ]) # create our figure p1 = figure(title="Income versus Age", width=800, tools=['pan', 'box_zoom', 'reset', hover]) # format our axes p1.xaxis.axis_label = 'Age' p1.yaxis.axis_label = 'Annual Income' p1.yaxis.formatter = NumeralTickFormatter(format="$0,0.00") # plot our scatter p1.circle('x',
from motion_detector import df from bokeh.plotting import figure, output_file, show from bokeh.models import HoverTool, ColumnDataSource df["Start_string"] = df["Start"].dt.strftime("%Y-%m-%d, %H:%M:%S") df["End_string"] = df["End"].dt.strftime("%Y-%m-%d, %H:%M:%S") cds = ColumnDataSource(df) plot = figure(x_axis_type="datetime", width=600, height=400) plot.title.text = "Motion graph" hover = HoverTool(tooltips=[("Start", "@Start_string"), ("End", "@End_string")]) plot.add_tools(hover) q = plot.quad(left="Start", right="End", top=1, bottom=0, color="green", source=cds) output_file("Motion_graph.html") show(plot)
# Define node positions data structure (dict) for plotting node_positions = {node[0]: (node[1]['x'], -node[1]['y']) for node in g.nodes(data=True)} import networkx as nx from bokeh.io import show, output_file from bokeh.models import Plot, Range1d, MultiLine, Circle, HoverTool, TapTool, BoxSelectTool,WheelZoomTool, PanTool, ResetTool from bokeh.models.graphs import from_networkx, NodesAndLinkedEdges, EdgesAndLinkedNodes from bokeh.palettes import Reds, RdGy plot = Plot(plot_width=1200, plot_height=1200, x_range=Range1d(-1.1,1.1), y_range=Range1d(-1.1,1.1)) plot.title.text = "Graph Interaction Demonstration" plot.add_tools(HoverTool(tooltips=None), TapTool(), BoxSelectTool(), WheelZoomTool(), PanTool(),ResetTool()) graph_renderer = from_networkx(g, nx.circular_layout, scale=1, center=(0,0)) graph_renderer.node_renderer.glyph = Circle(size=10, fill_color='#cb181d') graph_renderer.node_renderer.selection_glyph = Circle(size=10, fill_color=Spectral4[2]) graph_renderer.node_renderer.hover_glyph = Circle(size=10, fill_color=Spectral4[1]) graph_renderer.edge_renderer.glyph = MultiLine(line_color='#000000', line_alpha=0.8, line_width=2) graph_renderer.edge_renderer.selection_glyph = MultiLine(line_color=Spectral4[2], line_width=2) graph_renderer.edge_renderer.hover_glyph = MultiLine(line_color=Spectral4[1], line_width=2) graph_renderer.selection_policy = NodesAndLinkedEdges() graph_renderer.inspection_policy = EdgesAndLinkedNodes() plot.renderers.append(graph_renderer)
"""bokeh columndatasource""" from bokeh.models import ColumnDataSource source=ColumnDataSource(data={'x':[1,2,3,4,5],'y':[1,2,3,4,5]}) #or source=ColumnDataSource(df) #different code given from bokeh.plotting import ColumnDataSource source = ColumnDataSource(df) p.circle(source=source, color='color', size=8, x='Year', y ='Time') """interactivity using tools""" plot = figure(tools='box_select,lasso_select') plot.circle(x, y, selection_color='red', nonselection_fill_alpha=0.2, nonselection_fill_color='gray') #x and y coordinates of the circle from bokeh.models import HoverTool hover = HoverTool(tooltips=None, mode='hline') plot = figure(tools=[hover, 'crosshair']) plot.circle(x,y,size=15,hover_color='red') #hover_... #add tools p.add_tools(hover) """Color mapping""" from bokeh.models import CategoricalColorMapper mapper = CategoricalColorMapper(factors=['setosa','virginica','versicolor'],palette=['red','green','blue']) #Note: there are other color palettes that can be used as well.. plot.circle('columna','columnb',size=10,source=source,color={'field':'species','transform':mapper}) """layouts""" from bokeh.layouts import row, column #column layout = row(column(p1,p2),p3)
def update_chart(timeseries_data): #chart_palette = sns.color_palette("Paired", len(timeseries_data['xs'])).as_hex() ts_size = len(timeseries_data['xs']) chart_palette = sns.hls_palette(ts_size, l=.5, s=.6).as_hex() chart_modes = ['count', 'cummulative', 'histogram'] chart_mode = chart_modes[chart_rbg_radios.active] clear_chart() #if time-based, convert x axis to datetime if chart_mode in ['count', 'cummulative']: datetime_xs = [] for raw_xs in timeseries_data['xs']: datetime_xs.append([datetime.strptime(x, '%Y-%m-%d') for x in raw_xs]) timeseries_data['xs_datetime'] = datetime_xs timeseries_data['line_color'] = chart_palette line_opts = { 'xs': 'xs_datetime', 'ys': 'ys', 'line_width': 1.5, 'line_alpha': .8, 'line_color': 'line_color', 'source': ColumnDataSource(timeseries_data), } global chart_lines chart_lines = chart.multi_line(**line_opts) chart_tooltips = [ ("value", "$y{int}"), ("Date", "$x{%F}"), ("name", "@keys"), ] chart_labels = [s['title'] for s in conf.selectors] chart_by = chart_labels[select_chart_radios.active] chart_modes = ["Yearly", "Monthly", "Weekly", "Daily"] chart_mode = chart_modes[chart_res_radios.active] agg_modes = ['Count', 'Due', 'Paid', 'Penalties', 'Initial Amnt.'] agg_mode = agg_modes[select_type_radios.active] if agg_mode == 'Count': of_or_from = 'Of' else: of_or_from = 'From' cumm_modes = ['', 'Cummulative '] cumm_mode = cumm_modes[chart_rbg_radios.active] title_vals = [cumm_mode, chart_mode, agg_mode, of_or_from, chart_by] chart_title.text = '{}{} {} {} Tickets By {}'.format(*title_vals) for tool in chart.tools: if tool.name == 'chart_hovertool': del tool hovertool_opts = dict(tooltips=chart_tooltips, formatters={'$x': 'datetime'}, line_policy='nearest', mode='mouse', name='chart_hovertool') chart_hovertool = HoverTool(**hovertool_opts) chart.add_tools(chart_hovertool) chart.xaxis.visible = True
source = ColumnDataSource(data=dict(lat=list(df.reclat), lon=list(df.reclong), fill=list(df.fill), mass=list(df.mass), year=list(df.year), recclass=list(df.recclass), name=list(df.name))) # Adding Map's Glyphs: circle = Circle(x="lon", y="lat", size=5, fill_color="fill", line_alpha=0) plot.add_glyph(source, circle) # Initializing and adding Map Tools to the plot object: pan = PanTool() wheel_zoom = WheelZoomTool() box_select = BoxSelectTool() box_zoom = BoxZoomTool() reset = ResetTool() # This allows you to specify what goes in the Map's Tooltip hover = HoverTool(tooltips=[("Class", "@recclass"), ( "Name", "@name"), ("Year", "@year"), ("(lat, long)", "(@lat, @lon)"), ("Mass", "@mass")]) plot.add_tools(pan, wheel_zoom, box_select, box_zoom, reset, hover) # Run the line below if you want to print a HTML file to your working directory: #output_file("gmap_plot.html") # Print the map: show(plot)
cr1 = plot.add_glyph( ds1, Circle(x="x", y="y", radius="s", fill_color="c", line_color="c")) ds2 = ColumnDataSource(data=fds(-5, 5, 0.5, "d")) cr2 = plot.add_glyph( ds2, Circle(x="x", y="y", radius="s", fill_color="c", line_color="c")) ln2 = plot.add_glyph(ds2, Line(x="x", y="y", line_width=3, line_color="red")) ds3 = ColumnDataSource(data=fds(5, 5, 0.0, "e")) cr3 = plot.add_glyph( ds3, Circle(x="x", y="y", radius="s", fill_color="c", line_color="c")) tooltips = "<b>@name</b> = (@x{0.00}, @y{0.00})" hover = HoverTool(tooltips=tooltips, renderers=[cr1, cr2, ln2, cr3], point_policy="follow_mouse") plot.add_tools(hover) crosshair = CrosshairTool() plot.add_tools(crosshair) tap = TapTool(renderers=[cr1, cr2, cr3], callback=CustomJS(code="console.log('TAP')")) plot.add_tools(tap) wheelzoom = WheelZoomTool() plot.add_tools(wheelzoom) legends = lambda: [ LegendItem(label="CR1", renderers=[cr1]),
def make_document(doc): """ Internal function to create the document for visualisation. Hidden from users who need only call outer function with a fitted model. """ # Use the model to predict an average representationof data, using the mean value of each predictor centred_predict = model.master_data_frame.mean().to_dict() _, final_im, shape, texture_im = model.predict(**centred_predict) # Call the image tidy function to change RGB texture to RGBA final_im = _image_tidy(final_im) texture = _image_tidy(texture_im) # Set up the plot for the shape coordinates ############################### # Define a column data source shape_source = ColumnDataSource({'x': shape[:, 0], 'y': shape[:, 1]}) # Instantiate plot object for shape coordinates shape_plot = figure(title='Predicted Shape', y_range=(900, 0), x_range=(0, 900)) shape_plot.cross('x', 'y', size=10, source=shape_source) # Define hover tool and add to plot hover = HoverTool(tooltips=[('x', '@x'), ('y', '@y')]) shape_plot.add_tools(hover) ########################################################################### # Set up a column data source for the actual warped face ################## # Define a column data source warp_source = ColumnDataSource({'image': [final_im]}) # Instantiate plot object for warped image - add a constant extra few pixels to make sure image is not squashed to window warp_image_plot = figure(title='Predicted Face', y_range=(0, model.image_dims[0] + 150), x_range=(0, model.image_dims[1] + 150)) warp_image_plot.image_rgba(image='image', x=0, y=0, dw=model.image_dims[1], dh=model.image_dims[0], source=warp_source) # Set up a column data source for the texture-only face ################### # Define a column data source texture_source = ColumnDataSource({'image': [texture]}) # Instantiate plot object for shape-free face image_plot = figure(title='Predicted Texture', y_range=(0, model.image_dims[0] + 150), x_range=(0, model.image_dims[1] + 150)) image_plot.image_rgba(image='image', x=0, y=0, dw=model.image_dims[1], dh=model.image_dims[0], source=texture_source) ########################################################################### # Define the internal callback function to update objects interactively def callback(attr, old, new): """ Bokeh callback for updating glyphs """ # Iterate over the traits, get their title and their value and store in the dictionary predictor_dict = {} for slide in sliders: predictor_dict[slide.title] = slide.value # Use this dictionary to feed to the model's predict method, generating new ouput to show _, final_im, shape, texture = model.predict(**predictor_dict) # Fix the images for show final_im = _image_tidy(final_im) texture = _image_tidy(texture) # Update data sources with the new information shape_source.data = {'x': shape[:, 0], 'y': shape[:, 1]} warp_source.data = {'image': [final_im]} texture_source.data = {'image': [texture]} ########################################################################### # Set up sliders to alter properties sliders = [] for trait in model.trait_list: # Get the middle and far end points by applying mean, min, and max, and rounding to zero avg, mini, maxi = model.master_data_frame[trait].apply( ['mean', 'min', 'max']).round() slider = Slider(title=trait, start=mini, end=maxi, step=1, value=avg) slider.on_change('value', callback) sliders.append(slider) ########################################################################### # Set layout according to specification of user, extract from dictionary layout_dict = { 'combined': warp_image_plot, 'texture': image_plot, 'shape': shape_plot } layout = row([widgetbox(sliders), layout_dict[display]]) # Update and add to curdoc doc.add_root(layout)
def bokeh_plot(): #1: Read shape file as geoDataFrame #Pune admin level data - https://github.com/datameet fp = 'E:\Coursera Material\Python For Everyone\Birds\Owl analysis\Datameet\Pune_wards-master\GeoData\pune-admin-wards.geojson' #reading the file stored in variable fp map_df = gpd.read_file(fp) map_df.plot(color='skyblue', linewidth=0.1, edgecolor='black') #2: Read ward to sector data rain_data = 'E:\Coursera Material\Python For Everyone\Pune Rainfall\Rainfall Volunteers_2020 daily.xlsx' rain_data_spreadsheet = pd.read_excel(rain_data, sheet_name=None) admin_to_sector = rain_data_spreadsheet['Sector_admin'] map_df_admin = map_df.merge(admin_to_sector, on='name') map_df_sector = map_df_admin[['geometry','Sector']] map_df_sector = map_df_sector.dissolve(by='Sector') map_df_sector.reset_index(inplace=True) #Reference link - https://www.earthdatascience.org/workshops/gis-open-source-python/dissolve-polygons-in-python-geopandas-shapely/ #saving file as geojson format map_df_sector.to_file("pune_sectors.geojson", driver='GeoJSON') #3.1: Calculate centroid of a multipolygon map_df_sector["centroid"] = map_df_sector["geometry"].centroid for index, row in map_df_sector.iterrows(): centroid_coords = row.geometry.centroid.coords.xy map_df_sector.loc[index, 'cen_x'] = centroid_coords[0][0] map_df_sector.loc[index, 'cen_y'] = centroid_coords[1][0] map_df_sector.drop(['centroid'], axis=1, inplace=True) #Read data to json. merged_json = json.loads(map_df_sector.to_json()) #Convert to String like object. json_data = json.dumps(merged_json) #json check - merged_json['features'][17]['properties'] #Plot Bokeh graph from bokeh.io import output_notebook, show, output_file, curdoc from bokeh.plotting import figure from bokeh.models import GeoJSONDataSource, ColumnDataSource, LinearColorMapper, ColorBar, Div, Panel from bokeh.models import (Slider,HoverTool,Select, DatetimeTickFormatter, LabelSet) from bokeh.palettes import brewer from bokeh.layouts import widgetbox, row, column from bokeh.transform import factor_cmap from bokeh.tile_providers import get_provider, Vendors from bokeh.palettes import Spectral6 #Input GeoJSON source that contains features for plotting. geosource = GeoJSONDataSource(geojson = json_data) #Create Pune City map object. p = figure(title = 'Rainfall collection points in Pune City', plot_height = 600 , plot_width = 600, toolbar_location = None, match_aspect=True) p.xgrid.grid_line_color = None p.ygrid.grid_line_color = None tile_provider = get_provider(Vendors.OSM) p.add_tile(tile_provider) #to investigate why tile is not getting rendered #Add patch renderer to figure. p.patches('xs','ys', source = geosource, line_color = 'black', line_width = 0.5, fill_alpha = 0) #p.add_tools(HoverTool(renderers=[r1], tooltips=[ ('admin name','@name')])) labels = LabelSet(x='cen_x', y='cen_y', source = geosource, text='Sector', level='glyph', x_offset=0, y_offset=0, render_mode='css') p.add_layout(labels) #Plot rain data collection pointson Pune City map object rain_data = rain_data_spreadsheet['June_per_day'] rain_data['Date'] = rain_data['Date'].dt.date rain_data_scope = rain_data[(rain_data['Region']=='PMC') & (rain_data['Obs Status']=='complete')] #Allocate rain_data points to 'Sector' rain_data_GeoDataFrame = gpd.GeoDataFrame(rain_data_scope, geometry=gpd.points_from_xy(rain_data_scope.Long, rain_data_scope.Lat)) rain_data_GeoDataFrame.crs = {'init': 'epsg:4326'} rain_data_alloc_to_Sector = gpd.sjoin(rain_data_GeoDataFrame, map_df_sector) rain_data_total = rain_data_alloc_to_Sector.groupby(['Region','Location','Lat','Long','Obs Status','Sector']).sum()[['Rainfall']].reset_index() rain_data_total = rain_data_total.sort_values(by=['Sector','Location']) rain_data_points = ColumnDataSource(data = {'x': rain_data_total['Long'], 'y': rain_data_total['Lat'], 'Obs Status': rain_data_total['Obs Status'], 'Location':rain_data_total['Location'], 'Rainfall':rain_data_total['Rainfall']}) STATUS = ['complete','na'] STATUS_colour = ['green','orange'] r2= p.circle('x', 'y', source = rain_data_points, alpha = 0.4, legend_field='Obs Status', color = factor_cmap('Obs Status', STATUS_colour, STATUS),#'navy', size = 10) p.add_tools(HoverTool(renderers=[r2], tooltips=[ ('Location','@Location')])) #Create Bar chart: ''' Method 1: b = figure(x_range=rain_data_total['Location'], y_range=(0,200),plot_height=700, title="Sector Wise Rainfall", toolbar_location=None, tools="") b.vbar(x=rain_data_total['Location'], top=rain_data_total['Rainfall'], width=0.5) ''' #Method 2: bar_chart_data = ColumnDataSource(data = {'x': rain_data_total['Location'], 'y': rain_data_total['Rainfall'], 'Sector': rain_data_total['Sector']}) sector = rain_data_total['Sector'].unique() b = figure(x_range=rain_data_total['Location'], y_range=(0,100),plot_height=400, title="Sector Wise Rainfall", toolbar_location=None) b.vbar(x='x', top='y', width=0.5, source=bar_chart_data,legend_field='Sector', color=factor_cmap('Sector',Spectral6,sector)) b.xgrid.grid_line_color = None b.xaxis.major_label_orientation = pi/2.5 b.xaxis.major_label_text_align = 'left' b.y_range.start = 0 m=row(p) #show(m) #Plot matplotlib line graph rain_analysis_mat(rain_data_alloc_to_Sector) import folium from folium import Choropleth, Circle, Marker from folium.plugins import HeatMap, MarkerCluster m_2 = folium.Map(location=[18.5, 73.9], tiles='StamenTerrain', zoom_start=12) #StamenTerrain, StamenToner, StamenWatercolor #map_gdf_admin = map_df[['name','geometry']].set_index('name') map_gdf_sector = map_df_sector[['Sector','geometry']].set_index('Sector') #admin_count = map_df.name.value_counts() sector_count = map_df_sector.Sector.value_counts() Choropleth(geo_data = map_gdf_sector.__geo_interface__, data = sector_count, key_on="feature.id", fill_color = 'YlGn', fill_opacity=0, line_opacity=0.2, #legend_name = 'Jo count per state', smooth_factor=0).add_to(m_2) #https://python-visualization.github.io/folium/quickstart.html#Choropleth-maps folium.GeoJson(map_gdf_sector.__geo_interface__, name='geojson' ).add_to(m_2) folium_color = {'complete':'green','na':'orange'} rain_data_total.apply(lambda row:folium.CircleMarker(location=[row["Lat"], row["Long"]], radius=5, color=folium_color[row['Obs Status']], popup=row['Obs Status']).add_to(m_2), axis=1) # ref link -https://stackoverflow.com/questions/42756934/how-to-plot-lat-and-long-from-pandas-dataframe-on-folium-map-group-by-some-label m_2.save('static/pune_Chropleth.html') folium_html = m_2._repr_html_() #Plot output path #C:/Users/dell/AppData/Local/Temp/tmpzm9borp4.html return(m,folium_html)
def main(): global originator parser = argparse.ArgumentParser( description='based on prov json generate graph') parser.add_argument('-f', '--file', dest='file_name', required=True, help='name of file need to be process') args = parser.parse_args() with open(args.file_name) as f: data = json.load(f) root_url = data['root'] bundle_data = data['bundle'] originator = find_originator(bundle_data) #initial empty graph: G = nx.Graph() # add all nodes node_types = [item for item in bundle_data if item in NODE_TYPES] for node_type in node_types: print("add all " + node_type + " nodes") nodes = bundle_data[node_type] for node_name in nodes: node = nodes[node_name] n_type = node[add_prefix_to_name('type')] n_color, n_name = assign_color(n_type) n_value = node[add_prefix_to_name(n_name)] if n_name else None n_color = 'black' if n_value == root_url else n_color G.add_node(extract_name(node_name), type=n_type, color=n_color, value=n_value) # add all edges edge_types = [item for item in bundle_data if item not in NODE_TYPES] for edge_type in edge_types: print("add all " + edge_type + " edges") edges = bundle_data[edge_type] for edge_name in edges: edge = edges[edge_name] values = list(edge.values()) from_node = extract_name(values[0]) to_node = extract_name(values[1]) G.add_edge(from_node, to_node) # Show with Bokeh plot = Plot(plot_width=1200, plot_height=750, x_range=Range1d(-1.1, 1.1), y_range=Range1d(-1.1, 1.1)) plot.title.text = "Provenance graph" node_hover_tool = HoverTool(tooltips=[("type", "@type"), ("value", "@value")]) 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='color') graph_renderer.edge_renderer.glyph = MultiLine(line_alpha=0.8, line_width=1) plot.renderers.append(graph_renderer) output_file("provenance_graphs_" + args.file_name + ".html") show(plot)
def generateNavigatorFigure(dataframe, i, title): global pixelsForTitle global pixelsPerHeightUnit global plotWidth # Generate the colors, such that the current interval is shown in a # different color than the rest. # numIntervals = dataframe['intervalnumber'].size color = ["white" for x in range(numIntervals)] color[i] = "salmon" dataframe['color'] = color cds = ColumnDataSource(dataframe) title = title + " CLICK TO NAVIGATE" hover = HoverTool( tooltips=[("interval #", "@intervalnumber"), ("interval start", "@intervalbegin{0,0}" ), ("interval end", "@intervalend{0,0}")]) TOOLS = [hover, "tap"] p = figure(title=title, plot_width=plotWidth, x_range=(0, numIntervals), plot_height=2 * pixelsPerHeightUnit + pixelsForTitle, x_axis_label="", y_axis_label="", tools=TOOLS, toolbar_location="above") # No minor ticks or labels on the y-axis p.yaxis.major_tick_line_color = None p.yaxis.minor_tick_line_color = None p.yaxis.major_label_text_font_size = '0pt' p.yaxis.ticker = FixedTicker(ticks=list(range(0, 1))) p.ygrid.ticker = FixedTicker(ticks=list(range(0, 1))) p.xaxis.formatter = NumeralTickFormatter(format="0,") p.title.align = "center" p.title.text_font_style = "normal" p.quad(left='intervalnumber', right='intervalnumbernext', bottom=0, top=2, color='color', source=cds, nonselection_fill_color='color', nonselection_fill_alpha=1.0, line_color="aliceblue", selection_fill_color="white", selection_line_color="lightgrey") url = "@bucketfiles" taptool = p.select(type=TapTool) taptool.callback = OpenURL(url=url) return p
plot_width=570, plot_height=500, x_range = Range1d(), y_range = Range1d(), # border_fill = '#130f30', map_options=GMapOptions(lat=52.521123, lng=13.407478, zoom=10)) # tools="pan,wheel_zoom,box_zoom,reset,hover,save") p.map_options.map_type="satellite" # satellite, roadmap, terrain or hybrid source_patches = bk.ColumnDataSource(data=dict( boroughs_xs=boroughs_xs, boroughs_ys=boroughs_ys, boroughs_colors=boroughs_colors, boroughsnames=boroughsnames, population=population, area=area, density=density)) patches = Patches(xs="boroughs_xs", ys="boroughs_ys", fill_color="boroughs_colors", fill_alpha=0.5, line_color="black", line_width=0.5) patches_glyph = p.add_glyph(source_patches, patches) p.add_tools(PanTool(), WheelZoomTool(), BoxSelectTool(), HoverTool(), ResetTool(), PreviewSaveTool()) #xaxis = LinearAxis(axis_label="lat", major_tick_in=0, formatter=NumeralTickFormatter(format="0.000")) #p.add_layout(xaxis, 'below') #yaxis = LinearAxis(axis_label="lon", major_tick_in=0, formatter=PrintfTickFormatter(format="%.3f")) #p.add_layout(yaxis, 'left') hover = p.select(dict(type=HoverTool)) #hover.snap_to_data = False # not supported in new bokeh versions hover.tooltips = OrderedDict([ ("Borough", "@boroughsnames"), ("Density (pop/km2)", "@density"), # ("Population", "@population"), ("Area (km2)", "@area"), # ("(long,lat)", "($x, $y)"),
def generateBucketChartForFile(figureName, dataframe, y_max, x_min, x_max): global funcToColor global plotWidth global timeUnitString colorAlreadyUsedInLegend = {} MAX_ITEMS_PER_LEGEND = 10 numLegends = 0 legendItems = {} pixelsPerStackLevel = 30 pixelsPerLegend = 60 pixelsForTitle = 30 cds = ColumnDataSource(dataframe) hover = HoverTool( tooltips=[("function", "@function"), ( "duration", "@durations{0,0}"), ("log file begin timestamp", "@origstart{0,0}")]) TOOLS = [hover] p = figure(title=figureName, plot_width=plotWidth, x_range=(x_min, x_max), y_range=(0, y_max + 1), x_axis_label="Time (" + timeUnitString + ")", y_axis_label="Stack depth", tools=TOOLS, toolbar_location="above") # No minor ticks or labels on the y-axis p.yaxis.major_tick_line_color = None p.yaxis.minor_tick_line_color = None p.yaxis.major_label_text_font_size = '0pt' p.yaxis.ticker = FixedTicker(ticks=list(range(0, y_max + 1))) p.ygrid.ticker = FixedTicker(ticks=list(range(0, y_max + 1))) p.xaxis.formatter = NumeralTickFormatter(format="0,") p.title.text_font_style = "bold" p.quad(left='start', right='end', bottom='stackdepth', top='stackdepthNext', color='color', line_color="lightgrey", line_width=0.5, source=cds) for func, fColor in funcToColor.items(): # If this function is not present in this dataframe, # we don't care about it. # boolVec = (dataframe['function'] == func) fDF = dataframe[boolVec] if (fDF.size == 0): continue # If we already added a color to any legend, we don't # add it again to avoid redundancy in the charts and # in order not to waste space. # if fColor in colorAlreadyUsedInLegend: continue else: colorAlreadyUsedInLegend[fColor] = True legendItems[func] = fColor # Plot height is the function of the maximum call stack and the number of # legends p.plot_height = max( (y_max + 1) * pixelsPerStackLevel, 100) + pixelsForTitle return p, legendItems
def aggregate_plot(tb): """ Function for creating a bokeh plot that shows aggregate tax liabilities for each year the TaxBrain instance was run Parameters ---------- tb: An instance of the TaxBrain object Returns ------- Bokeh figure """ # Pull aggregate data by year and transpose it for plotting varlist = ["iitax", "payrolltax", "combined"] base_data = tb.multi_var_table(varlist, "base").transpose() base_data["calc"] = "Base" reform_data = tb.multi_var_table(varlist, "reform").transpose() reform_data["calc"] = "Reform" base_cds = ColumnDataSource(base_data) reform_cds = ColumnDataSource(reform_data) num_ticks = len(base_data) del base_data, reform_data fig = figure(title="Aggregate Tax Liability by Year", width=700, height=500, tools="save") ii_base = fig.line(x="index", y="iitax", line_width=4, line_color="#12719e", legend_label="Income Tax - Base", source=base_cds) ii_reform = fig.line(x="index", y="iitax", line_width=4, line_color="#73bfe2", legend_label="Income Tax - Reform", source=reform_cds) proll_base = fig.line(x="index", y="payrolltax", line_width=4, line_color="#408941", legend_label="Payroll Tax - Base", source=base_cds) proll_reform = fig.line(x="index", y="payrolltax", line_width=4, line_color="#98cf90", legend_label="Payroll Tax - Reform", source=reform_cds) comb_base = fig.line(x="index", y="combined", line_width=4, line_color="#a4201d", legend_label="Combined - Base", source=base_cds) comb_reform = fig.line(x="index", y="combined", line_width=4, line_color="#e9807d", legend_label="Combined - Reform", source=reform_cds) # format figure fig.legend.location = "top_left" fig.yaxis.formatter = NumeralTickFormatter(format="$0.00a") fig.yaxis.axis_label = "Aggregate Tax Liability" fig.xaxis.minor_tick_line_color = None fig.xaxis[0].ticker.desired_num_ticks = num_ticks # Add hover tool tool_str = """ <p><b>@calc - {}</b></p> <p>${}</p> """ ii_hover = HoverTool(tooltips=tool_str.format("Individual Income Tax", "@iitax{0,0}"), renderers=[ii_base, ii_reform]) proll_hover = HoverTool(tooltips=tool_str.format("Payroll Tax", "@payrolltax{0,0}"), renderers=[proll_base, proll_reform]) combined_hover = HoverTool(tooltips=tool_str.format( "Combined Tax", "@combined{0,0}"), renderers=[comb_base, comb_reform]) fig.add_tools(ii_hover, proll_hover, combined_hover) # toggle which lines are shown plot_js = """ object1.visible = toggle.active object2.visible = toggle.active object3.visible = toggle.active """ base_callback = CustomJS(code=plot_js, args={}) base_toggle = Toggle(label="Base", button_type="primary", active=True) base_callback.args = { "toggle": base_toggle, "object1": ii_base, "object2": proll_base, "object3": comb_base } base_toggle.js_on_change('active', base_callback) reform_callback = CustomJS(code=plot_js, args={}) reform_toggle = Toggle(label="Reform", button_type="primary", active=True) reform_callback.args = { "toggle": reform_toggle, "object1": ii_reform, "object2": proll_reform, "object3": comb_reform } fig_layout = layout([fig], [base_toggle, reform_toggle]) reform_toggle.js_on_change('active', reform_callback) # Components needed to embed the figure data = json_item(fig_layout) outputs = { "media_type": "bokeh", "title": "", "data": data, } return outputs
def main() -> None: """The main function.""" # Get the data reader_cors = csv.reader( open("../combined_graph/clusters/node_coordination.txt"), delimiter=";" ) reader_clusters = csv.reader( open("../combined_graph/clusters/node_clusters.txt"), delimiter=";" ) node_data = top_10_data() # Process the data names: List[str] = [] clusters: List[float] = [] categories: List[str] = [] x_cors: List[float] = [] y_cors: List[float] = [] top_10s: List[str] = [] similarities: List[List[float]] = [] for row_cors, row_clusters in zip(reader_cors, reader_clusters): if "@" in row_cors[0]: items = row_cors[0].split("@") names.append(items[0]) categories.append(items[1]) else: names.append(row_cors[0]) categories.append("NA") clusters.append(int(row_clusters[1])) x_cors.append(float(row_cors[1])) y_cors.append(float(row_cors[2])) top = top_10(row_cors[0], node_data) if len(top) == 2: top_10s.append(", ".join(top[0])) similarities.append(top[1]) elif len(top) == 1: if top[0]: top_10s.append(", ".join(top[0])) elif top[1]: similarities.append(top[1]) elif len(top) == 0: top_10s.append("") similarities.append([]) # Cleanup names = [name.replace("\t", " ").replace(" ", " ") for name in names] # Create a dataframe df = pd.DataFrame( { "name": names, "cluster": clusters, "category": categories, "x": x_cors, "y": y_cors, "top10": top_10s, "similarity": similarities, } ) # Selecting the colors for each unique category in album_name unique_clusters = df["cluster"].unique() palette = magma(len(unique_clusters) + 1) # Mapping each category with a color of Set2 colormap = dict(zip(unique_clusters, palette)) # Making a color column based on album_name df["color"] = df["cluster"].map(colormap) # Interactive elements interactive_tools = [ HoverTool( tooltips=[ ("name", "@name"), ("type", "@category"), ("top10", "@top10"), ] ), WheelZoomTool(), PanTool(), BoxZoomTool(), ResetTool(), SaveTool(), ] # Define the plot and add the attributes plot = figure( tools=interactive_tools, title="COVID-19 Co-occurence Network Embeddings Visualization " "(by David Oniani and Dr. Feichen Shen)", background_fill_color="#fafafa", plot_height=300, sizing_mode="scale_width", ) plot.toolbar.active_scroll = plot.select_one(WheelZoomTool) # Remove redundant elements plot.axis.visible = False plot.title.align = "center" plot.title.text_font_size = "16pt" plot.title.text_font_style = "italic" # Show (and save) the plot plot.circle( x="x", y="y", source=ColumnDataSource(df), color="color", alpha=0.8, size=4.5, ) # output_file( # "graph.html", # title="COVID-19 Co-occurence Network Embeddings Visualization", # ) # show(plot) # CODE GENERATION -- MIGHT LOOK UGLY with open("search.html", "w") as file: file.write("<!DOCTYPE html>\n") file.write('<html lang="en">\n') file.write(" <head>\n") file.write(' <meta charset="utf-8">\n') file.write( ' <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>\n' ) file.write( ' <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">\n' ) file.write( ' <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>\n' ) file.write( ' <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/select2.min.css" rel="stylesheet" />\n' ) file.write(" <head>\n") file.write(" <body>\n") file.write(' <div class="container">\n') file.write(' <div class="row mt-2 mx-auto">\n') file.write(' <div class="col">\n') # Selection file.write(' <select id="search" class="btn form-control">\n') reader_assoc = csv.reader( open("../combined_graph/Combined_Dict.txt"), delimiter=";" ) names_cats: Dict[str, str] = {} for row in reader_assoc: if "@" in row[0]: items = row[0].split("@") names_cats[ items[0].replace("\t", " ").replace(" ", " ") ] = items[1] else: names_cats[row[0].replace("\t", " ").replace(" ", " ")] = "NA" for name, category, top, similarity in zip( df["name"], df["category"], df["top10"], df["similarity"] ): similarity = ", ".join([str(i) for i in similarity]) temp = [] for item in top.split(","): try: temp.append(names_cats[item.strip()]) except KeyError: temp.append("NA") types = ", ".join(temp) file.write( f' <option value="{name} ({category})" name="{name}" types="{types}" top="{top}" similarity="{similarity}">{name} ({category})</option>\n' ) file.write(" </select>\n") # Table file.write(" </div>\n") file.write(" </div>\n") file.write(' <div class="row mt-2 mx-auto">\n') file.write(' <div class="col">\n') file.write( ' <table id="similarityTable" class="table table-striped">\n' ) file.write(' <thead class="thead-light">\n') file.write(" <tr>\n") file.write(' <th scope="col">Name</th>\n') file.write(' <th scope="col">Type</th>\n') file.write(' <th scope="col">Association</th>\n') file.write(' <th scope="col">Cosine Similarity</th>\n') file.write(" <tr>\n") file.write(" <thead>\n") file.write(" <tbody></tbody>\n") file.write(" <table>\n") file.write(" </div>\n") file.write(" </div>\n") file.write(" <div>\n") # Scripts file.write( ' <script>$("#search").select2({ placeholder: "Select an entity" });</script>\n' ) file.write( ' <script>$("#search").on("select2:select", function(e) { ' "let attrs = e.params.data.element.attributes; " "let name = attrs.name.nodeValue; " 'let tops = attrs.top.nodeValue.split(","); ' 'let types = attrs.types.nodeValue.split(","); ' 'let sims = attrs.similarity.nodeValue.split(","); ' "console.log(attrs); " '$("#similarityTable tbody").empty(); ' "tops.forEach((top, idx) => { " "let sim = sims[idx]; " "let type = types[idx]; " '$("#similarityTable").append(`<tr><td>${top.trim()}</td><td>${type.trim()}</td><td>${name}</td><td>${sim.trim()}</td></tr>`);}) ' "})" "</script>\n", ) file.write(" <body>\n") file.write("</html>\n")
def plot_benchmark(savefile, benchmarks, title, xaxis_type, yaxis_type, save_prefix=""): show_backends = False show_os = False # Determine the number of backends backends = list_recordTable_attribute(benchmarks, 'AF_PLATFORM') if len(backends) > 1: show_backends = True operating_sys = list_recordTable_attribute(benchmarks, 'AF_OS') if len(operating_sys) > 1: show_os = True # Sort the benchmarks by device name bmarks_sorted = sorted(benchmarks, key=lambda k: k['extra_data']['AF_DEVICE']) benchmarks = bmarks_sorted # configure the colors colors = unique_colors() # configure the hover box hover = HoverTool( tooltips = [ ("Device", "@device"), ("Backend", "@platform"), ("OS", "@os"), ("(x,y)", "(@x,@y)") ]) # configure the plot title and axis labels, use CDN for the data source #bplt.output_file(save_prefix + savefile + ".html", title=title, mode='cdn') bplt.output_file(save_prefix + savefile + ".html", title=title) plot = bplt.figure(title=title, tools=[hover,'save,box_zoom,resize,reset']) xlabel = "" ylabel = "" legend_location = "top_right" # plot images/second vs. data size scatter_renderers = list() for benchmark in benchmarks: # get the color we will use for this plot color = colors.next() # extract benchmarks x,xlabel,legend_location = format_data(benchmark, xaxis_type) y,ylabel,legend_location = format_data(benchmark, yaxis_type) platform = benchmark['extra_data']['AF_PLATFORM'] # get the device name, override if necessary device = benchmark['extra_data']['AF_DEVICE'] operating_system = benchmark['extra_data']['AF_OS'] if 'AF_LABEL' in benchmark['extra_data'].keys(): device = benchmark['extra_data']['AF_LABEL'] source = bplt.ColumnDataSource( data = dict(x=x,y=y, device=[device]*len(x), platform=[platform]*len(x), os=[operating_system]*len(x), )) # Generate the legend, automatically add the platform if needed legend = device if show_os or show_backends: legend += "( " if show_os: legend += operating_system + " " if show_backends: legend += platform + " " if show_os or show_backends: legend += ")" # generate the plot plot.line(x,y, legend=legend, color=color, line_width=2) sr = plot.scatter('x', 'y', source=source, legend=legend, color=color, fill_color="white", size=8) scatter_renderers.append(sr) hover = plot.select(HoverTool) hover.renderers = scatter_renderers plot.xaxis.axis_label = xlabel plot.yaxis.axis_label = ylabel plot.legend.location = legend_location # save the plot bplt.save(plot)
def fit(self, cv_results, estimeted_end_time): cv_results, cv_score_std, param_dists = self._init_cv_results( cv_results) nbi = len(cv_results) tot = self.nbTot 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): {}".format( 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.add_tools(cv_hover) #TODO HOVER 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]) self.stri = randomString() title = Div(text=NoteBookVisualizer.title.replace( "TEXT", self.model_id), width=int(NoteBookVisualizer.display_width)) scores_headline = Div(text=NoteBookVisualizer.headline.replace( "TEXT", "<span id='ooi_{}'> Score History ({}/{})</span>".format( self.stri, nbi, tot)), 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): {}".format( 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) try: nbi = len( self.cv_src.data[NoteBookVisualizer.time_col]) + 1 if nbi > self.last: from IPython.display import Javascript, display display( Javascript(""" document.getElementById("ooi_{}").innerHTML=" Score History ({}/{})"; """.format(self.stri, nbi, tot))) self.last = nbi except: pass 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 scatter_with_hover(df, x, y, fig=None, cols=None, name=None, marker='x', fig_width=500, fig_height=500, **kwargs): """ Plots an interactive scatter plot of `x` vs `y` using bokeh, with automatic tooltips showing columns from `df`. Parameters ---------- df : pandas.DataFrame DataFrame containing the data to be plotted x : str Name of the column to use for the x-axis values y : str Name of the column to use for the y-axis values fig : bokeh.plotting.Figure, optional Figure on which to plot (if not given then a new figure will be created) cols : list of str Columns to show in the hover tooltip (default is to show all) name : str Bokeh series name to give to the scattered data marker : str Name of marker to use for scatter plot **kwargs Any further arguments to be passed to fig.scatter Returns ------- bokeh.plotting.Figure Figure (the same as given, or the newly created figure) Example ------- fig = scatter_with_hover(df, 'A', 'B') show(fig) fig = scatter_with_hover(df, 'A', 'B', cols=['C', 'D', 'E'], marker='x', color='red') show(fig) Author ------ Robin Wilson <*****@*****.**> with thanks to Max Albert for original code example """ # If we haven't been given a Figure obj then create it with default # size etc. if fig is None: fig = figure(width=fig_width, height=fig_height, tools=['box_zoom', 'reset']) # We're getting data from the given dataframe source = ColumnDataSource(data=df) # We need a name so that we can restrict hover tools to just this # particular 'series' on the plot. You can specify it (in case it # needs to be something specific for other reasons), otherwise # we just use 'main' if name is None: name = 'main' # Actually do the scatter plot - the easy bit # (other keyword arguments will be passed to this function) fig.scatter(df[x], df[y], source=source, name=name, marker=marker, **kwargs) # Now we create the hover tool, and make sure it is only active with # the series we plotted in the previous line hover = HoverTool(names=[name]) if cols is None: # Display *all* columns in the tooltips hover.tooltips = [(c, '@' + c) for c in df.columns] else: # Display just the given columns in the tooltips hover.tooltips = [(c, '@' + c) for c in cols] hover.tooltips.append(('index', '$index')) # Finally add/enable the tool fig.add_tools(hover) return fig
content_data.to_csv('data/content_data.csv', index = False) content_data['length'] = content_data['content'].str.strip().str.len() import bokeh.plotting as bp from bokeh.io import show from bokeh.models import HoverTool array = content_data['length'][content_data['length'].values < 100000].values hist, edges = np.histogram(array, bins=50) source = bp.ColumnDataSource(data = dict(data_value = hist)) p = bp.figure() p.quad(top=hist, bottom=0, left=edges[:-1], right=edges[1:], line_color="white", source = source) p.add_tools(HoverTool(tooltips= [("Value:", "@data_value")])) p.xaxis.axis_label = 'Length of Article' p.yaxis.axis_label = 'Frequency' show(p) content_data = content_data.loc[content_data['length'] > 60,] content_data = content_data.loc[content_data['length'] < 160000,] content_data = content_data.reset_index(drop = True) import gensim from sklearn.feature_extraction.text import CountVectorizer import nltk
lon = longitude, color = loc_color, rprice = price, dis = distance, addr = address, restaurant = food, cafe = cafes, pub = pubs, size = sqft, ) ) circle = Circle(x = 'lon', y = 'lat', fill_color = 'color', size = 10, fill_alpha=0.6, line_color=None) plot.add_glyph(source, circle) # Hover hover = HoverTool() hover.point_policy = "follow_mouse" hover.tooltips = [ ("Address", "@addr"), ("Price per Room", "@rprice"), ("Distance to Campus", "@dis"), ("Food Quality", "@restaurant"), ("Number of cafes", "@cafe"), ("Number of pubs", "@pub"), ("Room size", "@size"), ] tools = [PanTool(), WheelZoomTool(), hover] plot.add_tools(*tools) output_file("gmap_plot.html")
def create_plot_figure(self, active_tab): """ create a new plot and insert it in given tab. """ #find table name of active tab and its bokeh instances test = active_tab.name #contains csv filename x_sel = active_tab.select_one({'name': 'x_sel'}) y_sel = active_tab.select_one({'name': 'y_sel'}) y_sel2 = active_tab.select_one({'name': 'y_sel2'}) plot_df = self.plot_dfs[test] source = ColumnDataSource(plot_df) #Replace entirely p with a new plot p = Plot(x_range=DataRange1d(), y_range=DataRange1d(), plot_height=600, plot_width=600, title=Title(text=self.sel_csv), name='plot') p.add_tools(BoxZoomTool(), SaveTool(), ResetTool(), PanTool(), HoverTool(tooltips=[('x', '$x'), ('y', '$y')])) #see https://bokeh.github.io/blog/2017/7/5/idiomatic_bokeh/ x_axis = LinearAxis(axis_label=x_sel.value, ticker=BasicTicker(desired_num_ticks=10), name='x_axis') y_axis = LinearAxis(axis_label=y_sel.value, ticker=BasicTicker(desired_num_ticks=10), name='y_axis') #primary y-axis ly = p.add_glyph(source, Line(x=x_sel.value, y=y_sel.value, line_width=2, line_color='black'), name='ly') p.add_layout(x_axis, 'below') p.add_layout(y_axis, 'left') p.y_range.renderers = [ly] #secondary y-axis if y_sel2.value.strip() != 'None': #secondary y-axis y_axis2 = LinearAxis(axis_label=y_sel2.value, ticker=BasicTicker(desired_num_ticks=10), name='y_axis2', y_range_name='right_axis') p.add_layout(y_axis2, 'right') p.extra_y_ranges = {"right_axis": DataRange1d()} ly2 = p.add_glyph(source, Line(x=x_sel.value, y=y_sel2.value, line_width=2, line_color='red'), y_range_name='right_axis', name='ly2') p.extra_y_ranges['right_axis'].renderers = [ly2] leg_items = [ LegendItem(label=y_sel.value, renderers=[ly]), LegendItem(label=y_sel2.value, renderers=[ly2]) ] else: leg_items = [LegendItem(label=y_sel.value, renderers=[ly])] p.add_layout(Legend(items=leg_items, location='top_right')) active_tab.child.children[1] = p return p