def test_basic(self): t = bt.linear_cmap("foo", ["red", "green"], 0, 10, low_color="orange", high_color="blue", nan_color="pink") assert isinstance(t, dict) assert set(t) == {"field", "transform"} assert t['field'] == "foo" assert isinstance(t['transform'], LinearColorMapper) assert t['transform'].palette == ["red", "green"] assert t['transform'].low == 0 assert t['transform'].high is 10 assert t['transform'].low_color == "orange" assert t['transform'].high_color == "blue" assert t['transform'].nan_color == "pink"
def test_defaults(self): t = bt.linear_cmap("foo", ["red", "green"], 0, 10) assert isinstance(t, dict) assert set(t) == {"field", "transform"} assert t['field'] == "foo" assert isinstance(t['transform'], LinearColorMapper) assert t['transform'].palette == ["red", "green"] assert t['transform'].low == 0 assert t['transform'].high is 10 assert t['transform'].low_color is None assert t['transform'].high_color is None assert t['transform'].nan_color == "gray"
def plot(): import pandas as pd import numpy as np #import folium from bokeh.plotting import figure from bokeh.embed import components from bokeh.resources import CDN from bokeh.models import ColumnDataSource, ColorBar from bokeh.palettes import Spectral6 from bokeh.transform import linear_cmap df=pd.read_csv("Santa_Margarita_pre.csv",parse_dates=['fecha'],decimal='.') aapl = np.array(df['pronostico']) aapl_dates = np.array(df['fecha'], dtype=np.datetime64) # output to static HTML file #0output_file("app3/templates/SMargarita.html", title="Prediction example") # add renderers #Use the field name of the column source mapper = linear_cmap(field_name='y', palette=Spectral6 ,low=min(aapl) ,high=max(aapl)) source = ColumnDataSource(dict(x=aapl_dates,y=aapl)) # create a new plot with a a datetime axis type p =figure(plot_width=1000, plot_height=500, x_axis_type="datetime", title="Previsión niveles NO2 (Est. Santa Margarita, A Coruña, Galicia, España)") p.circle(x='x', y='y', line_color=mapper,color=mapper, fill_alpha=1, size=12, source=source) color_bar = ColorBar(color_mapper=mapper['transform'], width=8, location=(0,0)) p.add_layout(color_bar, 'right') #p.circle(aapl_dates, aapl, size=10, color='black', alpha=0.2, legend='NO2') p.line(aapl_dates, aapl, color='navy') script1,div1= components(p) cdn_js=CDN.js_files[0] cdn_css=CDN.css_files[0] return render_template("plot.html",script1=script1,div1=div1, cdn_css=cdn_css,cdn_js=cdn_js) return render_template("about.html")
def create_plot(): # Add figure p = figure( title='', plot_width=1000, plot_height=700, x_axis_label='ID', toolbar_location='above', tools='pan, box_select, save, undo, redo, xwheel_zoom, reset' ) # Save value for y-axis from select input widget within the "top" variable # in the glyph. It will automatically update on changing the select widget within the application. y_axis = head_columns[sel_yaxis.value] x_axis = head_columns[sel_xaxis.value] # Set the label on y-axis so that the glyph displayed and y-axis label match p.yaxis.axis_label = sel_yaxis.value p.xaxis.axis_label = sel_xaxis.value # Holds the x-axis ID values for the glyph id = head_columns['id'] # compare range slider values with y-axis values # Manipulate the ColumnDataSource so that it only contains the values which meets # the conditions of the range slider current = df[(df[y_axis] >= num_tokens.value[0]) & (df[y_axis] <= num_tokens.value[1])].dropna() source.data = { 'ID': current.ID, 'nrOfMatches_AmplifierWordu46Amplifier': current.nrOfMatches_AmplifierWordu46Amplifier, 'nrOfMatches_BeWordu46BeAsMainVerb': current.nrOfMatches_BeWordu46BeAsMainVerb, 'nrOfMatches_CausativeAdverbialSubordinatorWordu46CausativeAdverbialSubordinator': current.nrOfMatches_CausativeAdverbialSubordinatorWordu46CausativeAdverbialSubordinator, 'nrOfMatches_ConcessiveAdverbialSubordinatorWordu46ConcessiveAdverbialSubordinator': current.nrOfMatches_ConcessiveAdverbialSubordinatorWordu46ConcessiveAdverbialSubordinator, 'nrOfMatches_ConditionalAdverbialSubordinatorWordu46ConditionalAdverbialSubordinator': current.nrOfMatches_ConditionalAdverbialSubordinatorWordu46ConditionalAdverbialSubordinator, 'nrOfMatches_DemonstrativeWordu46Demonstrative': current.nrOfMatches_DemonstrativeWordu46Demonstrative, 'nrOfMatches_DowntonerWordu46Downtoner': current.nrOfMatches_DowntonerWordu46Downtoner, 'nrOfMatches_EmphaticWordu46Emphatic': current.nrOfMatches_EmphaticWordu46Emphatic, 'nrOfMatches_HedgeWordu46Hedge': current.nrOfMatches_HedgeWordu46Hedge, 'nrOfMatches_InfinitiveWordu46Infinitive': current.nrOfMatches_InfinitiveWordu46Infinitive, 'nrOfMatches_OtherAdverbialSubordinatorWordu46OtherAdverbialSubordinator': current.nrOfMatches_OtherAdverbialSubordinatorWordu46OtherAdverbialSubordinator, 'nrOfMatches_PassivesWordu46AgentlessPassive': current.nrOfMatches_PassivesWordu46AgentlessPassive, 'nrOfMatches_PassivesWordu46ByPassive': current.nrOfMatches_PassivesWordu46ByPassive, 'nrOfMatches_PronounWordu46ItPronoun': current.nrOfMatches_PronounWordu46ItPronoun, 'nrOfMatches_ThatComplementWordu46ThatAdjectiveComplement': current.nrOfMatches_ThatComplementWordu46ThatAdjectiveComplement, 'nrOfMatches_ThatComplementWordu46ThatVerbComplement': current.nrOfMatches_ThatComplementWordu46ThatVerbComplement, 'nrOfMatches_WHClauseWordu46WH_Clause': current.nrOfMatches_WHClauseWordu46WH_Clause, 'outcome': current.outcome } # Define colors and provide it to linear_cmap for coding different plot items respectively to # their outcome colours = ['red', 'orange', 'green'] mapper = linear_cmap(field_name=head_columns['Outcome'], palette=colours, low=0.0, high=12.0) # If-statements which takes the value of the y-axis selection widget and uses # it to provide the right plot type to the figure if sel_plot.value == 'vbar': p.vbar( x=x_axis, top=y_axis, source=source, width=0.8, color=mapper, hover_color='violet', line_alpha=0.3, ) elif sel_plot.value == 'scatter': p.scatter( x=x_axis, y=y_axis, color=mapper, hover_color='violet', source=source, ) elif sel_plot.value == 'line': out = [] sub = source.to_df() for i in sub[x_axis]: if i in out: continue else: out.append(i) x_line = df.groupby(by=x_axis).mean() p.line( x=sorted(out), y=x_line[y_axis] ) elif sel_plot.value == 'varea': out = [] sub = source.to_df() for i in sub[x_axis]: if i in out: continue else: out.append(i) area1 = df.copy().groupby(by='outcome').min() area2 = df.copy().groupby(by='outcome').max() p.varea( x=sorted(out), y1=area1[y_axis], y2=area2[y_axis], fill_alpha=0.7, ) # Create tooltips with HTML and add them to the plot if sel_plot.value == 'scatter' or sel_plot.value == 'vbar': hover = HoverTool() hover.tooltips = """ <div> <h3><strong>ID: </strong>@ID</h3> <div><strong>Outcome: </strong>@outcome</div> </div> """ p.add_tools(hover) # Remove vertical grid lines p.xgrid.grid_line_color = None # Remove logo p.toolbar.logo = None # Create tabs. One for the plot and another for the table view t1 = Panel(child=p, title='Plot') t2 = Panel(child=data_table, title='Table') tabs = Tabs(tabs=[t1, t2]) return tabs
def makeGraph(request, df_enron): G = networkx.from_pandas_edgelist(df_enron, 'fromId', 'toId', edge_attr=True) di = { 'CEO': 1, 'Director': 2, 'Employee': 3, 'In House Lawyer': 4, 'Manager': 5, 'Managing Director': 6, 'President': 7, 'Trader': 8, 'Unknown': 9, 'Vice President': 10 } df_rejob = df_enron.replace({"fromJobtitle": di}) df_attributes = df_enron[['fromId', 'fromJobtitle', 'fromEmail']].drop_duplicates() df_attributes.columns = ['fromId', 'job', 'fromEmail'] df_attributesx = df_rejob[['fromId', 'fromJobtitle', 'fromEmail']].drop_duplicates() job = df_attributes.set_index('fromId').to_dict('i') jobx = df_attributesx.set_index('fromId').to_dict('i') fromEmail = df_attributes.set_index('fromEmail').to_dict('i') networkx.set_node_attributes(G, job) networkx.set_node_attributes(G, jobx) networkx.set_node_attributes(G, fromEmail) #jobs = ['Employee','Vice President','Unknown','Manager','CEO','Trader','Director','President','Managing Director','In House Lawyer'] degrees = dict(networkx.degree(G)) networkx.set_node_attributes(G, name='degree', values=degrees) adjusted_node_size = dict([(node, (degree + 5) - ((degree + 5) * 0.3)) for node, degree in networkx.degree(G)]) networkx.set_node_attributes(G, name='adjusted_node_size', values=adjusted_node_size) size_by_this_attribute = 'adjusted_node_size' color_by_this_attribute = 'fromJobtitle' color_palette = Category10[10] TOOLTIPS = [ ("Person ID", "@index"), ("Email", "@fromEmail"), ("people communicated with", "@degree"), ("Jobtitle", "@job"), ] graph_size = int(request.POST.get('graph_size', '720')) plot = figure(tooltips=TOOLTIPS, tools="pan,zoom_in,wheel_zoom,save,reset,box_select,undo", active_scroll='wheel_zoom', x_range=Range1d(-20, 20), y_range=Range1d(-20, 20), title='Enron Emails', plot_width=graph_size, plot_height=graph_size) plot.axis.visible = False N_graph = from_networkx(G, networkx.spring_layout, scale=100) N_graph.node_renderer.glyph = Circle(size=size_by_this_attribute, fill_color=linear_cmap( color_by_this_attribute, color_palette, 1, 10)) N_graph.edge_renderer.glyph = MultiLine(line_alpha=10, line_width=1) plot.renderers.append(N_graph) item_text = json.dumps(json_item(plot)) return item_text
from bokeh.plotting import figure, show, output_file from bokeh.models import ColumnDataSource, ColorBar from bokeh.palettes import Spectral6 from bokeh.transform import linear_cmap output_file("styling_linear_mappers.html", title="styling_linear_mappers.py example") x = [1,2,3,4,5,7,8,9,10] y = [1,2,3,4,5,7,8,9,10] #Use the field name of the column source mapper = linear_cmap(field_name='y', palette=Spectral6 ,low=min(y) ,high=max(y)) source = ColumnDataSource(dict(x=x,y=y)) p = figure(plot_width=300, plot_height=300, title="Linear Color Map Based on Y") p.circle(x='x', y='y', line_color=mapper,color=mapper, fill_alpha=1, size=12, source=source) color_bar = ColorBar(color_mapper=mapper['transform'], width=8, location=(0,0)) p.add_layout(color_bar, 'right') show(p)
def create_figure(m, m_source, columns): print("creating figure with x = %s, y = %s, color = %s, size = %s" % (x.value, y.value, color.value, size.value)) tooltips = [("id", "@id"), (x.value, "@{%s}" % x.value), (y.value, "@{%s}" % y.value)] if color.value != 'None': tooltips += [(color.value, "@{%s}" % color.value)] if size.value != 'None': tooltips += [(size.value, "@{%s}" % size.value)] x_range = range_defaults[x.value] if x.value in range_defaults else None y_range = range_defaults[y.value] if y.value in range_defaults else None p = figure(plot_height=800, plot_width=800, x_range=x_range, y_range=y_range, tooltips=tooltips, tools=["tap", "hover", "box_select", "reset", "save"], title=("%s: %s vs %s" % (data.value, y.value, x.value))) p.xaxis.axis_label = x.value p.yaxis.axis_label = y.value sz = 8 print("size.value = '%s'" % size.value) if size.value != 'None': if (size.value + "_plotsize") in m: sz = size.value + "_plotsize" else: sz = size.value print(sz) mapper = None c = "#31AADE" if color.value != 'None': if color.value in colormap_overrides: colormap_args = colormap_overrides[color.value] else: colormap_args = dict(palette=Viridis5) if 'low' not in colormap_args: colormap_args['low'] = m[color.value].min() if 'high' not in colormap_args: colormap_args['high'] = m[color.value].max() print(color.value, colormap_args) mapper = linear_cmap(field_name=color.value, **colormap_args) c = mapper p.circle(x=x.value, y=y.value, color=c, size=sz, line_color=c, alpha=0.4, hover_color='white', hover_alpha=0.7, source=m_source) fs = "1.3em" if mapper: color_bar = ColorBar(color_mapper=mapper['transform'], width=8, location=(0, 0)) color_bar.major_label_text_font_size = fs color_bar.major_label_text_align = "left" p.add_layout(color_bar, 'right') p.yaxis.axis_label_text_font_size = fs p.yaxis.major_label_text_font_size = fs p.xaxis.axis_label_text_font_size = fs p.xaxis.major_label_text_font_size = fs p.title.text_font_size = fs return p
p = figure(title="CSD drug subset", tools="wheel_zoom,pan,reset", match_aspect=True, x_range=(left, right), y_range=(bottom, top), background_fill_color='#440154', tooltips=TOOLTIPS) p.grid.visible = False p.hex_tile(q="x", r="y", size=0.08, line_color=None, source=source, fill_color=linear_cmap('counts', 'Viridis256', 0, max(bins.counts))) controls = [year] for control in controls: control.on_change('value', lambda attr, old, new: update()) sizing_mode = 'fixed' # 'scale_width' also looks nice with this example inputs = column(*controls, sizing_mode=sizing_mode) l = layout([ [inputs, p], ], sizing_mode=sizing_mode) update() # initial load of the data curdoc().add_root(l)
def interactive( umap_object, labels=None, values=None, hover_data=None, theme=None, cmap="Blues", color_key=None, color_key_cmap="Spectral", background="white", width=800, height=800, point_size=None, subset_points=None, ): """Create an interactive bokeh plot of a UMAP embedding. While static plots are useful, sometimes a plot that supports interactive zooming, and hover tooltips for individual points is much more desireable. This function provides a simple interface for creating such plots. The result is a bokeh plot that will be displayed in a notebook. Note that more complex tooltips etc. will require custom code -- this is merely meant to provide fast and easy access to interactive plotting. Parameters ---------- umap_object: trained UMAP object A trained UMAP object that has a 2D embedding. labels: array, shape (n_samples,) (optional, default None) An array of labels (assumed integer or categorical), one for each data sample. This will be used for coloring the points in the plot according to their label. Note that this option is mutually exclusive to the ``values`` option. values: array, shape (n_samples,) (optional, default None) An array of values (assumed float or continuous), one for each sample. This will be used for coloring the points in the plot according to a colorscale associated to the total range of values. Note that this option is mutually exclusive to the ``labels`` option. hover_data: DataFrame, shape (n_samples, n_tooltip_features) (optional, default None) A dataframe of tooltip data. Each column of the dataframe should be a Series of length ``n_samples`` providing a value for each data point. Column names will be used for identifying information within the tooltip. theme: string (optional, default None) A color theme to use for plotting. A small set of predefined themes are provided which have relatively good aesthetics. Available themes are: * 'blue' * 'red' * 'green' * 'inferno' * 'fire' * 'viridis' * 'darkblue' * 'darkred' * 'darkgreen' cmap: string (optional, default 'Blues') The name of a matplotlib colormap to use for coloring or shading points. If no labels or values are passed this will be used for shading points according to density (largely only of relevance for very large datasets). If values are passed this will be used for shading according the value. Note that if theme is passed then this value will be overridden by the corresponding option of the theme. color_key: dict or array, shape (n_categories) (optional, default None) A way to assign colors to categoricals. This can either be an explicit dict mapping labels to colors (as strings of form '#RRGGBB'), or an array like object providing one color for each distinct category being provided in ``labels``. Either way this mapping will be used to color points according to the label. Note that if theme is passed then this value will be overridden by the corresponding option of the theme. color_key_cmap: string (optional, default 'Spectral') The name of a matplotlib colormap to use for categorical coloring. If an explicit ``color_key`` is not given a color mapping for categories can be generated from the label list and selecting a matching list of colors from the given colormap. Note that if theme is passed then this value will be overridden by the corresponding option of the theme. background: string (optional, default 'white') The color of the background. Usually this will be either 'white' or 'black', but any color name will work. Ideally one wants to match this appropriately to the colors being used for points etc. This is one of the things that themes handle for you. Note that if theme is passed then this value will be overridden by the corresponding option of the theme. width: int (optional, default 800) The desired width of the plot in pixels. height: int (optional, default 800) The desired height of the plot in pixels point_size: int (optional, default None) The size of each point marker subset_points: array, shape (n_samples,) (optional, default None) A way to select a subset of points based on an array of boolean values. Returns ------- """ if theme is not None: cmap = _themes[theme]["cmap"] color_key_cmap = _themes[theme]["color_key_cmap"] background = _themes[theme]["background"] if labels is not None and values is not None: raise ValueError( "Conflicting options; only one of labels or values should be set") points = umap_object.embedding_ if subset_points: points = points[subset_points] if points.shape[1] != 2: raise ValueError( "Plotting is currently only implemented for 2D embeddings") if point_size is None: point_size = 100.0 / np.sqrt(points.shape[0]) data = pd.DataFrame(umap_object.embedding_, columns=("x", "y")) if labels is not None: data["label"] = labels if color_key is None: unique_labels = np.unique(labels) num_labels = unique_labels.shape[0] color_key = _to_hex( plt.get_cmap(color_key_cmap)(np.linspace(0, 1, num_labels))) if isinstance(color_key, dict): data["color"] = pd.Series(labels).map(color_key) else: unique_labels = np.unique(labels) if len(color_key) < unique_labels.shape[0]: raise ValueError( "Color key must have enough colors for the number of labels" ) new_color_key = { k: color_key[i] for i, k in enumerate(unique_labels) } data["color"] = pd.Series(labels).map(new_color_key) colors = "color" elif values is not None: data["value"] = values palette = _to_hex(plt.get_cmap(cmap)(np.linspace(0, 1, 256))) colors = btr.linear_cmap("value", palette, low=np.min(values), high=np.max(values)) else: colors = matplotlib.colors.rgb2hex(plt.get_cmap(cmap)(0.5)) if subset_points is not None: if len(subset_points) != points.shape[0]: raise ValueError( "Size of subset points ({}) does not match number of input points ({})" .format(len(subset_points), points.shape[0])) data = data[subset_points] if hover_data is not None: hover_data = hover_data[subset_points] if points.shape[0] <= width * height // 10: if hover_data is not None: tooltip_dict = {} for col_name in hover_data: data[col_name] = hover_data[col_name] tooltip_dict[col_name] = "@" + col_name tooltips = list(tooltip_dict.items()) else: tooltips = None # bpl.output_notebook(hide_banner=True) # this doesn't work for non-notebook use data_source = bpl.ColumnDataSource(data) plot = bpl.figure( width=width, height=height, tooltips=tooltips, background_fill_color=background, ) plot.circle(x="x", y="y", source=data_source, color=colors, size=point_size) plot.grid.visible = False plot.axis.visible = False # bpl.show(plot) else: if hover_data is not None: warn("Too many points for hover data -- tooltips will not" "be displayed. Sorry; try subssampling your data.") hv.extension("bokeh") hv.output(size=300) hv.opts( 'RGB [bgcolor="{}", xaxis=None, yaxis=None]'.format(background)) if labels is not None: point_plot = hv.Points(data, kdims=["x", "y"], vdims=["color"]) plot = hd.datashade( point_plot, aggregator=ds.count_cat("color"), cmap=plt.get_cmap(cmap), width=width, height=height, ) elif values is not None: min_val = data.values.min() val_range = data.values.max() - min_val data["val_cat"] = pd.Categorical( (data.values - min_val) // (val_range // 256)) point_plot = hv.Points(data, kdims=["x", "y"], vdims=["val_cat"]) plot = hd.datashade( point_plot, aggregator=ds.count_cat("val_cat"), cmap=plt.get_cmap(cmap), width=width, height=height, ) else: point_plot = hv.Points(data, kdims=["x", "y"]) plot = hd.datashade( point_plot, aggregator=ds.count(), cmap=plt.get_cmap(cmap), width=width, height=height, ) return plot
def fullSizeGraph(request): import pandas as pd import networkx import matplotlib.pyplot as plt import numpy as np df_enron = pd.read_csv(filterDataByTime(request.FILES['csv_data'])) #from bokeh.io import output_notebook, show, save from bokeh.models import Range1d, Circle, ColumnDataSource, MultiLine from bokeh.plotting import figure from bokeh.models.graphs import from_networkx from bokeh.palettes import Category10 from bokeh.transform import linear_cmap from bokeh.embed import json_item #output_notebook() #remove this when not using notebook G = networkx.from_pandas_edgelist(df_enron, 'fromId', 'toId', edge_attr=True) di = { 'CEO': 1, 'Director': 2, 'Employee': 3, 'In House Lawyer': 4, 'Manager': 5, 'Managing Director': 6, 'President': 7, 'Trader': 8, 'Unknown': 9, 'Vice President': 10 } df_rejob = df_enron.replace({"fromJobtitle": di}) df_attributes = df_enron[['fromId', 'fromJobtitle']].drop_duplicates() df_attributes.columns = ['fromId', 'job'] df_attributesx = df_rejob[['fromId', 'fromJobtitle']].drop_duplicates() job = df_attributes.set_index('fromId').to_dict('i') jobx = df_attributesx.set_index('fromId').to_dict('i') networkx.set_node_attributes(G, job) networkx.set_node_attributes(G, jobx) #jobs = ['Employee','Vice President','Unknown','Manager','CEO','Trader','Director','President','Managing Director','In House Lawyer'] degrees = dict(networkx.degree(G)) networkx.set_node_attributes(G, name='degree', values=degrees) adjusted_node_size = dict([(node, (degree + 5) - ((degree + 5) * 0.3)) for node, degree in networkx.degree(G)]) networkx.set_node_attributes(G, name='adjusted_node_size', values=adjusted_node_size) size_by_this_attribute = 'adjusted_node_size' color_by_this_attribute = 'fromJobtitle' color_palette = Category10[10] TOOLTIPS = [ ("Person ID", "@index"), ("people communicated with", "@degree"), ("Jobtitle", "@job"), ] plot = figure(tooltips=TOOLTIPS, tools="pan,zoom_in,wheel_zoom,save,reset,box_select,undo", active_scroll='wheel_zoom', x_range=Range1d(-20, 20), y_range=Range1d(-20, 20), title='Enron Emails', plot_width=950, plot_height=950) plot.axis.visible = False N_graph = from_networkx(G, networkx.spring_layout, scale=100) N_graph.node_renderer.glyph = Circle(size=size_by_this_attribute, fill_color=linear_cmap( color_by_this_attribute, color_palette, 1, 10)) N_graph.edge_renderer.glyph = MultiLine(line_alpha=10, line_width=1) plot.renderers.append(N_graph) item_text = json.dumps(json_item(plot)) return django.http.JsonResponse(item_text, safe=False)
df = pd.read_csv('citi-bike-history-data.csv', usecols=[ "tripduration", "starttime", "start station name", "start station latitude", "start station longitude" ]) df.columns = ["Tripduration", "Station", "Starttime", "Latitude", "Longitude"] df['Starttime'] = pd.to_datetime(df['Starttime']) # Plot trip duration minutes by start time grouped_data_by_start_time = df.groupby( df['Starttime'].dt.date)['Tripduration'].sum().to_frame().reset_index() mapper_top = linear_cmap( field_name='top', palette=Spectral6, low=min(grouped_data_by_start_time['Tripduration'] % 60), high=max(grouped_data_by_start_time['Tripduration'] % 60)) plot_by_starttime = figure(title="Trip duration minutes in December 2019", plot_width=800, plot_height=620, x_axis_type='datetime') start_time = pd.to_datetime(grouped_data_by_start_time['Starttime']) plot_by_starttime.vbar(x=pd.to_datetime( grouped_data_by_start_time['Starttime']), top=grouped_data_by_start_time['Tripduration'] % 60, width=0.9, line_color=mapper_top,
# state. # Reference: merged_sm_geo = GDF for singlemotherhood. lons, lats = gpd_bokeh(merged_sm_geo) # Converts GPD to bokeh format sm_source = ColumnDataSource(data=dict( #Dictionary maps coordiantes, needed attributes into CDS x=lons, y=lats, sm_percentage = merged_sm_geo['sm_percentage'], # attribute data we're interested in NAME = merged_sm_geo['NAME'])) # attribute data we're interested in # Set up color scheme of the choropleth map using a linear_cmap #credit: color brewer for color scheme assistance. color_map = linear_cmap (field_name= 'sm_percentage', palette= ('#feebe2','#fbb4b9','#f768a1','#c51b8a','#7a0177'), low = min(merged_sm_geo['sm_percentage']), high = max(merged_sm_geo ['sm_percentage'])) # Set tools within the map TOOLS = "pan,wheel_zoom,reset,hover,save" # Set up components of the map # FIGURE - Sets up map canvas, size, tools and title: map = figure(plot_width = 800, plot_height = 600, title ="Single Motherhood Rates by State, 2019", tools = TOOLS,) # PATCHES = Create the individual county patches based on provided geometry data. map.patches('x', 'y', source = sm_source, line_color = "gray", line_width=1, color = color_map)
def __init__(self, parent, dataset, parameters): self.parent = parent self.dataset = dataset # Set up the controls self.tools = Selector( name="Tools", kind="tools", css_classes=["tools"], entries={ "None": tools.BaseTool, "Show Light Curve": tools.ShowLightCurve, }, default="None", ) self.data = Selector( name="Datasets", kind="datasets", css_classes=["data"], entries={ "Test data": "test", # "TOI Catalog": "toi", # "Confirmed Planets": "confirmed", }, default="Test data", ) self.xaxis = Selector( name="Build-Your-Own", kind="parameters", css_classes=["build-your-own"], entries=parameters, default="ra", title="X Axis", ) self.yaxis = Selector( kind="parameters", css_classes=["build-your-own"], entries=parameters, default="dec", title="Y Axis", ) self.size = Selector( name="Sides", kind="parameters", css_classes=["sides"], entries=parameters, default="dist", title="Marker Size", none_allowed=True, ) self.color = Selector( kind="parameters", css_classes=["sides"], entries=parameters, default="dist", title="Marker Color", none_allowed=True, ) # Set up the plot self.source = ColumnDataSource( data=dict(x=[], y=[], size=[], color=[])) self.plot = figure( plot_height=600, plot_width=700, title="", tooltips=[("TIC ID", "@ticid")], sizing_mode="scale_both", ) self.plot.circle( x="x", y="y", source=self.source, size="size", color=linear_cmap(field_name="color", palette=Viridis256, low=0, high=1), line_color=None, ) self.plot.add_tools( BoxSelectTool(), BoxZoomTool(), LassoSelectTool(), PanTool(), PolySelectTool(), TapTool(), WheelZoomTool(), WheelPanTool(), ZoomInTool(), ZoomOutTool(), HoverTool(), CrosshairTool(), ResetTool(), ) # Register the callbacks for control in [self.xaxis, self.yaxis, self.size, self.color]: control.widget.on_change("value", self.param_callback) self.tools.widget.on_change("value", self.tool_callback) self.data.widget.on_change("value", self.data_callback) # Load and display the data self.param_callback(None, None, None)
# %% # DATA # bokeh.plotting.output_notebook() bokeh.plotting.output_file(FILE_OUT) cart_init_val = CART_SLIDER_INIT data = rec_df_spl.copy() data['x'] = data['LON'] * (1 - cart_init_val) + data['X'] * cart_init_val data['y'] = data['LAT'] * (1 - cart_init_val) + data['Y'] * cart_init_val # %% # COLOR from bokeh.transform import linear_cmap from bokeh.transform import log_cmap cm = linear_cmap('d_mas_cc', palette=PALETTE, low=C_BAR_LOW, high=C_BAR_HIGH) # cm = log_cmap('DEN', palette=bokeh.palettes.Viridis11, low=1, high=10000) # %% # SOURCES data['mas'] = data['MAS']/data['VV'] * 100 data['cc'] = data['CC']/data['VV'] * 100 data['ad_mas_cc'] = data['d_mas_cc'].abs() data['mas_o_cc'] = 'n' data.loc[data['d_mas_cc']>=0,'mas_o_cc'] = 'MAS' data.loc[data['d_mas_cc']<0,'mas_o_cc'] = 'CC' # %% source_master = ColumnDataSource(data) source_red_map = ColumnDataSource({'gx': [], 'gy': []})
df['mercator'] = mercators # using series we are splitting tuple into 2 separete column df[['mercator_x', 'mercator_y']] = df['mercator'].apply(pd.Series) # helps to select map type, STAMEN_TONER gives black and white map chosentile = get_provider(Vendors.STAMEN_TONER) # pallet color ie color bar graph palette = Turbo256 # source store the data as source of data source = ColumnDataSource(data=df) # bar graph for min to max cases color_mapper = linear_cmap(field_name='totalInfected', palette=palette, low=df['totalInfected'].min(), high=df['totalInfected'].max()) # tool tips which displays below values when we click on it tooltips = [('Region', '@region'), ('Total Infected', '@totalInfected'), ('New Infected', '@newInfected'), ('Total Recovered', '@recovered'), ('New Recovered', '@newRecovered'), ('Total Deceased', '@deceased'), ('New Deceased', '@newDeceased')] # figure contains details for map sizes plot = figure(plot_width=850, plot_height=850, x_axis_type='mercator', y_axis_type='mercator',
def _construct_shadowprice_visuals(interconnect, lmp_split_points, bus_segments, branch_data): """Use bokeh to plot variation in lmp and shadow prices :param str interconnect: scenario interconnect :param list/tuple/set/numpy.array lmp_split_points: lmp values chosen to split the bus data :param list bus_segments: bus data split into 9 segments :param pandas.DataFrame branch_data: branch data :return: (*bokeh.models.layout.Row*) -- bokeh map visual in row layout. """ tools = "pan,wheel_zoom,reset,hover,save" p = figure( title=f"{interconnect} Interconnect", tools=tools, x_axis_location=None, y_axis_location=None, plot_width=800, plot_height=800, ) # Add USA map p.add_tile(get_provider(Vendors.CARTODBPOSITRON)) # Add colored circles for bus locations indices = list(range(len(bus_segments))) indices.reverse() # We want the lowest prices on top for i in indices: bus_cds = ColumnDataSource({ "x": bus_segments[i]["x"], "y": bus_segments[i]["y"], "lmp": bus_segments[i]["lmp"], }) p.circle("x", "y", color=shadow_price_pallette[i], alpha=0.4, size=11, source=bus_cds) # Add branches branch_cds = ColumnDataSource({ "xs": branch_data[["from_x", "to_x"]].values.tolist(), "ys": branch_data[["from_y", "to_y"]].values.tolist(), "medianval": branch_data.medianval, }) # branch outline p.multi_line("xs", "ys", color="black", line_width=14, source=branch_cds) # branch color palette = shadow_price_pallette[-5:] mapper = linear_cmap(field_name="medianval", palette=palette, low=0, high=2000) p.multi_line("xs", "ys", color=mapper, line_width=9, source=branch_cds) # Add legends bus_legend = _construct_bus_legend(lmp_split_points) branch_legend = ColorBar( color_mapper=mapper["transform"], width=16, location=(0, 0), title="SP ($/MWh)", title_text_font_size="8pt", title_standoff=8, ) p.add_layout(branch_legend, "right") return row(bus_legend, p)
def __init__(self, dataset): self.dataset = dataset # Set up the controls self.specials = Selector( name="Specials", kind="specials", css_classes=["specials"], entries={ "Color-magnitude diagram": "cmd", "Period vs. radius": "pr", "Period vs. transit duration": "pdt", }, default="Color-magnitude diagram", ) self.data = Selector( name="Datasets", kind="datasets", css_classes=["data"], entries={"TOI Catalog": "toi", "Confirmed Planets": "confirmed"}, default="Confirmed Planets", ) self.xaxis = Selector( name="Build-Your-Own", kind="parameters", css_classes=["build-your-own"], entries=parameters, default="Right Ascension", title="X Axis", ) self.yaxis = Selector( name="Y Axis", kind="parameters", css_classes=["build-your-own"], entries=parameters, default="Declination", title="Y Axis", ) self.size = Selector( name="Marker Size", kind="parameters", css_classes=["sides"], entries=parameters, default="Distance", title="Marker Size", none_allowed=True, ) self.color = Selector( name="Marker Color", kind="parameters", css_classes=["sides"], entries=parameters, default="Distance", title="Marker Color", none_allowed=True, ) # Set up the plot self.source = ColumnDataSource( data=dict(x=[], y=[], size=[], color=[]) ) self.plot = figure( plot_height=600, plot_width=700, title="", tooltips=[("TIC ID", "@ticid")], sizing_mode="scale_both", ) self.plot.circle( x="x", y="y", source=self.source, size="size", color=linear_cmap( field_name="color", palette=Viridis256, low=0, high=1 ), line_color=None, ) self.plot.add_tools( BoxSelectTool(), BoxZoomTool(), LassoSelectTool(), PanTool(), PolySelectTool(), TapTool(), WheelZoomTool(), WheelPanTool(), ZoomInTool(), ZoomOutTool(), HoverTool(), CrosshairTool(), ) # Register the callback for control in [ self.specials, self.data, self.xaxis, self.yaxis, self.size, self.color, ]: control.widget.on_change("value", self.callback)
from bokeh.palettes import Oranges, OrRd, RdYlGn, Reds decided_palet = Reds[5] palet = decided_palet.copy() #palet.reverse() color_mapper = LinearColorMapper(palette=palet) #color_mapper.high=max(df['idle_hours']) #color_mapper.low=min(df['idle_hours']) th = list(set(df['idle_hours'])) #th=[str(x) for x in th] mapper = linear_cmap('idle_hours', palet, th[0], th[-1]) """ map_figure.circle(x='mrc_end_long_x', y='mrc_end_lat_x', #size=cluster_point_size, fill_alpha=0.4, source=datapoints_source,color="firebrick", line_alpha=0) """ def plot_points(map_figure, datapoints_source): noise_point_size = 1 cluster_point_size = 10 map_figure.circle( x='mrc_end_long_x',
def bokeh_cycling_stats(df, output_html_file): output_file(output_html_file, title = 'cycling stats ' + df.datetime[-1].strftime('%Y-%m-%d')) # calculate gradient for each ride df['grad'] = df['ascent [km]'] / df['distance [km]'] # do regression xreg = df['grad'].values yreg = df['avg speed [km/h]'].values isort = np.argsort(xreg) xreg = xreg[isort] yreg = yreg[isort] reg, low, up = linreg(xreg, yreg) # date for tooltips df['date'] = [x.date().strftime("%y-%m-%d") for x in df.datetime] #--- create weekly, monthly and yearly stats weekly_stats = df.resample('W', on = 'datetime').sum() monthly_stats = df.resample('M', on = 'datetime').sum() yearly_stats = df.resample('Y', on = 'datetime').sum() weekly_stats['cat'] = [(x - timedelta(days=x.weekday())).strftime('%y-%V') for x in weekly_stats.index] monthly_stats['cat'] = [x.strftime('%y-%m') for x in monthly_stats.index] yearly_stats['cat'] = [str(x.year) for x in yearly_stats.index] p11 = figure(title ="weekly distance [km]", x_range = weekly_stats['cat'], tooltips = [('week', "@{cat}"),('distance [km]', "@{distance [km]}")]) p11.vbar(x = 'cat', top = 'distance [km]', width = 0.7, source = weekly_stats, line_width = 0) p11.xaxis.major_label_orientation = np.pi/2 p12 = figure(title ="monthly distance [km]", x_range = monthly_stats['cat'], tooltips = [('month', "@{cat}"),('distance [km]', "@{distance [km]}")]) p12.vbar(x = 'cat', top = 'distance [km]', width = 0.7, source = monthly_stats, line_width = 0) p13 = figure(title ="yearly distance [km]", x_range = yearly_stats['cat'], tooltips = [('year', "@{cat}"),('distance [km]', "@{distance [km]}")]) p13.vbar(x = 'cat', top = 'distance [km]', width = 0.7, source = yearly_stats, line_width = 0) p21 = figure(title ="weekly ascent [km]", x_range = weekly_stats['cat'], tooltips = [('week', "@{cat}"),('ascent [km]', "@{ascent [km]}")]) p21.vbar(x = 'cat', top = 'ascent [km]', width = 0.7, source = weekly_stats, fill_color = 'darkorange', line_width = 0) p21.xaxis.major_label_orientation = np.pi/2 p22 = figure(title ="monthly ascent [km]", x_range = monthly_stats['cat'], tooltips = [('month', "@{cat}"),('ascent [km]', "@{ascent [km]}")]) p22.vbar(x = 'cat', top = 'ascent [km]', width = 0.7, source = monthly_stats, fill_color = 'darkorange', line_width = 0) p23 = figure(title ="yearly ascent [km]", x_range = yearly_stats['cat'], tooltips = [('year', "@{cat}"),('ascent [km]', "@{ascent [km]}")]) p23.vbar(x = 'cat', top = 'ascent [km]', width = 0.7, source = yearly_stats, fill_color = 'darkorange', line_width = 0) # plot histograms about rides dist_histo = np.histogram(df["distance [km]"], bins = np.arange(np.ceil(df['distance [km]'].max()/10) + 2) * 10 - 5) p31 = figure(title = 'ride distance [km] histogram') p31.quad(top = dist_histo[0], bottom = 0, left = dist_histo[1][:-1], right = dist_histo[1][1:], fill_color="darkseagreen", line_width = 0) mt_histo = np.histogram(df["moving time [min]"], bins = np.arange(np.ceil(df['moving time [min]'].max()/20) + 2) * 20 - 10) p32 = figure(title = 'ride moving time [min] histogram') p32.quad(top = mt_histo[0], bottom = 0, left = mt_histo[1][:-1], right = mt_histo[1][1:], fill_color="darkseagreen", line_width = 0) p33 = figure(title = 'ride avg speed [km/h] vs (ascent / distance)', tooltips = [('distance [km]', "@{distance [km]}"),('ascent [km]', "@{ascent [km]}"), ('moving time [min]', "@{moving time [min]}"), ('avg speed [km/h]',"@{avg speed [km/h]}"), ('date',"@{date}")]) #p33.line(xreg, reg) p33.line(xreg, low, color = 'gray') p33.line(xreg, up, color = 'gray') p33.scatter('grad', 'avg speed [km/h]', source = df, size = 8, color = linear_cmap(field_name = 'distance [km]', palette = Plasma11, low = df['distance [km]'].min(), high = 1.1*df['distance [km]'].max())) for fig in [p11, p21, p12, p22, p13, p23, p31, p32, p33]: fig.toolbar.active_drag = None fig.toolbar.active_scroll = None fig.toolbar.active_tap = None # group all figures in a grid grid = gridplot([[p11, p21], [p12, p22], [p13, p23], [p31, p32], [p33, None]], merge_tools = False, plot_width = 600, plot_height = 250, sizing_mode = 'scale_both') show(grid)
def bokeh_scatter_plot(self, tool1, tool2, **kwargs): """Return (and show) an interactive scatter plot comparing 2 tools rendered in bokeh library. Needs bokeh and colorcet libraries. Always return the `bokeh.plotting.Figure` instance with the plot. This can be used to further tune the plot. `tool1` (axis `x`) and `tool2` (axis `y`) `show` : Bool if `True` (default), show the plot in Jupyter notebook Possible kwargs =============== `show` : Bool, indicates, whether or not show the plot (in Jupyter) `col` : String name of ltlcross metric to plot, `states` by default `merge_same` : Bool if `True` (default), merge same instances and add colorbar for count, see `add_count` of `self.get_plot_data`. `include_equal` : Bool (default True) if `False` do not include formulas with the same values for both tools And we have 4 arguments that control the appearance of the plot `palette` : color palette to use if `merge_same` is `True` default : `bwy` from `colorcet` `marker_color` : color to use if `merge_same` is `False` default : "navy" `alpha` : alpha of marks default `1` if `merge_same` and `.3` otherwise `marker_size` : int default `10` All remaining kwargs are supplied to `bokeh.plotting.scatter` """ from bokeh.models import ColumnDataSource, CustomJS, ColorBar, TapTool, HoverTool, Slope from bokeh.transform import linear_cmap import bokeh.plotting as bplt # Get the arguments merge_same = kwargs.pop("merge_same", True) alpha = kwargs.pop("alpha", 1) if merge_same else kwargs.pop( "alpha", .3) marker_size = kwargs.pop("marker_size", 10) show = kwargs.pop("show", True) include_equal = kwargs.pop("include_equal", True) col = kwargs.pop("col", "states") # Import colorcet for palette if merge_same: import colorcet as cc palette = kwargs.pop("palette", cc.bgy) # Make the graph render in notebooks if show: bplt.output_notebook() # Create the basic plot object p = bplt.figure(title=f"Numbers of {col}") p.xaxis.axis_label = f"{tool1}" p.yaxis.axis_label = f"{tool2}" # Prepare the data data = self.get_plot_data(tool1, tool2, add_count=merge_same, include_equal=include_equal, col=col) if not merge_same: # We want to have the form_id and formula fields available for tooltip data = data.reset_index() source = ColumnDataSource(data) # Tooltips tooltips = [ (tool1, f"@{{{tool1}}}"), (tool2, f"@{{{tool2}}}"), ] if merge_same: # Map count of cases to color mapper = linear_cmap(palette=palette, field_name="count", low=1, high=data["count"].max()) color = mapper # Add count to tooltip tooltips.append(("count", "@count")) # Print command to display selected formulas callback = CustomJS(args=dict(source=source), code=f""" // Select the data var inds = source.selected.indices; var data = source.data; var x = data['{tool1}'][inds]; var y = data['{tool2}'][inds]; // Create the two commands var fst_row = "data = a.get_plot_data('{tool1}','{tool2}',add_count=False)"; var snd_row = "data[(data['{tool1}'] == " + x + ") & (data['{tool2}'] == " + y + ")]"; // Instructions var instructions = "Use the following code to list the formulas.\\n"; instructions += "Replace `a` with the ResAnalyzer` object:\\n\\n" alert(instructions + fst_row + "\\n" + snd_row); """) else: color = kwargs.pop("marker_color", "navy") tooltips.append(("formula id", "@form_id")) # Print formula on selection (currently only works for 1) callback = CustomJS(args=dict(source=source), code=f""" // Select the data var inds = source.selected.indices; var data = source.data; // Print formulas ids var output = data['form_id'][inds[0]]; for (var i = 1; i < inds.length; i++) {{ var f = data['form_id'][inds[i]]; output += ', ' + f; }} output += '\\n' // Print formulas (1 per line) for (var i = 0; i < inds.length; i++) {{ var f = data['formula'][inds[i]]; output += f + '\\n'; }} alert(output); """) # Plot data and add `y=x` slope = Slope(gradient=1, y_intercept=0, line_color="orange", line_width=2, line_dash="dashed") p.add_layout(slope) p.scatter(x=tool1, y=tool2, source=source, color=color, alpha=alpha, size=marker_size, **kwargs) # Add the hoover & selecting tool p.add_tools(TapTool(callback=callback)) p.add_tools(HoverTool(tooltips=tooltips, mode="mouse")) if merge_same: color_bar = ColorBar(color_mapper=mapper['transform'], width=16, location=(0, 0)) p.add_layout(color_bar, 'right') if show: bplt.show(p) return p
# DATA bokeh.plotting.output_notebook() cart_init_val = .0 data = rec_df_spl.copy() data['x'] = data['LON'] * (1 - cart_init_val) + data['X'] * cart_init_val data['y'] = data['LAT'] * (1 - cart_init_val) + data['Y'] * cart_init_val # %% # COLOR from bokeh.transform import linear_cmap from bokeh.transform import log_cmap # cm = linear_cmap('d_mas_cc', palette=ebu.P_DIF[::-1], low=-80, high=80) # cm = log_cmap('DEN', palette=bokeh.palettes.Viridis11, low=1, high=10000) cm = linear_cmap('DEN_CUT', palette=bokeh.palettes.Viridis[NL - 1], low=0, high=NL - 1) # %% # SOURCES source_master = ColumnDataSource(data) source_red_map = ColumnDataSource({'gx': [], 'gy': []}) la, lo = ebu.get_la_lo_bolivia() source_bol = ColumnDataSource({'la': la, 'lo': lo}) # source_red_car = ColumnDataSource({'lo': [], 'la': []}) # %% # JS CODE code_draw_red_map = """ const data = {'gx': [], 'gy': []} const indices = cb_data.index.indices
def load_page(plate): ''' Load new page ''' global well_id well_id = (0, 0) global sample sample = plate[well_id] # Button to upload local file global file_source file_source = ColumnDataSource(data=dict(file_contents = [], file_name = [])) file_source.on_change('data', file_callback) try: output_file_name = file_source.data['file_name'] + '-out.csv' except: output_filename = 'output.csv' global upload_button upload_button = Button(label="Upload local file", button_type="success", width=200, height=30) upload_button.js_on_click(CustomJS(args=dict(file_source=file_source), code=open(join(dirname(__file__), "upload.js")).read())) # Text boxes for setting fit parameters global bottom_set_text bottom_set_text = TextInput(value='', title="Set initial value for Fmin", width=200, height=50) bottom_set_text.on_change('value', parameter_set_callback) global top_set_text top_set_text = TextInput(value='', title="Set initial value for Fmax", width=200, height=50) top_set_text.on_change('value', parameter_set_callback) global slope_set_text slope_set_text = TextInput(value='', title="Set initial value for a", width=200, height=50) slope_set_text.on_change('value', parameter_set_callback) # Radio button group for setting plate type global plate_type_buttons global plate_type plate_type_buttons = RadioButtonGroup(labels=['96 well', '384 well'], width=200, height=25, active=plate_type) plate_type_buttons.on_change('active', plate_type_callback) # Radio button group for setting data layout global plate_layout_buttons global plate_layout plate_layout_buttons = RadioButtonGroup(labels=['by row', 'by column'], width=200, height=25, active=plate_layout) plate_layout_buttons.on_change('active', plate_layout_callback) # Checkbox groups for fixing fit parameters global fix_bottom_checkbox fix_bottom_checkbox = CheckboxButtonGroup(labels=['Fix min fluoresence (Fmin)'], width=200, height=30) fix_bottom_checkbox.on_change('active', parameter_set_callback) global fix_top_checkbox fix_top_checkbox = CheckboxButtonGroup(labels=['Fix max fluorescence (Fmax)'], width=200, height=30) fix_top_checkbox.on_change('active', parameter_set_callback) global fix_slope_checkbox fix_slope_checkbox = CheckboxButtonGroup(labels=['Fix curve shape parameter (a)'], width=200, height=30) fix_slope_checkbox.on_change('active', parameter_set_callback) # Slider for selecting data to fit global df xmin = df[df.columns[0]].values[0] xstep = df[df.columns[0]].values[1] - xmin xstart = sample.data['x'].values[0] xend = sample.data['x'].values[-1] xmax = df[df.columns[0]].values[-1] global range_slider range_slider = RangeSlider(start=xmin, end=xmax, value=(xstart, xend), step=xstep, title='Fine tune temperature range', width=550) range_slider.on_change('value', slider_callback) # Scatter plot for fitting individual samples global sample_source sample_source = ColumnDataSource(data=dict(x=sample.data.x, y=sample.data.y, fit=sample.y_fit, residuals=sample.residuals)) global sample_scatter plot_tools = 'wheel_zoom, pan, reset, save' sample_scatter = figure(title="Boltzman sigmoidal fit", x_axis_label='Temperature ('+degree_sign+'C)', y_axis_label="Fluoresence intensity", plot_width=600, plot_height=300, tools=plot_tools) sample_scatter.circle(x='x', y='y', color='grey', size=8, alpha=0.6, source=sample_source) sample_scatter.line(x='x', y='fit', color='black', line_width=2, alpha=1.0, source=sample_source) sample_scatter.title.text = sample.name + ' fit' # Scatter plot for residuals of individual sample fit global residual_scatter residual_scatter = figure(title="Fit residuals", x_axis_label='Temperature ('+degree_sign+'C)', y_axis_label="Residual", plot_width=600, plot_height=200, tools='wheel_zoom,pan,reset') residual_scatter.yaxis.formatter = BasicTickFormatter(precision=2, use_scientific=True) residual_scatter.circle('x', 'residuals', size=8, source=sample_source, color='grey', alpha=0.6) # Heatmap for displaying all Tm values in dataset global plate_source letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P'] w, n, t, e = [], [], [], [] if plate_type_buttons.active == 1: rows = 16 columns = 24 else: rows = 8 columns = 12 for i in range(rows): for j in range(columns): w.append(letters[i]+str(j+1)) try: n.append(plate[(i, j)].name) t.append(plate[(i, j)].v50_fit) e.append(plate[(i, j)].v50_err) except: n.append('') t.append(np.nan) e.append(np.nan) xname = [x[1:] for x in w] yname = [y[0] for y in w] plate_source = ColumnDataSource(dict(w=w, n=n, t=t, e=e, xname=xname, yname=yname)) plate_columns = [ TableColumn(field='w', title='Well ID'), TableColumn(field='n', title='Sample name'), TableColumn(field='t', title='Tm ('+degree_sign+'C)'), TableColumn(field='e', title='Error ('+degree_sign+'C)'), ] plate_map_hover = HoverTool(tooltips=""" <div> <div> <span style="font-size: 14px; font-weight: bold; ">@n:</span> <span style="font-size: 14px; font-weight: bold; ">@t</span> </div> </div> """ ) if plate_type_buttons.active == 1: plate_map = figure(title="Plate view", x_axis_location="above", height=400, width=620, tools=["save, tap, reset", plate_map_hover], x_range=[str(x+1) for x in range(0, columns)]+['', 'Tm ('+degree_sign+'C)'], y_range=letters[:rows][::-1]) else: plate_map = figure(title="Plate view", x_axis_location="above", height=400, width=620, tools=["save, tap, reset", plate_map_hover], x_range=[str(x+1) for x in range(0, columns)]+['Tm ('+degree_sign+'C)'], y_range=letters[:rows][::-1]) taptool = plate_map.select(type=TapTool) plate_map.on_event(Tap, plate_select) global mapper mapper = linear_cmap(field_name='t', palette=RdBu[8], low=min(t), high=max(t)) global color_bar color_bar = ColorBar(color_mapper=mapper['transform'], width=10, height=250, name='Tm ('+degree_sign+'C)') plate_map.add_layout(color_bar, 'right') plate_map.grid.grid_line_color = None plate_map.axis.axis_line_color = None plate_map.axis.major_tick_line_color = None plate_map.axis.major_label_text_font_size = "10pt" plate_map.axis.major_label_standoff = 0 plate_map.rect('xname', 'yname', .95, .95, source=plate_source, color=mapper, line_color='black', line_width=1) # Table listing all Tm values in dataset global plate_table plate_table = DataTable(source=plate_source, columns=plate_columns, width=500, height=500, selectable=True, editable=True) plate_table.source.selected.on_change('indices', table_select) # Table showing fitting parameters for current sample global sample_table_source sample_table_source = ColumnDataSource(data=dict(l=['Fit value', 'Std. error'], b=[sample.bottom_fit, sample.bottom_err], t=[sample.top_fit, sample.top_err], v=[sample.v50_fit, sample.v50_err], s=[sample.slope_fit, sample.slope_err])) sample_table_columns = [ TableColumn(field='l', title=''), TableColumn(field='b', title='Fmin'), TableColumn(field='t', title='Fmax'), TableColumn(field='v', title='Tm ('+degree_sign+'C)'), TableColumn(field='s', title='a') ] global sample_table sample_table = DataTable(source=sample_table_source, columns=sample_table_columns, width=600, height=200, selectable=False, editable=False) # Button to re-fit all with current parameter settings global refit_all_button refit_all_button = Button(label="Re-fit all samples", button_type='danger', width=200, height=30) refit_all_button.on_click(refit_all_callback) # Button to download Tm table to csv file global download_button download_button = Button(label="Download table to CSV", button_type="primary", width=200, height=30) download_button.js_on_click(CustomJS(args=dict(source=plate_source, file_name=output_filename), code=open(join(dirname(__file__), "download.js")).read())) # Button to copy Tm table to clipboard global copy_button copy_button = Button(label="Copy table to clipboard", button_type="primary", width=200, height=30) copy_button.js_on_click(CustomJS(args=dict(source=plate_source), code=open(join(dirname(__file__), "copy.js")).read())) # page formatting desc = Div(text=open(join(dirname(__file__), "description.html")).read(), width=1200) main_row = row(column(plate_type_buttons, plate_layout_buttons, upload_button, fix_bottom_checkbox, bottom_set_text, fix_top_checkbox, top_set_text, fix_slope_checkbox, slope_set_text, refit_all_button, download_button, copy_button), column(sample_scatter, residual_scatter, range_slider, sample_table), column(plate_map, plate_table)) sizing_mode = 'scale_width' l = layout([ [desc], [main_row] ], sizing_mode=sizing_mode) update() curdoc().clear() curdoc().add_root(l) curdoc().title = "DSF"
def plot_carto_single(self, data, frente, palette, path=FILE_OUT, name_file="", low=0, high=100, show_plot=True): """ :param data: df loaded by data_load :param frente: string, name of "partido" lowercase: diff, mas, cc, creemos, fpv, pan_bol :param palette: ej: P_GRAD_CC :param name_file: default:test :param low: cmap low limit: default: -80 :param high: cmap high limit: defauilt: +80. :return: df """ if frente == "diff": low = self.C_BAR_LOW high = self.C_BAR_HIGH frente = "d_mas_cc" bokeh.plotting.output_file( path + 'z037_' + frente + '_' + name_file + '.html') bokeh.plotting.output_file( os.path.join( os.path.dirname(ebu.DIR), 'docs', 'graficas_htmls', 'z037_' + frente + '_' + 'latest' + '.html' )) cart_init_val = self.CART_SLIDER_INIT # add slider data['x'] = data['LON'] * (1 - cart_init_val) + data[ 'X'] * cart_init_val data['y'] = data['LAT'] * (1 - cart_init_val) + data[ 'Y'] * cart_init_val cm = linear_cmap(frente, palette=palette, low=low, high=high) data['mas'] = data['MAS'] / data['VV'] * 100 data['cc'] = data['CC'] / data['VV'] * 100 data['creemos'] = data['CREEMOS'] / data['VV'] * 100 data['fpv'] = data['FPV'] / data['VV'] * 100 data['pan_bol'] = data['PAN_BOL'] / data['VV'] * 100 data['ad_mas_cc'] = data['d_mas_cc'].abs() data['mas_o_cc'] = 'n' data.loc[data['d_mas_cc'] >= 0, 'mas_o_cc'] = 'MAS' data.loc[data['d_mas_cc'] < 0, 'mas_o_cc'] = 'CC' source_master = ColumnDataSource(data) source_red_map = ColumnDataSource({'gx': [], 'gy': []}) la, lo = ebu.get_la_lo_bolivia() source_bol = ColumnDataSource({'la': la, 'lo': lo}) # source_red_car = ColumnDataSource({'lo': [], 'la': []}) # JS CODE code_draw_red_map = """ const data = {'gx': [], 'gy': []} const indices = cb_data.index.indices for (var i = 0; i < indices.length; i++ ) { data['gx'].push(source_master.data.GX[indices[i]]) data['gy'].push(source_master.data.GY[indices[i]]) } source_red_map.data = data """ code_draw_red_car = """ const data = {'lo': [], 'la': []} const indices = cb_data.index.indices for (var i = 0; i < indices.length; i++) { data['lo'].push(source_master.data.x[indices[i]]) data['la'].push(source_master.data.y[indices[i]]) } source_red_car.data = data """ code_merged = """ const data_map = {'lo': [], 'la': []} const data_car = {'gx': [], 'gy': []} const indices = cb_data.index.indices for (var i = 0; i < indices.length; i++) { data_map['lo'].push(source_master.data.x[indices[i]]) data_map['la'].push(source_master.data.y[indices[i]]) data_car['gx'].push(source_master.data.GX[indices[i]]) data_car['gy'].push(source_master.data.GY[indices[i]]) } source_red_car.data = data_car source_red_map.data = data_map """ code_slider = """ var data = source.data; var f = cb_obj.value var x = data['x'] var y = data['y'] var Y = data['Y'] var X = data['X'] var lat = data['LAT'] var lon = data['LON'] for (var i = 0; i < x.length; i++) { y[i] = (1-f)*lat[i] + f*Y[i] x[i] = (1-f)*lon[i] + f*X[i] } source.change.emit(); """ # FIGURES curr_time = ebu.get_bolivian_time(-3) # from datetime import datetime # curr_time = datetime.utcnow() pw = self.FIG_WIDTH cart_fig = Figure(plot_width=pw, plot_height=pw, output_backend="webgl", ) map_fig = Figure(plot_width=pw, plot_height=pw, x_axis_type='mercator', y_axis_type='mercator', output_backend="webgl", title="Última actualización: " + curr_time[ "datetime_val"].strftime( "%Y-%m-%d %H:%M") + "BOT", ) cart_fig.background_fill_color = "grey" cart_fig.background_fill_alpha = .5 # cb_fig = bokeh.plotting.Figure(plot_height=pw,plot_width=) # cb_fig.toolbar.logo = None # cb_fig.toolbar_location = None # SCATTER # noinspection PyUnresolvedReferences # add tiles tile_provider = bokeh.tile_providers.get_provider( bokeh.tile_providers.Vendors.CARTODBPOSITRON) map_fig.add_tile(tile_provider) # scatter in map map_fig.scatter( 'GX', 'GY', source=source_master, size='r2', color=cm ) # todo if we wont use map then we nee to delete the source # cart_fig.line('lo', 'la', source=source_bol, color='black') cart_fig.scatter('x', 'y', source=source_master, radius='r', color=cm ) red_scat_map = map_fig.circle_cross('gx', 'gy', source=source_red_map, # color='red', fill_color=None, # line_color='green', size=20, line_color="white", line_width=4 ) red_scat_map = map_fig.circle_cross('gx', 'gy', source=source_red_map, # color='red', fill_color=None, # line_color='green', size=20, line_color="red", line_width=1 ) # red_scat_car = cart_fig.scatter('lo', 'la', # source=source_red_car, color='green') # add a hover tool that sets the link data for a hovered circle # callbacks callback_red_map = CustomJS( args={'source_master': source_master, 'source_red_map': source_red_map, # 'source_red_car':source_red_car }, code=code_draw_red_map) # code = code_merged) # callback_red_car = CustomJS( # args={'source_master': source_master, 'source_red_car': source_red_car}, # code=code_draw_red_car) # tools hover_cart = bokeh.models.HoverTool( tooltips=self.TOOL_TIP_DIC[frente], callback=callback_red_map, # renderers = [red_scat_car] ) cart_fig.add_tools(hover_cart, ) hover_map = bokeh.models.HoverTool( tooltips=self.TOOL_TIP_DIC[frente], # callback=callback_red_car, # renderers = [red_scat_map] ) map_fig.add_tools(hover_map, ) # slider callback_slider = CustomJS(args=dict(source=source_master), code=code_slider) slider = Slider(start=0, end=1, value=cart_init_val, step=.02, title="carto") slider.js_on_change('value', callback_slider) # COLOR BAR ml = {int(i): str(np.abs(i)) for i in np.arange(-80, 81, 20)} cb = bokeh.models.ColorBar( color_mapper=cm['transform'], width=int(.9 * self.FIG_WIDTH), location=(0, 0), # title="DEN (N/km^2)", # title=(BAR_TITLE), # margin=0,padding=0, title_standoff=10, # ticker=bokeh.models.LogTicker(), orientation='horizontal', major_label_overrides=ml ) cart_fig.add_layout(cb, 'above') # cb.title_text_align = 'left' cart_fig.title.text = self.BAR_TITLE_DIC[frente] cart_fig.title.align = 'center' # layout = row(column(slider, cart_f),map_f) layout = bokeh.layouts.gridplot( [[slider], [cart_fig], [map_fig]], sizing_mode='scale_width', merge_tools=False) layout.max_width = 800 # layout = bokeh.layouts.column([slider, cart_fig]) cart_fig.x_range.start = self.CXS cart_fig.x_range.end = self.CXE cart_fig.y_range.start = self.CYS cart_fig.y_range.end = self.CYE _ll = ebu.lola_to_cart(lo=[self.MXS, self.MXE], la=[self.MYS, self.MYE]) map_fig.x_range.start = _ll[0][0] map_fig.x_range.end = _ll[0][1] map_fig.y_range.start = _ll[1][0] map_fig.y_range.end = _ll[1][1] cart_fig.xaxis.major_tick_line_color = None # turn off x-axis major ticks cart_fig.xaxis.minor_tick_line_color = None # turn off x-axis minor ticks cart_fig.yaxis.major_tick_line_color = None # turn off y-axis major ticks cart_fig.yaxis.minor_tick_line_color = None cart_fig.xaxis.major_label_text_font_size = '0pt' # turn off x-axis tick labels cart_fig.yaxis.major_label_text_font_size = '0pt' # turn off y-axis tick labels if show_plot: bokeh.plotting.show(layout) return data
merged['dnc'] = merged.iloc[:, -2] # Build a GeoJSONDataSource from merged geosource = GeoJSONDataSource(geojson=merged.to_json()) # Task 2: Data Visualization # T2.1 Create linear color mappers for 2 attributes in demo_raw: population density, hospital beds per capita # Map their maximum values to the high, and mimimum to the low labels = ['Density', 'BedsPerCapita'] palette = bp.inferno(256)[::-1] mappers = {} mappers['Density'] = linear_cmap(field_name='Density', palette=palette, low=min(demo_raw.Density), high=max(demo_raw.Density)) mappers['BedsPerCapita'] = linear_cmap(field_name='BedsPerCapita', palette=palette, low=demo_raw.BedsPerCapita.min(), high=demo_raw.BedsPerCapita.max()) # T2.2 Draw a Switzerland Map on canton level # Define a figure p1 = figure(title='Demographics in Switzerland', plot_height=500, plot_width=900, toolbar_location='above', tools="pan, wheel_zoom, box_zoom, reset")
def mapGenerator(path, mapSelect, htmlString, title, scaleMin, scaleMax, metric): ''' HELPER FUNCTION mapGenerator() Generate a map from the processed level_1_dataframe and diplay as a bokeh map of the earth for each weather station @param path - String, of the current working directory @param mapSelect - String, string to select what map to generate 'Annual Global Horizontal Irradiance (GJ/m^-2)', 'Annual Direct Normal Irradiance (GJ/m^-2)', 'Annual Diffuse Horizontal Irradiance (GJ/m^-2)', 'Annual POA Global Irradiance (GJ/m^-2)', 'Annual POA Direct Irradiance (GJ/m^-2)', 'Annual POA Diffuse Irradiance (GJ/m^-2)', 'Annual POA Sky Diffuse Irradiance (GJ/m^-2)', 'Annual POA Ground Diffuse Irradiance (GJ/m^-2)', 'Annual Global UV Dose (MJ/y^-1)', 'Annual UV Dose at Latitude Tilt (MJ/y^-1)', 'Annual Minimum Ambient Temperature (C)', 'Annual Average Ambient Temperature (C)', 'Annual Maximum Ambient Temperature (C)', 'Annual Range Ambient Temperature (C)', 'Average of Yearly Water Vapor Pressure(kPa)', 'Sum of Yearly Water Vapor Pressure(kPa)', "Annual number of Hours Relative Humidity > to 85%", 'Sum of Yearly Dew(mmd-1)' @param htmlString - String, what to name the html @param title - String, title of the map @param scaleMin - Float, minimum value of the scale @param scaleMax - Float, maximum value of the scale @param metric - String, metric of the value being measured @return -void, Bokeh map as a html ''' colorSelector = "Viridis256" #Create the html to be exported output_file(htmlString + '.html') # Create the tools used for zooming and hovering on the map tools = "pan,wheel_zoom,box_zoom,reset,previewsave" #Access the .json file to create the map of countries and states # The json files will create layers to overlap the data with with open(path + "/Map/countries.geojson", "r") as f: countries = bkm.GeoJSONDataSource(geojson=f.read()) with open(path + "/Map/us-states.json", "r") as f: states = bkm.GeoJSONDataSource(geojson=f.read()) #Access the processed summary data pickle level_1_df = pd.read_pickle( path + "\\Pandas_Pickle_DataFrames\\Pickle_Level1_Summary\\Pickle_Level1_Summary.pickle" ) #Radius is the size of the circle to be displayed on the map radiusList = [] for i in range(0, len(level_1_df)): #Toggle size of circle radiusList.append(2) radius = radiusList selector = level_1_df[mapSelect] station = level_1_df['Station name'] latitude = level_1_df['Site latitude'] longitude = level_1_df['Site longitude'] moduleTemp = level_1_df[mapSelect] uniqueID = level_1_df['Site Identifier Code'] dataSource = level_1_df['Data Source'] elevation = level_1_df['Site elevation (meters)'].astype(float) # The Boken map rendering package needs to store data in the ColumnDataFormat # Store the lat/lon from the Map_pickle. Formatting for Lat/Lon has been # processed prior see "Map_Pickle_Processing.py" file for more details # Add other data to create hover labels source = ColumnDataSource(data=dict(Lat=latitude, Lon=longitude, radius=radius, selector=selector, Station=station, Latitude=latitude, Longitude=longitude, Module_Temp=moduleTemp, uniqueID=uniqueID, elevation=elevation, dataSource=dataSource)) p = bkp.figure(width=1500, height=900, tools=tools, title=title, x_axis_type="mercator", y_axis_type="mercator", x_axis_label='Longitude', y_axis_label='Latitude') p.x_range = bkm.Range1d(start=-180, end=180) p.y_range = bkm.Range1d(start=-90, end=90) #Create the datapoints as overlapping circles p.circle( "Lon", "Lat", source=source, radius="radius", #fill color will use linear_cmap() to scale the colors of the circles being displayed fill_color=linear_cmap('selector', colorSelector, low=scaleMin, high=scaleMax), line_color=None, # Alpha is the transparency of the circle alpha=0.3) #Stations will be the black dots displayed on the map stations = p.circle( "Lon", "Lat", source=source, radius=.1, #fill color will use linear_cmap() to scale the colors of the circles being displayed fill_color='black', line_color=None, # Alpha is the transparency of the circle alpha=.99) #Create the scale bar to the right of the map # Create color mapper to make the scale bar on the right of the map # palette = color scheme of the mapo # low/high sets the scale of the data, use the minimum value and maximum value of the data we are analyzing color_mapper = LinearColorMapper(palette=colorSelector, low=scaleMin, high=scaleMax) # color bar will be scale bar set to the right of the map color_bar = ColorBar(color_mapper=color_mapper, ticker=LogTicker(), label_standoff=12, border_line_color=None, location=(0, 0)) # Assign the scale bar to " p " and put it to the right p.add_layout(color_bar, 'right') # These are the labels that are displayed when you hover over a spot on the map #( label , @data), data needs to be inside the ColumnDataSource() TOOLTIPS = [("Station", "@Station"), ("Site ID", "@uniqueID"), ("Data Source", "@dataSource"), ("Lat", "@Latitude"), ("Lon", "@Longitude"), (htmlString, "@selector" + " " + metric), ("Elevation", "@elevation" + " (m)")] #Create a hover tool that will rinder only the weather stations i.e stations are small black circles hover_labels = bkm.HoverTool(renderers=[stations], tooltips=TOOLTIPS) #Add the hover tool to the map p.add_tools(hover_labels) #Overlay the Country and States boarders p.patches("xs", "ys", color="white", line_color="black", source=countries, fill_alpha=0, line_alpha=1) p.patches("xs", "ys", color="white", line_color="black", source=states, fill_alpha=0, line_alpha=1) #Display the plot show(p)
def hexbin_viz( df: pd.DataFrame, x: str, y: str, plot_width: int, plot_height: int, tile_size: Optional[float] = None, ) -> Panel: """ Render a hexbin plot """ # pylint: disable=too-many-arguments,too-many-locals xmin, xmax = df[x].min(), df[x].max() ymin, ymax = df[y].min(), df[y].max() if tile_size is None: tile_size = (xmax - xmin) / 25 title = f"{y} by {x}" aspect_scale = (ymax - ymin) / (xmax - xmin) bins = hexbin( x=df[x], y=df[y], size=tile_size, orientation="flattop", aspect_scale=aspect_scale, ) fig = figure( title=title, tools=[], match_aspect=False, background_fill_color="#f5f5f5", toolbar_location=None, plot_width=plot_width, plot_height=plot_height, ) palette = list(reversed(viridis(256))) rend = fig.hex_tile( q="q", r="r", size=tile_size, line_color=None, source=bins, orientation="flattop", fill_color=linear_cmap( field_name="counts", palette=palette, low=min(bins.counts), high=max(bins.counts), ), aspect_scale=aspect_scale, ) fig.add_tools( HoverTool( tooltips=[("Count", "@counts")], renderers=[rend], )) mapper = LinearColorMapper(palette=palette, low=min(bins.counts), high=max(bins.counts)) color_bar = ColorBar(color_mapper=mapper, width=8, location=(0, 0)) color_bar.label_standoff = 8 fig.add_layout(color_bar, "right") tweak_figure(fig, "hex") _format_axis(fig, xmin, xmax, "x") _format_axis(fig, ymin, ymax, "y") fig.xaxis.axis_label = x fig.yaxis.axis_label = y return Panel(child=fig, title="hexbin plot")
def _plot_bokeh(self, harmonics=[1], **kwargs): wd = self.wd num_frequencies = wd.shape[1] log_dec = self.log_dec whirl = self.whirl_values speed_range = np.repeat( self.speed_range[:, np.newaxis], num_frequencies, axis=1 ) log_dec_map = log_dec.flatten() default_values = dict( cmap="viridis", vmin=min(log_dec_map), vmax=max(log_dec_map), s=30, alpha=1.0, ) for k, v in default_values.items(): kwargs.setdefault(k, v) camp = figure( tools="pan, box_zoom, wheel_zoom, reset, save", title="Campbell Diagram - Damped Natural Frequency Map", width=1600, height=900, x_axis_label="Rotor speed (rad/s)", y_axis_label="Damped natural frequencies (rad/s)", ) camp.xaxis.axis_label_text_font_size = "14pt" camp.yaxis.axis_label_text_font_size = "14pt" color_mapper = linear_cmap( field_name="color", palette=bp.viridis(256), low=min(log_dec_map), high=max(log_dec_map), ) for mark, whirl_dir, legend in zip( ["^", "o", "v"], [0.0, 0.5, 1.0], ["Foward", "Mixed", "Backward"] ): num_frequencies = wd.shape[1] for i in range(num_frequencies): w_i = wd[:, i] whirl_i = whirl[:, i] log_dec_i = log_dec[:, i] speed_range_i = speed_range[:, i] for harm in harmonics: camp.line( x=speed_range[:, 0], y=harm * speed_range[:, 0], line_width=3, color=bokeh_colors[0], line_dash="dotdash", line_alpha=0.75, legend="Rotor speed", muted_color=bokeh_colors[0], muted_alpha=0.2, ) idx = np.argwhere(np.diff(np.sign(w_i - harm*speed_range_i))).flatten() if len(idx) != 0: idx = idx[0] interpolated = interpolate.interp1d( x=[speed_range_i[idx], speed_range_i[idx+1]], y=[w_i[idx], w_i[idx+1]], kind="linear" ) xnew = np.linspace( speed_range_i[idx], speed_range_i[idx+1], num=30, endpoint=True, ) ynew = interpolated(xnew) idx = np.argwhere(np.diff(np.sign(ynew - harm*xnew))).flatten() source = ColumnDataSource( dict(xnew=xnew[idx], ynew=ynew[idx]) ) camp.square( "xnew", "ynew", source=source, size=10, color=bokeh_colors[9], name="critspeed" ) hover = HoverTool(names=["critspeed"]) hover.tooltips = [ ("Frequency :", "@xnew"), ("Critical Speed :", "@ynew") ] hover.mode = "mouse" whirl_mask = whirl_i == whirl_dir if whirl_mask.shape[0] == 0: continue else: source = ColumnDataSource( dict( x=speed_range_i[whirl_mask], y=w_i[whirl_mask], color=log_dec_i[whirl_mask], ) ) camp.scatter( x="x", y="y", color=color_mapper, marker=mark, fill_alpha=1.0, size=9, muted_color=color_mapper, muted_alpha=0.2, source=source, legend=legend, ) color_bar = ColorBar( color_mapper=color_mapper["transform"], width=8, location=(0, 0), title="log dec", title_text_font_style="bold italic", title_text_align="center", major_label_text_align="left", ) camp.add_tools(hover) camp.legend.background_fill_alpha = 0.1 camp.legend.click_policy = "mute" camp.legend.location = "top_left" camp.add_layout(color_bar, "right") return camp
def generate_graph_internal_link_interactive(website, maximum): domain = urllib.parse.urlparse(website).netloc urls = add_edge({}, website, domain, maximum) # Generating graph and dict of degrees g = nx.Graph(urls) d = dict(g.degree) # Adding table table = dict(url=[k for k, v in d.items()], count=[v for k, v in d.items()]) source = ColumnDataSource(table) columns = [ TableColumn(field="url", title="URL"), TableColumn(field="count", title="Count"), ] data_table = DataTable(source=source, columns=columns, width=400, height_policy="max") # Generating node size and color maxi = 1 if len(d.values()) > 0: maxi = max(d.values()) node_size = {k: max(5, math.ceil((v / maxi) * 30)) for k, v in d.items()} node_color = {k: v for k, v in d.items()} mapper = linear_cmap(field_name='node_color', palette=Spectral6, low=min(node_color.values()), high=max(node_color.values())) nx.set_node_attributes(g, d, 'connection') nx.set_node_attributes(g, node_size, "node_size") nx.set_node_attributes(g, node_color, "node_color") plot = figure(title="Maillage Interne " + domain, plot_width=1200, plot_height=800, x_range=Range1d(-1.1, 1.1), y_range=Range1d(-1.1, 1.1), sizing_mode='stretch_both') p = row([data_table, plot]) graph = from_networkx(g, nx.spring_layout, scale=2) node_hover_tool = HoverTool( tooltips=[("urls", "@index"), ("Connection", "@connection")]) plot.add_tools(node_hover_tool, BoxZoomTool(), ResetTool()) plot.toolbar.active_scroll = "auto" graph.node_renderer.hover_glyph = Circle(size=20, fill_color=Spectral4[1]) graph.edge_renderer.hover_glyph = MultiLine(line_color=Spectral8[6], line_width=1) graph.edge_renderer.glyph = MultiLine(line_alpha=0.8, line_width=0.03) graph.node_renderer.glyph = Circle(size='node_size', fill_color=mapper) graph.inspection_policy = NodesAndLinkedEdges() color_bar = ColorBar(color_mapper=mapper['transform'], width=8, location=(0, 0)) plot.add_layout(color_bar, 'right') plot.renderers.append(graph) return p, domain
) ld = ld_backup.copy() # convert price dtype ld['price'] = ld.price.str[1:].replace("$", '') ld['price'] = ld.price.str.replace(',', '') ld['price'] = ld.price.astype(float) # calculcate deal index ld['deal_index'] = ld.review_scores_rating / ld.price # instantiate mapper and source ld_nona = ld.dropna(subset=['deal_index']) psource = ColumnDataSource(ld_nona) mapper = linear_cmap(field_name='deal_index', palette=Viridis[256], low=ld.deal_index.min(), high=ld.deal_index.max()) # plot p = figure(title="A test", plot_width=1280, plot_height=1280, output_backend="webgl") c = p.circle('longitude', 'latitude', source=psource, color=mapper, fill_alpha=0.7, size=12) color_bar = ColorBar(color_mapper=mapper['transform'])
def get_map(zipcode, zdf): import matplotlib matplotlib.use('Agg') # load required Python libraries from uszipcode import SearchEngine from bokeh.io import export_png, output_file, show from bokeh.models import ColumnDataSource, GMapOptions, LinearColorMapper, ColorBar, NumeralTickFormatter from bokeh.models.glyphs import Patch from bokeh.plotting import gmap from bokeh.embed import components from bokeh.transform import linear_cmap from bokeh.palettes import inferno import numpy as np import pandas as pd import matplotlib.pyplot as plt from babel.numbers import format_currency #WRITE NOTIFICATION TO LOG print("Beginning Map...") # ns/dsi8 - July 2019 # Project 5 Team: N.Scott, J.Huessy, E.Stokes # Problem 4: Extracting Building Values from Zillow # this function accepts input arguments for zipcode and a dataframe (Zillow Zestimate data) # invoke SearchEngine for zipcode search search = SearchEngine(simple_zipcode=False) # pass a zipcode into search zipcode_data = search.by_zipcode(zipcode) # create a dictionary with retrieved details zip_dict = zipcode_data.to_dict() # get all longitudes, latitudes for specified zip zip_poly = zip_dict['polygon'] # isolate all latitudes for specified zip zip_poly_lat = [poly[1] for poly in zip_poly] # isolate all longitudes for specified zip zip_poly_lng = [poly[0] for poly in zip_poly] # define zipcode border (latitude & longitude) zip_dict_gmap_lat = zip_dict['lat'] zip_dict_gmap_lng = zip_dict['lng'] zip_dict_zipcode = zip_dict['zipcode'] zest_total = zdf['price'].sum() zest_dollar = format_currency(zest_total, 'USD', locale='en_US') # output filename includes zipcode #filename2 = f'''ZIP_{zip_dict_zipcode}_gmap.html''' # mapping options based on Bokeh documentation # https://bokeh.pydata.org/en/latest/docs/reference/models/map_plots.html#bokeh.models.map_plots.GMapOptions map_options = GMapOptions(lat=zip_dict_gmap_lat, lng=zip_dict_gmap_lng, map_type="roadmap", zoom=11) # For GMaps to function, Google requires you obtain and enable an API key: # https://developers.google.com/maps/documentation/javascript/get-api-key # Replace the value below with your personal API key: # p = gmap("GOOGLE_API_KEY", map_options, title="Austin") # map title includes zipcode and TOTAL Zillow "Zestimate" value for specified zipcode p = gmap("", map_options) #, title=f'''ZIPCODE: {zip_dict_zipcode} ----> Zestimate TOTAL: {zest_dollar}''') # based on code from Boston DSI cohort - Jan 2019 source = ColumnDataSource(data=dict(lat=zip_poly_lat, lon=zip_poly_lng)) patch = Patch(x='lon', y='lat', fill_color="blue", fill_alpha=0.08) p.add_glyph(source, patch) #ADDING SCATTERPLOT TO MAP #CREATE COLUMN DATA SOURCE FROM ZIPCODE DATAFRAME x = zdf['long'] y = zdf['lat'] z = zdf['price'] source = ColumnDataSource(dict(x=x, y=y, z=z)) #CREATE COLOR MAPPER FROM PRICE COLUMN mapper = linear_cmap(field_name='z', palette=inferno(256), low=min(z), high=max(z)) #ADD CIRCLES AT EACH LAT/LONG PAIR COLOR-CODED BY PRICE p.circle(x='x', y='y', line_color='black', color=mapper, fill_alpha=1, size=5, source=source) #CURRENCY FORMAT FOR TICKS formatter = NumeralTickFormatter(format='$0,0.00') #ADD COLORBAR LEGEND color_bar = ColorBar(color_mapper=mapper['transform'], width=8, formatter=formatter, location=(0, 0)) p.add_layout(color_bar, 'right') # CREATE HTML/JAVASCRIPT COMPONENTS OF BOKEH PLOT script, div = components(p) return script, div
#PC3=pca[:,2].tolist(),)) source_vln = ColumnDataSource(data=dict(xs=[], ys=[], xj=[], yj=[], color=[])) ## setup figures tools = 'reset,pan,wheel_zoom,box_select,save' # color_palette= godsnot_102 color_palette = cc.b_glasbey_bw_minc_20 ### catogories = sorted(anndat.obs[dd].unique().tolist()) palette = color_palette[:len(catogories)] low, high = 0, 1 ## transforms fcmap = factor_cmap('color', palette=palette, factors=[str(c) for c in catogories]) mapper = linear_cmap(field_name='umis', palette="Viridis256", low=low, high=high) color_bar = ColorBar(color_mapper=mapper['transform'], width=10, major_label_text_font_size="10pt", location=(0, 0)) #figures t1 = figure(plot_width=500, plot_height=500, title="t-SNE", tools=tools) t1.toolbar.logo = None t1.xaxis.axis_label = "tSNE1" t1.yaxis.axis_label = "tSNE2" # t2 = figure(plot_width=550, plot_height=500, tools=tools) t2.toolbar.logo = None
import numpy as np from bokeh.io import output_file, show from bokeh.plotting import figure from bokeh.transform import linear_cmap from bokeh.util.hex import hexbin n = 50000 x = np.random.standard_normal(n) y = np.random.standard_normal(n) bins = hexbin(x, y, 0.1) p = figure(title="Manual hex bin for 50000 points", tools="wheel_zoom,pan,reset", match_aspect=True, background_fill_color='#440154') p.grid.visible = False p.hex_tile(q="q", r="r", size=0.1, line_color=None, source=bins, fill_color=linear_cmap('counts', 'Viridis256', 0, max(bins.counts))) output_file("hex_tile.html") show(p)
def create(): det_data = [] fit_params = {} js_data = ColumnDataSource(data=dict(content=["", ""], fname=["", ""])) def proposal_textinput_callback(_attr, _old, new): proposal = new.strip() for zebra_proposals_path in pyzebra.ZEBRA_PROPOSALS_PATHS: proposal_path = os.path.join(zebra_proposals_path, proposal) if os.path.isdir(proposal_path): # found it break else: raise ValueError(f"Can not find data for proposal '{proposal}'.") file_list = [] for file in os.listdir(proposal_path): if file.endswith((".ccl", ".dat")): file_list.append((os.path.join(proposal_path, file), file)) file_select.options = file_list file_open_button.disabled = False file_append_button.disabled = False proposal_textinput = TextInput(title="Proposal number:", width=210) proposal_textinput.on_change("value", proposal_textinput_callback) def _init_datatable(): scan_list = [s["idx"] for s in det_data] file_list = [] for scan in det_data: file_list.append(os.path.basename(scan["original_filename"])) scan_table_source.data.update( file=file_list, scan=scan_list, param=[None] * len(scan_list), fit=[0] * len(scan_list), export=[True] * len(scan_list), ) scan_table_source.selected.indices = [] scan_table_source.selected.indices = [0] scan_motor_select.options = det_data[0]["scan_motors"] scan_motor_select.value = det_data[0]["scan_motor"] param_select.value = "user defined" file_select = MultiSelect(title="Available .ccl/.dat files:", width=210, height=250) def file_open_button_callback(): nonlocal det_data det_data = [] for f_name in file_select.value: with open(f_name) as file: base, ext = os.path.splitext(f_name) if det_data: append_data = pyzebra.parse_1D(file, ext) pyzebra.normalize_dataset(append_data, monitor_spinner.value) det_data.extend(append_data) else: det_data = pyzebra.parse_1D(file, ext) pyzebra.normalize_dataset(det_data, monitor_spinner.value) js_data.data.update( fname=[base + ".comm", base + ".incomm"]) _init_datatable() append_upload_button.disabled = False file_open_button = Button(label="Open New", width=100, disabled=True) file_open_button.on_click(file_open_button_callback) def file_append_button_callback(): for f_name in file_select.value: with open(f_name) as file: _, ext = os.path.splitext(f_name) append_data = pyzebra.parse_1D(file, ext) pyzebra.normalize_dataset(append_data, monitor_spinner.value) det_data.extend(append_data) _init_datatable() file_append_button = Button(label="Append", width=100, disabled=True) file_append_button.on_click(file_append_button_callback) def upload_button_callback(_attr, _old, new): nonlocal det_data det_data = [] for f_str, f_name in zip(new, upload_button.filename): with io.StringIO(base64.b64decode(f_str).decode()) as file: base, ext = os.path.splitext(f_name) if det_data: append_data = pyzebra.parse_1D(file, ext) pyzebra.normalize_dataset(append_data, monitor_spinner.value) det_data.extend(append_data) else: det_data = pyzebra.parse_1D(file, ext) pyzebra.normalize_dataset(det_data, monitor_spinner.value) js_data.data.update( fname=[base + ".comm", base + ".incomm"]) _init_datatable() append_upload_button.disabled = False upload_div = Div(text="or upload new .ccl/.dat files:", margin=(5, 5, 0, 5)) upload_button = FileInput(accept=".ccl,.dat", multiple=True, width=200) upload_button.on_change("value", upload_button_callback) def append_upload_button_callback(_attr, _old, new): for f_str, f_name in zip(new, append_upload_button.filename): with io.StringIO(base64.b64decode(f_str).decode()) as file: _, ext = os.path.splitext(f_name) append_data = pyzebra.parse_1D(file, ext) pyzebra.normalize_dataset(append_data, monitor_spinner.value) det_data.extend(append_data) _init_datatable() append_upload_div = Div(text="append extra files:", margin=(5, 5, 0, 5)) append_upload_button = FileInput(accept=".ccl,.dat", multiple=True, width=200, disabled=True) append_upload_button.on_change("value", append_upload_button_callback) def monitor_spinner_callback(_attr, _old, new): if det_data: pyzebra.normalize_dataset(det_data, new) _update_plot() monitor_spinner = Spinner(title="Monitor:", mode="int", value=100_000, low=1, width=145) monitor_spinner.on_change("value", monitor_spinner_callback) def scan_motor_select_callback(_attr, _old, new): if det_data: for scan in det_data: scan["scan_motor"] = new _update_plot() scan_motor_select = Select(title="Scan motor:", options=[], width=145) scan_motor_select.on_change("value", scan_motor_select_callback) def _update_table(): fit_ok = [(1 if "fit" in scan else 0) for scan in det_data] scan_table_source.data.update(fit=fit_ok) def _update_plot(): _update_single_scan_plot(_get_selected_scan()) _update_overview() def _update_single_scan_plot(scan): scan_motor = scan["scan_motor"] y = scan["counts"] x = scan[scan_motor] plot.axis[0].axis_label = scan_motor plot_scatter_source.data.update(x=x, y=y, y_upper=y + np.sqrt(y), y_lower=y - np.sqrt(y)) fit = scan.get("fit") if fit is not None: x_fit = np.linspace(x[0], x[-1], 100) plot_fit_source.data.update(x=x_fit, y=fit.eval(x=x_fit)) x_bkg = [] y_bkg = [] xs_peak = [] ys_peak = [] comps = fit.eval_components(x=x_fit) for i, model in enumerate(fit_params): if "linear" in model: x_bkg = x_fit y_bkg = comps[f"f{i}_"] elif any(val in model for val in ("gaussian", "voigt", "pvoigt")): xs_peak.append(x_fit) ys_peak.append(comps[f"f{i}_"]) plot_bkg_source.data.update(x=x_bkg, y=y_bkg) plot_peak_source.data.update(xs=xs_peak, ys=ys_peak) fit_output_textinput.value = fit.fit_report() else: plot_fit_source.data.update(x=[], y=[]) plot_bkg_source.data.update(x=[], y=[]) plot_peak_source.data.update(xs=[], ys=[]) fit_output_textinput.value = "" def _update_overview(): xs = [] ys = [] param = [] x = [] y = [] par = [] for s, p in enumerate(scan_table_source.data["param"]): if p is not None: scan = det_data[s] scan_motor = scan["scan_motor"] xs.append(scan[scan_motor]) x.extend(scan[scan_motor]) ys.append(scan["counts"]) y.extend([float(p)] * len(scan[scan_motor])) param.append(float(p)) par.extend(scan["counts"]) if det_data: scan_motor = det_data[0]["scan_motor"] ov_plot.axis[0].axis_label = scan_motor ov_param_plot.axis[0].axis_label = scan_motor ov_plot_mline_source.data.update(xs=xs, ys=ys, param=param, color=color_palette(len(xs))) if y: mapper["transform"].low = np.min([np.min(y) for y in ys]) mapper["transform"].high = np.max([np.max(y) for y in ys]) ov_param_plot_scatter_source.data.update(x=x, y=y, param=par) if y: interp_f = interpolate.interp2d(x, y, par) x1, x2 = min(x), max(x) y1, y2 = min(y), max(y) image = interp_f( np.linspace(x1, x2, ov_param_plot.inner_width // 10), np.linspace(y1, y2, ov_param_plot.inner_height // 10), assume_sorted=True, ) ov_param_plot_image_source.data.update(image=[image], x=[x1], y=[y1], dw=[x2 - x1], dh=[y2 - y1]) else: ov_param_plot_image_source.data.update(image=[], x=[], y=[], dw=[], dh=[]) def _update_param_plot(): x = [] y = [] fit_param = fit_param_select.value for s, p in zip(det_data, scan_table_source.data["param"]): if "fit" in s and fit_param: x.append(p) y.append(s["fit"].values[fit_param]) param_plot_scatter_source.data.update(x=x, y=y) # Main plot plot = Plot( x_range=DataRange1d(), y_range=DataRange1d(only_visible=True), plot_height=450, plot_width=700, ) plot.add_layout(LinearAxis(axis_label="Counts"), place="left") plot.add_layout(LinearAxis(axis_label="Scan motor"), place="below") plot.add_layout(Grid(dimension=0, ticker=BasicTicker())) plot.add_layout(Grid(dimension=1, ticker=BasicTicker())) plot_scatter_source = ColumnDataSource( dict(x=[0], y=[0], y_upper=[0], y_lower=[0])) plot_scatter = plot.add_glyph( plot_scatter_source, Scatter(x="x", y="y", line_color="steelblue")) plot.add_layout( Whisker(source=plot_scatter_source, base="x", upper="y_upper", lower="y_lower")) plot_fit_source = ColumnDataSource(dict(x=[0], y=[0])) plot_fit = plot.add_glyph(plot_fit_source, Line(x="x", y="y")) plot_bkg_source = ColumnDataSource(dict(x=[0], y=[0])) plot_bkg = plot.add_glyph( plot_bkg_source, Line(x="x", y="y", line_color="green", line_dash="dashed")) plot_peak_source = ColumnDataSource(dict(xs=[[0]], ys=[[0]])) plot_peak = plot.add_glyph( plot_peak_source, MultiLine(xs="xs", ys="ys", line_color="red", line_dash="dashed")) fit_from_span = Span(location=None, dimension="height", line_dash="dashed") plot.add_layout(fit_from_span) fit_to_span = Span(location=None, dimension="height", line_dash="dashed") plot.add_layout(fit_to_span) plot.add_layout( Legend( items=[ ("data", [plot_scatter]), ("best fit", [plot_fit]), ("peak", [plot_peak]), ("linear", [plot_bkg]), ], location="top_left", click_policy="hide", )) plot.add_tools(PanTool(), WheelZoomTool(), ResetTool()) plot.toolbar.logo = None # Overview multilines plot ov_plot = Plot(x_range=DataRange1d(), y_range=DataRange1d(), plot_height=450, plot_width=700) ov_plot.add_layout(LinearAxis(axis_label="Counts"), place="left") ov_plot.add_layout(LinearAxis(axis_label="Scan motor"), place="below") ov_plot.add_layout(Grid(dimension=0, ticker=BasicTicker())) ov_plot.add_layout(Grid(dimension=1, ticker=BasicTicker())) ov_plot_mline_source = ColumnDataSource( dict(xs=[], ys=[], param=[], color=[])) ov_plot.add_glyph(ov_plot_mline_source, MultiLine(xs="xs", ys="ys", line_color="color")) hover_tool = HoverTool(tooltips=[("param", "@param")]) ov_plot.add_tools(PanTool(), WheelZoomTool(), hover_tool, ResetTool()) ov_plot.add_tools(PanTool(), WheelZoomTool(), ResetTool()) ov_plot.toolbar.logo = None # Overview perams plot ov_param_plot = Plot(x_range=DataRange1d(), y_range=DataRange1d(), plot_height=450, plot_width=700) ov_param_plot.add_layout(LinearAxis(axis_label="Param"), place="left") ov_param_plot.add_layout(LinearAxis(axis_label="Scan motor"), place="below") ov_param_plot.add_layout(Grid(dimension=0, ticker=BasicTicker())) ov_param_plot.add_layout(Grid(dimension=1, ticker=BasicTicker())) ov_param_plot_image_source = ColumnDataSource( dict(image=[], x=[], y=[], dw=[], dh=[])) ov_param_plot.add_glyph( ov_param_plot_image_source, Image(image="image", x="x", y="y", dw="dw", dh="dh")) ov_param_plot_scatter_source = ColumnDataSource(dict(x=[], y=[], param=[])) mapper = linear_cmap(field_name="param", palette=Turbo256, low=0, high=50) ov_param_plot.add_glyph( ov_param_plot_scatter_source, Scatter(x="x", y="y", line_color=mapper, fill_color=mapper, size=10), ) ov_param_plot.add_tools(PanTool(), WheelZoomTool(), ResetTool()) ov_param_plot.toolbar.logo = None # Parameter plot param_plot = Plot(x_range=DataRange1d(), y_range=DataRange1d(), plot_height=400, plot_width=700) param_plot.add_layout(LinearAxis(axis_label="Fit parameter"), place="left") param_plot.add_layout(LinearAxis(axis_label="Parameter"), place="below") param_plot.add_layout(Grid(dimension=0, ticker=BasicTicker())) param_plot.add_layout(Grid(dimension=1, ticker=BasicTicker())) param_plot_scatter_source = ColumnDataSource(dict(x=[], y=[])) param_plot.add_glyph(param_plot_scatter_source, Scatter(x="x", y="y")) param_plot.add_tools(PanTool(), WheelZoomTool(), ResetTool()) param_plot.toolbar.logo = None def fit_param_select_callback(_attr, _old, _new): _update_param_plot() fit_param_select = Select(title="Fit parameter", options=[], width=145) fit_param_select.on_change("value", fit_param_select_callback) # Plot tabs plots = Tabs(tabs=[ Panel(child=plot, title="single scan"), Panel(child=ov_plot, title="overview"), Panel(child=ov_param_plot, title="overview map"), Panel(child=column(param_plot, row(fit_param_select)), title="parameter plot"), ]) # Scan select def scan_table_select_callback(_attr, old, new): if not new: # skip empty selections return # Avoid selection of multiple indicies (via Shift+Click or Ctrl+Click) if len(new) > 1: # drop selection to the previous one scan_table_source.selected.indices = old return if len(old) > 1: # skip unnecessary update caused by selection drop return _update_plot() def scan_table_source_callback(_attr, _old, _new): _update_preview() scan_table_source = ColumnDataSource( dict(file=[], scan=[], param=[], fit=[], export=[])) scan_table_source.on_change("data", scan_table_source_callback) scan_table = DataTable( source=scan_table_source, columns=[ TableColumn(field="file", title="file", width=150), TableColumn(field="scan", title="scan", width=50), TableColumn(field="param", title="param", editor=NumberEditor(), width=50), TableColumn(field="fit", title="Fit", width=50), TableColumn(field="export", title="Export", editor=CheckboxEditor(), width=50), ], width=410, # +60 because of the index column editable=True, autosize_mode="none", ) def scan_table_source_callback(_attr, _old, _new): if scan_table_source.selected.indices: _update_plot() scan_table_source.selected.on_change("indices", scan_table_select_callback) scan_table_source.on_change("data", scan_table_source_callback) def _get_selected_scan(): return det_data[scan_table_source.selected.indices[0]] def param_select_callback(_attr, _old, new): if new == "user defined": param = [None] * len(det_data) else: param = [scan[new] for scan in det_data] scan_table_source.data["param"] = param _update_param_plot() param_select = Select( title="Parameter:", options=["user defined", "temp", "mf", "h", "k", "l"], value="user defined", width=145, ) param_select.on_change("value", param_select_callback) def fit_from_spinner_callback(_attr, _old, new): fit_from_span.location = new fit_from_spinner = Spinner(title="Fit from:", width=145) fit_from_spinner.on_change("value", fit_from_spinner_callback) def fit_to_spinner_callback(_attr, _old, new): fit_to_span.location = new fit_to_spinner = Spinner(title="to:", width=145) fit_to_spinner.on_change("value", fit_to_spinner_callback) def fitparams_add_dropdown_callback(click): # bokeh requires (str, str) for MultiSelect options new_tag = f"{click.item}-{fitparams_select.tags[0]}" fitparams_select.options.append((new_tag, click.item)) fit_params[new_tag] = fitparams_factory(click.item) fitparams_select.tags[0] += 1 fitparams_add_dropdown = Dropdown( label="Add fit function", menu=[ ("Linear", "linear"), ("Gaussian", "gaussian"), ("Voigt", "voigt"), ("Pseudo Voigt", "pvoigt"), # ("Pseudo Voigt1", "pseudovoigt1"), ], width=145, ) fitparams_add_dropdown.on_click(fitparams_add_dropdown_callback) def fitparams_select_callback(_attr, old, new): # Avoid selection of multiple indicies (via Shift+Click or Ctrl+Click) if len(new) > 1: # drop selection to the previous one fitparams_select.value = old return if len(old) > 1: # skip unnecessary update caused by selection drop return if new: fitparams_table_source.data.update(fit_params[new[0]]) else: fitparams_table_source.data.update( dict(param=[], value=[], vary=[], min=[], max=[])) fitparams_select = MultiSelect(options=[], height=120, width=145) fitparams_select.tags = [0] fitparams_select.on_change("value", fitparams_select_callback) def fitparams_remove_button_callback(): if fitparams_select.value: sel_tag = fitparams_select.value[0] del fit_params[sel_tag] for elem in fitparams_select.options: if elem[0] == sel_tag: fitparams_select.options.remove(elem) break fitparams_select.value = [] fitparams_remove_button = Button(label="Remove fit function", width=145) fitparams_remove_button.on_click(fitparams_remove_button_callback) def fitparams_factory(function): if function == "linear": params = ["slope", "intercept"] elif function == "gaussian": params = ["amplitude", "center", "sigma"] elif function == "voigt": params = ["amplitude", "center", "sigma", "gamma"] elif function == "pvoigt": params = ["amplitude", "center", "sigma", "fraction"] elif function == "pseudovoigt1": params = ["amplitude", "center", "g_sigma", "l_sigma", "fraction"] else: raise ValueError("Unknown fit function") n = len(params) fitparams = dict( param=params, value=[None] * n, vary=[True] * n, min=[None] * n, max=[None] * n, ) if function == "linear": fitparams["value"] = [0, 1] fitparams["vary"] = [False, True] fitparams["min"] = [None, 0] elif function == "gaussian": fitparams["min"] = [0, None, None] return fitparams fitparams_table_source = ColumnDataSource( dict(param=[], value=[], vary=[], min=[], max=[])) fitparams_table = DataTable( source=fitparams_table_source, columns=[ TableColumn(field="param", title="Parameter"), TableColumn(field="value", title="Value", editor=NumberEditor()), TableColumn(field="vary", title="Vary", editor=CheckboxEditor()), TableColumn(field="min", title="Min", editor=NumberEditor()), TableColumn(field="max", title="Max", editor=NumberEditor()), ], height=200, width=350, index_position=None, editable=True, auto_edit=True, ) # start with `background` and `gauss` fit functions added fitparams_add_dropdown_callback(types.SimpleNamespace(item="linear")) fitparams_add_dropdown_callback(types.SimpleNamespace(item="gaussian")) fitparams_select.value = ["gaussian-1"] # add selection to gauss fit_output_textinput = TextAreaInput(title="Fit results:", width=750, height=200) def proc_all_button_callback(): for scan, export in zip(det_data, scan_table_source.data["export"]): if export: pyzebra.fit_scan(scan, fit_params, fit_from=fit_from_spinner.value, fit_to=fit_to_spinner.value) pyzebra.get_area( scan, area_method=AREA_METHODS[area_method_radiobutton.active], lorentz=lorentz_checkbox.active, ) _update_plot() _update_table() for scan in det_data: if "fit" in scan: options = list(scan["fit"].params.keys()) fit_param_select.options = options fit_param_select.value = options[0] break _update_param_plot() proc_all_button = Button(label="Process All", button_type="primary", width=145) proc_all_button.on_click(proc_all_button_callback) def proc_button_callback(): scan = _get_selected_scan() pyzebra.fit_scan(scan, fit_params, fit_from=fit_from_spinner.value, fit_to=fit_to_spinner.value) pyzebra.get_area( scan, area_method=AREA_METHODS[area_method_radiobutton.active], lorentz=lorentz_checkbox.active, ) _update_plot() _update_table() for scan in det_data: if "fit" in scan: options = list(scan["fit"].params.keys()) fit_param_select.options = options fit_param_select.value = options[0] break _update_param_plot() proc_button = Button(label="Process Current", width=145) proc_button.on_click(proc_button_callback) area_method_div = Div(text="Intensity:", margin=(5, 5, 0, 5)) area_method_radiobutton = RadioGroup(labels=["Function", "Area"], active=0, width=145) lorentz_checkbox = CheckboxGroup(labels=["Lorentz Correction"], width=145, margin=(13, 5, 5, 5)) export_preview_textinput = TextAreaInput(title="Export file preview:", width=450, height=400) def _update_preview(): with tempfile.TemporaryDirectory() as temp_dir: temp_file = temp_dir + "/temp" export_data = [] for s, export in zip(det_data, scan_table_source.data["export"]): if export: export_data.append(s) # pyzebra.export_1D(export_data, temp_file, "fullprof") exported_content = "" file_content = [] for ext in (".comm", ".incomm"): fname = temp_file + ext if os.path.isfile(fname): with open(fname) as f: content = f.read() exported_content += f"{ext} file:\n" + content else: content = "" file_content.append(content) js_data.data.update(content=file_content) export_preview_textinput.value = exported_content save_button = Button(label="Download File(s)", button_type="success", width=220) save_button.js_on_click( CustomJS(args={"js_data": js_data}, code=javaScript)) fitpeak_controls = row( column(fitparams_add_dropdown, fitparams_select, fitparams_remove_button), fitparams_table, Spacer(width=20), column(fit_from_spinner, lorentz_checkbox, area_method_div, area_method_radiobutton), column(fit_to_spinner, proc_button, proc_all_button), ) scan_layout = column(scan_table, row(monitor_spinner, scan_motor_select, param_select)) import_layout = column( proposal_textinput, file_select, row(file_open_button, file_append_button), upload_div, upload_button, append_upload_div, append_upload_button, ) export_layout = column(export_preview_textinput, row(save_button)) tab_layout = column( row(import_layout, scan_layout, plots, Spacer(width=30), export_layout), row(fitpeak_controls, fit_output_textinput), ) return Panel(child=tab_layout, title="param study")
def densidad_carto(width=500): bokeh.plotting.reset_output() WIDTH = width CB_VALS = [0, 1, 2, 3] CB_LIMS = ebu.DEN_LIMS CB_LABS = {s: str(l) for s, l in enumerate(CB_LIMS[:])} FILE_OUT = os.path.join(ebu.DIR, 'htlml_1_intermedios/2020/z040_densidad2020.html') # bokeh.plotting.output_file(FILE_OUT) df0 = pd.read_csv( os.path.join(ebu.DATA_PATH1_2020, 'z020_geopadron_recintos_2020_ALL_DEN.csv'), # encoding='ISO-8859-1' ).set_index('ID_RECI') df1 = pd.read_csv(os.path.join(ebu.DATA_PATH1_2020, 'z030_carto_xy.csv')).set_index('ID_RECI') rec_df = pd.merge(df0, df1, left_index=True, right_index=True, validate='1:1') # %% len(rec_df) # %% rec_df['r'] = np.sqrt(rec_df['HAB']) / 10 res = ebu.lola_to_cart(rec_df['LON'].values, rec_df['LAT'].values) rec_df['GX'] = res[0] rec_df['GY'] = res[1] needed_cols = [ 'X', 'Y', 'd_mas_cc', 'r', 'LAT', 'LON', 'PAIS', 'REC', 'MUN', 'DEN' 'GX', 'GY' ] # %% len(rec_df) # %% # order by density rec_df = rec_df.sort_values('DEN', axis=0, ascending=True) # %% # remove nans # rec_df = rec_df.dropna(axis=0) # assert rec_df.isna().sum().sum() == 0 # %% len(rec_df) # %% # cut = pd.IntervalIndex.from_tuples([(0, 50), (50, 500), (500, 1500),(1500,3000),(3000,4000),(4000,7000)]) # %% # lab = ['B','M','X','A'] lab = CB_VALS lims = CB_LIMS NL = len(lims) c = pd.cut( rec_df['DEN'], lims, labels=lab, # retbins=True ) # %% rec_df['DEN_CUT'] = c.astype(int) # %% # %% [markdown] # ## Carto Densidad # %% [markdown] # ###### código # %% # output_file(os.path.join(ebu.DATA_FIG_OUT, "carto_map_mas_cc.html")) # %% # rec_df_spl = rec_df.sample(200).copy() rec_df_spl = rec_df.copy() # %% # DATA bokeh.plotting.output_notebook() cart_init_val = .0 data = rec_df_spl.copy() data['x'] = data['LON'] * (1 - cart_init_val) + data['X'] * cart_init_val data['y'] = data['LAT'] * (1 - cart_init_val) + data['Y'] * cart_init_val # %% # COLOR from bokeh.transform import linear_cmap from bokeh.transform import log_cmap # cm = linear_cmap('d_mas_cc', palette=ebu.P_DIF[::-1], low=-80, high=80) # cm = log_cmap('DEN', palette=bokeh.palettes.Viridis11, low=1, high=10000) cm = linear_cmap('DEN_CUT', palette=bokeh.palettes.Viridis[NL - 1], low=0, high=NL - 1) # %% # SOURCES source_master = ColumnDataSource(data) source_red_map = ColumnDataSource({'gx': [], 'gy': []}) la, lo = ebu.get_la_lo_bolivia() source_bol = ColumnDataSource({'la': la, 'lo': lo}) # source_red_car = ColumnDataSource({'lo': [], 'la': []}) # %% # JS CODE code_draw_red_map = """ const data = {'gx': [], 'gy': []} const indices = cb_data.index.indices for (var i = 0; i < indices.length; i++) { data['gx'].push(source_master.data.GX[indices[i]]) data['gy'].push(source_master.data.GY[indices[i]]) } source_red_map.data = data """ code_draw_red_car = """ const data = {'lo': [], 'la': []} const indices = cb_data.index.indices for (var i = 0; i < indices.length; i++) { data['lo'].push(source_master.data.x[indices[i]]) data['la'].push(source_master.data.y[indices[i]]) } source_red_car.data = data """ code_merged = """ const data_map = {'lo': [], 'la': []} const data_car = {'gx': [], 'gy': []} const indices = cb_data.index.indices for (var i = 0; i < indices.length; i++) { data_map['lo'].push(source_master.data.x[indices[i]]) data_map['la'].push(source_master.data.y[indices[i]]) data_car['gx'].push(source_master.data.GX[indices[i]]) data_car['gy'].push(source_master.data.GY[indices[i]]) } source_red_car.data = data_car source_red_map.data = data_map """ code_slider = """ var data = source.data; var f = cb_obj.value var x = data['x'] var y = data['y'] var Y = data['Y'] var X = data['X'] var lat = data['LAT'] var lon = data['LON'] for (var i = 0; i < x.length; i++) { y[i] = (1-f)*lat[i] + f*Y[i] x[i] = (1-f)*lon[i] + f*X[i] } source.change.emit(); """ # %% # FIGURES pw = WIDTH cart_fig = Figure(plot_width=pw + int(.2 * pw), plot_height=pw, output_backend="webgl") # map_fig = Figure(plot_width=pw, plot_height=pw, # x_axis_type='mercator', # y_axis_type='mercator', # output_backend="webgl", # ) # cb_fig = bokeh.plotting.Figure(plot_height=pw,plot_width=) # cb_fig.toolbar.logo = None # cb_fig.toolbar_location = None # %% # SCATTER # noinspection PyUnresolvedReferences # add tiles tile_provider = bokeh.tile_providers.get_provider( bokeh.tile_providers.Vendors.CARTODBPOSITRON) # map_fig.add_tile(tile_provider) # scatter in map # map_fig.scatter( # 'GX', 'GY', source=source_master, size='r', # color=cm # ) # cart_fig.line('lo', 'la', source=source_bol, color='black') cart_fig.scatter('x', 'y', source=source_master, size='r', color=cm) # red_scat_map = map_fig.scatter('gx', 'gy', # source=source_red_map, color='red', # line_color='green', # size=10 # ) # red_scat_car = cart_fig.scatter('lo', 'la', # source=source_red_car, color='green') # add a hover tool that sets the link data for a hovered circle # callbacks callback_red_map = CustomJS( args={ 'source_master': source_master, 'source_red_map': source_red_map, # 'source_red_car':source_red_car }, code=code_draw_red_map) # code = code_merged) # callback_red_car = CustomJS( # args={'source_master': source_master, 'source_red_car': source_red_car}, # code=code_draw_red_car) # tools ebu.TOOL_TIPS1 = [('Inscritos', '@HAB'), ('País', '@PAIS'), ('Municipio', '@MUN'), ('Recinto', '@REC'), ('Votantes/km^2', '@DEN{0}'), ('--------', '------') # ('PAIS', '@PAIS'), ] hover_cart = bokeh.models.HoverTool( tooltips=ebu.TOOL_TIPS1, callback=callback_red_map, # renderers = [red_scat_car] ) cart_fig.add_tools(hover_cart, ) hover_map = bokeh.models.HoverTool( tooltips=ebu.TOOL_TIPS1, # callback=callback_red_car, # renderers = [red_scat_map] ) # map_fig.add_tools(hover_map, ) # slider callback_slider = CustomJS(args=dict(source=source_master), code=code_slider) slider = Slider(start=0, end=1, value=cart_init_val, step=.01, title="carto") slider.js_on_change('value', callback_slider) # %% # COLOR BAR cb = bokeh.models.ColorBar( color_mapper=cm['transform'], width=30, location=(0, 0), title="Den. (V./km^2)", # margin=0,padding=0, title_standoff=10, # ticker=bokeh.models.LogTicker(), major_label_overrides=CB_LABS, ticker=bokeh.models.FixedTicker(ticks=list(CB_LABS.keys()))) cart_fig.add_layout(cb, 'left') # layout = row(column(slider, cart_f),map_f) # layout = bokeh.layouts.gridplot( # [[slider, None], [cart_fig, map_fig]] # , merge_tools=False # ) layout = bokeh.layouts.column([slider, cart_fig], # sizing_mode='scale_width' ) layout.width = width cart_fig.x_range.start = -80 cart_fig.x_range.end = -45 cart_fig.y_range.start = -30 cart_fig.y_range.end = 0 _ll = ebu.lola_to_cart(lo=[-80, -45], la=[-30, 0]) # map_fig.x_range.start = _ll[0][0] # map_fig.x_range.end = _ll[0][1] # map_fig.y_range.start = _ll[1][0] # map_fig.y_range.end = _ll[1][1] # %% [markdown] # ###### gráfica # %% [markdown] # En el mapa de abajo, cada punto corresponde un recinto electoral, su color está relacionado con la densidad de votantes, y su tamaño con la cantidad de votos. # Mueve el slider (carto) para ver la deformación. # %% # %% bokeh.plotting.show(layout)
def plot(tables, output_filename, xmin, xmax, ymin, ymax, superterm): ''' This is the plot function that uses Bokeh functions and widgets to make an interactive hexagon plot. This function recieves: - tables: dictionary with tables used to create arrays of repeated x, y coordinates (depending on the counts) for the hexagon plot. - output_filename: filename of .html output in the plots folder The coordinate arrays are used to create a pandas dataframe with Bokeh functions. This dataframe contains the q, r coordinates and counts used to plot the hexagons. To this dataframe, extra information is added (e.g. most common chemicals), which is displayed in the hover tooltip. Gaussian blur is added to copies of this dataframe and given as input to the Bokeh slider widget. Other widgets are added as well, for saturation, normalization etc. Bokeh allows to customize these widges with javascript code. The hexagon plot is saved as a .html file and also shown in the browser. ''' file_name = 'plots/' + str(output_filename) + '.html' output_file(file_name) # Blur and saturation values BLUR_MAX = 4 BLUR_STEP_SIZE = 0.25 SATURATION_MAX = 5 SATURATION_STEP_SIZE = 0.25 # Hexagon plot properties SIZE_HEXAGONS = 10 orientation = 'flattop' #bokeh alows 2 different hexagon orientations which also influences hexagon size calculations, but we currently have only calculated blur distances for this orientation ratio = ((ymax - ymin) / (xmax - xmin)) size = SIZE_HEXAGONS / ratio hexagon_height = sqrt(3) * size hexagon_height = hexagon_height * ratio # make figure p = figure(x_range=[xmin, xmax], y_range=[ymin - (hexagon_height / 2), ymax], tools="wheel_zoom,reset,save", background_fill_color='#440154') p.grid.visible = False p.xaxis.axis_label = "log(P)" p.yaxis.axis_label = "mass in Da" p.xaxis.axis_label_text_font_style = 'normal' p.yaxis.axis_label_text_font_style = 'normal' # term_to_source, term_to_metadata, options = make_plot_sources(tables, size, ratio, orientation, BLUR_MAX, BLUR_STEP_SIZE) # source for widgets term_to_source = dict() term_to_class = dict() term_to_metadata = dict() options = [] for term in tables.keys(): options.append((term, term)) table = tables[term]['table'] if superterm: source = create_class_source(table, term, size, ratio, orientation, superterm) term_to_class[term] = {} term_to_class[term]['show_class'] = True term_to_class[term]['source'] = source else: term_to_class[term] = {'show_class': False} source, title = create_data_source(table, term, size, ratio, orientation, BLUR_MAX, BLUR_STEP_SIZE) metadata = return_html(tables[term]['metadata']) term_to_source[term] = {'source': source, 'title': title} term_to_metadata[term] = metadata # hex = p.hex_tile(q='q', r="r", size=size, line_color=None, source=source, aspect_scale=ratio,orientation=orientation, # fill_color='pink' ) # show(p) # make default souce for plot, this is the first source shown in the plot, and also works like a container. Old data is thrown out and new data is thrown in. default_term = list(tables.keys())[0] # pick the first one metadata = tables[default_term]['metadata'] metadata = return_html(metadata) table = tables[default_term]['table'] source, title = create_data_source(table, default_term, size, ratio, orientation, BLUR_MAX, BLUR_STEP_SIZE) p.title.text = title # color mapper mapper = linear_cmap('scaling', 'Viridis256', 0, max(source.data['scaling'])) # plot hex = p.hex_tile(q="q", r="r", size=size, line_color=None, source=source, aspect_scale=ratio, orientation=orientation, fill_color=mapper) if superterm: source_class = term_to_class[default_term]['source'] class_hex = p.hex_tile(q='q', r="r", size=size, line_color=None, source=source_class, aspect_scale=ratio, orientation=orientation, fill_color='pink', fill_alpha=0.7) class_hex.visible = False # HOVER TOOLTIPS = return_JS_code('tooltips') TOOLTIPS_tfidf = return_JS_code('tooltips_tfidf') code_callback_hover = return_JS_code('hover') callback_hover = CustomJS(code=code_callback_hover) hover = HoverTool(tooltips=TOOLTIPS, callback=callback_hover, show_arrow=False) p.add_tools(hover) # WIDGETS slider1 = Slider(start=1, end=SATURATION_MAX, value=1, step=SATURATION_STEP_SIZE, title="Saturation", width=100) slider2 = Slider(start=0, end=BLUR_MAX, value=0, step=BLUR_STEP_SIZE, title="Blur", width=100) checkbox = CheckboxGroup(labels=["TFIDF"], active=[]) radio_button_group = RadioGroup(labels=["Viridis256", "Greys256"], active=0) button = Button(label="Metadata", button_type="default", width=100) multi_select = MultiSelect(title=output_filename, value=[default_term], options=options, width=100, height=300) if superterm: label = "Show " + str(superterm) checkbox_class = CheckboxGroup(labels=[label], active=[]) # WIDGETS CODE FOR CALLBACK code_callback_slider1 = return_JS_code('slider1') code_callback_slider2 = return_JS_code('slider2') code_callback_checkbox = return_JS_code('checkbox') code_callback_rbg = return_JS_code('rbg') code_callback_button = return_JS_code('button') code_callback_ms = return_JS_code('multi_select') if superterm: code_callback_class = return_JS_code('class') # WIDGETS CALLBACK callback_slider1 = CustomJS(args={ 'source': source, 'mapper': mapper, 'slider2': slider2, 'checkbox': checkbox }, code=code_callback_slider1) callback_slider2 = CustomJS(args={ 'source': source, 'mapper': mapper, 'slider1': slider1, 'checkbox': checkbox }, code=code_callback_slider2) callback_checkbox = CustomJS(args={ 'source': source, 'slider1': slider1, 'slider2': slider2, 'mapper': mapper, 'hover': hover, 'tooltips': TOOLTIPS, 'tooltips_tfidf': TOOLTIPS_tfidf }, code=code_callback_checkbox) callback_radio_button_group = CustomJS(args={ 'p': p, 'multi_select': multi_select, 'mapper': mapper, 'term_to_class': term_to_class, 'Viridis256': Viridis256, 'Greys256': Greys256 }, code=code_callback_rbg) callback_button = CustomJS(args={ 'term_to_metadata': term_to_metadata, 'multi_select': multi_select }, code=code_callback_button) callback_ms = CustomJS(args={ 'source': source, 'term_to_source': term_to_source, 'term_to_class': term_to_class, 'checkbox': checkbox, 'slider2': slider2, 'slider1': slider1, 'p': p, 'mapper': mapper }, code=code_callback_ms) if superterm: callback_radio_button_group = CustomJS(args={ 'p': p, 'multi_select': multi_select, 'class_hex': class_hex, 'term_to_class': term_to_class, 'mapper': mapper, 'Viridis256': Viridis256, 'Greys256': Greys256 }, code=code_callback_rbg) callback_class = CustomJS(args={ 'multi_select': multi_select, 'term_to_class': term_to_class, 'class_hex': class_hex }, code=code_callback_class) callback_ms = CustomJS(args={ 'source': source, 'term_to_source': term_to_source, 'checkbox': checkbox, 'slider2': slider2, 'slider1': slider1, 'p': p, 'mapper': mapper, 'checkbox_class': checkbox_class, 'class_hex': class_hex, 'term_to_class': term_to_class }, code=code_callback_ms) # WIDGETS INTERACTION slider1.js_on_change('value', callback_slider1) slider2.js_on_change('value', callback_slider2) checkbox.js_on_change('active', callback_checkbox) radio_button_group.js_on_change('active', callback_radio_button_group) button.js_on_event(events.ButtonClick, callback_button) multi_select.js_on_change("value", callback_ms) if superterm: checkbox_class.js_on_change('active', callback_class) # LAYOUT if superterm: layout = row( multi_select, p, column(slider1, slider2, checkbox, checkbox_class, radio_button_group, button)) else: layout = row( multi_select, p, column(slider1, slider2, checkbox, radio_button_group, button)) show(layout)