""" #Plotting flower species #Importing libraries from bokeh.plotting import figure from bokeh.io import output_file, show from bokeh.sampledata.iris import flowers from bokeh.models import Range1d, PanTool, ResetTool, HoverTool, WheelZoomTool from screeninfo import get_monitors #Define the output file path output_file("iris.html") #Create the figure object f = figure(tools=[PanTool(), ResetTool(), WheelZoomTool()]) #Style the tools hover = HoverTool(tooltips=[("Species", "@species"), ("Sepal Width", "@sepal_width")]) f.add_tools(hover) f.toolbar_location = 'above' f.toolbar.logo = None #Style the plot area f.plot_width = get_monitors( )[0].width #get_monitors is part of the screeninfo module imported above f.plot_height = get_monitors( )[0].height - 50 #get_monitors is part of the screeninfo module imported above f.background_fill_color = "grey" f.background_fill_alpha = 0.1
f.circle(x="petal_length", y="petal_width", size=[i*4 for i in setosa.data["sepal_width"]], fill_alpha=0.2,color="color",line_dash=[5,3],legend='Versicolor',source=versicolor) f.circle(x="petal_length", y="petal_width", size=[i*4 for i in setosa.data["sepal_width"]], fill_alpha=0.2,color="color",line_dash=[5,3],legend='Virginica',source=virginica) labels = LabelSet(x='petal_length', y='petal_width', text='sepal_length',level='glyph', x_offset=5, y_offset=5, source=virginica) f.add_layout(labels) #Style the tools f.tools=[PanTool(),ResetTool()] hover=HoverTool(tooltips=""" <div> <div> <img src="@imgs" height="42" alt="@imgs" width="42" style="float: left; margin: 0px 15px 15px 0px;" border="2" ></img> </div> <div> <span style="font-size: 15px; font-weight: bold;">@species</span> </div> <div> <span style="font-size: 10px; color: #696;">Petal length: @petal_length</span><br> <span style="font-size: 10px; color: #696;">Petal width: @petal_width</span>
def make_candle_chart(stock_name=None): """ """ try: API_URL = 'https://api.iextrading.com/1.0' res = requests.get(f'{ API_URL }/stock/{ stock_name }/chart/5y') data = res.json() except JSONDecodeError: abort(404) df = pd.DataFrame(data) # df["date"] = pd.to_datetime(df["date"]) # import pdb; pdb.set_trace() seqs = np.arange(df.shape[0]) df['seqs'] = pd.Series(seqs) df['changePercent'] = df['changePercent'].apply(lambda x: str(x) + '%') df['mid'] = df.apply(lambda x: (x['open'] + x['close']) / 2, axis=1) df['height'] = df.apply(lambda x: x['close'] - x['open'] if x['close'] != x['open'] else 0.01, axis=1) inc = df.close > df.open dec = df.close < df.open w = .3 sourceInc = bk.ColumnDataSource(df.loc[inc]) sourceDec = bk.ColumnDataSource(df.loc[dec]) hover = HoverTool(tooltips=[ ('Date', '@date'), ('Low', '@low'), ('High', '@high'), ('Open', '@open'), ('Close', '@close'), ('Percent', '@changePercent'), ], names=["rect1", "rect2"]) TOOLS = [ hover, BoxZoomTool(), PanTool(), ZoomInTool(), ZoomOutTool(), ResetTool() ] p = figure(plot_width=1000, plot_height=800, title=stock_name, tools=TOOLS, toolbar_location='above') p.xaxis.major_label_orientation = np.pi / 4 p.grid.grid_line_alpha = w # descriptor = Label(x=70, y=70, text=f"5-year stock chart of {stock_name}") # p.add_layout(descriptor) p.segment(df.seqs[inc], df.high[inc], df.seqs[inc], df.low[inc], color='green') p.segment(df.seqs[dec], df.high[dec], df.seqs[dec], df.low[dec], color='red') p.rect(x='seqs', y='mid', width=w, height='height', fill_color='red', line_color='red', source=sourceDec, name="rect1") p.rect(x='seqs', y='mid', width=w, height='height', fill_color='green', line_color='green', source=sourceInc, name="rect2") script, div = components(p) return script, div, stock_name
def draw_bubble_map(request, start_date: str = None, end_date: str = None, pollutants: list = None): if request is not None: pollutants = request.GET.get('pollutants', ['PM25', 'PM10', 'NO2']) start_date = request.GET.get('start_date', None) end_date = request.GET.get('end_date', None) if start_date is None: return JsonResponse("'start_date' is a required parameter.", safe=False) if end_date is None: end_date = start_date bubble_data = get_target_bubblemap_data(start_date=start_date, end_date=end_date, pollutants=pollutants) # create bokeh elements _tabs = [] for k, v in bubble_data.items(): p = get_nuts_map(0, outline_map=True, include_tools=False, exclude_countries=['TR']) p.name = k # add annotation top = p.properties_with_values().get('plot_height') note1 = Label(x=10, y=50, x_units='screen', y_units='screen', text='NOTE: bubble size denotes Nuts2 Population.', render_mode='canvas', border_line_color=None, border_line_alpha=1.0, text_alpha=.5, text_font_size='12px', background_fill_color=None, background_fill_alpha=0.5) note2 = Label(x=10, y=30, x_units='screen', y_units='screen', text='NOTE: color denotes percentage of target.', render_mode='canvas', border_line_color=None, border_line_alpha=1.0, text_alpha=0.5, text_font_size='12px', background_fill_color=None, background_fill_alpha=0.5) p.add_layout(note1) p.add_layout(note2) _tabs.append(Panel(child=p, title=p.name)) tabs = Tabs(tabs=_tabs) tabs.sizing_mode = 'scale_both' color_mapper = LinearColorMapper(palette=RdYlGn11, low=.5, low_color='green', high=1.5, high_color='red') tick_format = NumeralTickFormatter(format='+0%') color_bar = ColorBar( color_mapper=color_mapper, ticker=FixedTicker(ticks=[0, .25, 0.50, .75, 1, 1.25, 1.50]), formatter=tick_format, label_standoff=9, border_line_color=None, location=(0, 0)) s_zoom = WheelZoomTool() s_pan = PanTool() s_reset = ResetTool() # create the bubbles and hover elements for t in tabs.tabs: # add colorbar t.child.add_layout(color_bar, 'right') # add bubbles glyphs = t.child.scatter(x='x', y='y', size='radius', source=bubble_data.get(t.child.name), fill_alpha=0.6, fill_color={ 'field': 'achievement', 'transform': color_mapper }, line_color=None) # add hover tool for stations hover_tool = HoverTool( renderers=[glyphs], tooltips=[("air_quality_station", "@air_quality_station"), ("Country", "@country_code_id"), ("NUTS 2", "@nuts_2_name"), ("NUTS 2 Pop", "@population"), (f"{t.child.name} Target Value", "@target_value"), ("Avg Value", "@value__avg"), ("% of Target", "@achievement{:+%0.0}")]) t.child.add_tools(hover_tool, s_zoom, s_pan, s_reset) # jupyter notebook # return tabs # django item = json_item(tabs) # item['metadata'] = 'somemetadata' return JsonResponse(item)
def multi_plot(nx, ny, x, ys, source, colors, alphas=None, x_range=None, y_label=None, rescale=False, plotters=None): if not ys or not present(source, x, *ys): return None tools = [ PanTool(dimensions='width'), ZoomInTool(dimensions='width'), ZoomOutTool(dimensions='width'), ResetTool(), HoverTool(tooltips=[tooltip(x) for x in ys + [LOCAL_TIME]], names=['with_hover']) ] f = figure(plot_width=nx, plot_height=ny, x_axis_type='datetime' if TIME in x else 'linear', tools=tools) if y_label: f.yaxis.axis_label = y_label elif rescale: f.yaxis.axis_label = ys[0] else: f.yaxis.axis_label = ', '.join(ys) if rescale: f.extra_y_ranges = {} if alphas is None: alphas = [1 for _ in ys] while len(plotters) < len(ys): plotters += plotters for y, color, alpha, plotter in zip(ys, colors, alphas, plotters): y_range = make_range(source, y) if rescale and y != ys[0]: f.extra_y_ranges[y] = y_range f.add_layout(LinearAxis(y_range_name=y, axis_label=y), 'right') plotter(f, x=x, y=y, source=source, color=color, alpha=alpha, y_range_name=y, name='with_hover') else: f.y_range = y_range plotter(f, x=x, y=y, source=source, color=color, alpha=alpha, name='with_hover') f.xaxis.axis_label = x f.toolbar.logo = None if ny < 300: f.toolbar_location = None if x_range: f.x_range = x_range return f
def makeplot(df, FC_P, PV_P, Inhibitor): df.loc[(df['Fold_change'] > FC_P) & (df['p_value'] < PV_P), 'color'] = "green" # upregulated #df.loc - Selects single row or subset of rows from the DataFrame by label df.loc[(df['Fold_change'] <= FC_P) & (df['p_value'] < PV_P), 'color'] = "red" # downregulated df['color'].fillna('grey', inplace=True) df["log_pvalue"] = -np.log10(df['p_value']) df["log_FC"] = np.log2(df['Fold_change']) df.head() from bokeh.plotting import figure, ColumnDataSource, output_notebook, show from bokeh.models import HoverTool, WheelZoomTool, PanTool, BoxZoomTool, ResetTool, TapTool, SaveTool from bokeh.palettes import brewer output_notebook() category = 'Substrate' category_items = df[category].unique() title = Inhibitor + " : Data Summary" #feeding data into ColumnDataSource source = ColumnDataSource(df) #Editing the hover that need to displayed while hovering hover = HoverTool( tooltips=[('Kinase', '@Kinase'), ('Substrate', '@Substrate'), ('Sub_gene', '@Sub_gene'), ('Phosphosite', '@Phosphosite'), ('Fold_change', '@Fold_change'), ('p_value', '@p_value')]) #tools that are need to explote data tools = [ hover, WheelZoomTool(), PanTool(), BoxZoomTool(), ResetTool(), SaveTool() ] #finally making figure with scatter plot p = figure( tools=tools, title=title, plot_width=700, plot_height=400, toolbar_location='right', toolbar_sticky=False, ) p.scatter(x='log_FC', y='log_pvalue', source=source, size=10, color='color') #displaying the graph return (p)
circ = Circle(x='xw', y='yw', size=10, line_alpha=0.0, fill_color='color', fill_alpha='rate', name='wells') #Draws the wells circ_r = p.add_glyph(source, circ) #p.line(xc, yc, line_color='black', line_width=1.0) #p.patches('x', 'y', source=source, # fill_color='color', fill_alpha=0.0, # line_color="black", line_width=0.5) hover = HoverTool() hover.renderers = [circ_r] p.add_tools(PanTool(), WheelZoomTool(), BoxSelectTool(), hover) #hover.point_policy = "follow_mouse" hover.tooltips = [ # ("Name", "@name"), ("(Well Level)", "@depth ft"), ("(Water Type)", "@color"), ("(Long, Lat)", "(@xw, @yw)"), ] output_file("california.html", title="california.py example") checkbox_button_group = CheckboxButtonGroup( #Checkbox group, currently doesn't work. Still figuring out how callbacks work on Bokeh. labels=["Groundwater", "Recycled"], active=[0, 1]) show(vform(checkbox_button_group, p))
def visualize(self): # Set up the output file output_file(self._outputFileName) ## BARPLOT ## per_day = self._stateCounts.transpose().values.tolist() data = dict({str(i): v for i, v in enumerate(per_day)}) data[ 'x'] = self._desiredStates_ns #add the statuses to the data source data['y'] = [0.0 for i in range(len(self._desiredStates_ns)) ] #dummy column for CustomJS to overwrite data['colorsOnly'] = self._colorsOnly source = ColumnDataSource(data) #plot setup barplot = figure(plot_width=800, plot_height=600, tools='pan', x_axis_label='Status', x_range=source.data['x'], y_range=ranges.Range1d( start=0, end=int(self._filteredNumHomes * 1.1)), title="Number of Homes by Status at Current Day") barplot.vbar(source=source, x='x', top='y', width=0.6, fill_color='colorsOnly', line_color=None) bar_hover = HoverTool(tooltips=[('num', '@y')]) barplot.yaxis.axis_label = "Number of Homes" barplot.add_tools(bar_hover) ## MAPS ## mapHoverInfo = self._mapHoverOptions options_html = "" for option in mapHoverInfo: options_html += "<span style=\"font-weight: bold;\">%s: </span><span>%s<br></span>" % ( str(option), "@" + str(option)) mapHoverInfo_html = "<div style=\"width: 450px\">" + options_html + "</div>" map_hover = HoverTool(tooltips=mapHoverInfo_html) #get average lat, long mean_lat = self._filteredData['latitude'].mean() mean_long = self._filteredData['longitude'].mean() #get the zip area name if self._desiredZipcode is None: areaData = self._zipSearch.by_coordinate(mean_lat, mean_long, returns=1)[0] areaName = "Greater " + areaData['City'] + " Area" else: areaData = self._zipSearch.by_zipcode(self._desiredZipcode) areaName = areaData['City'] + ", " + str(areaData['Zipcode']) map_options = GMapOptions(lat=mean_lat, lng=mean_long, map_type="roadmap") mapplot = GMapPlot(x_range=ranges.Range1d(), y_range=ranges.Range1d(), map_options=map_options) mapplot.title.text = areaName mapplot.add_tools(PanTool(), WheelZoomTool(), map_hover) #set Google Maps API key mapplot.api_key = "AIzaSyAr5Z6tbpyDQLPyD4PQmrxvqn6VEN_3vnU" #data wrangling for JS interaction home_data_for_map_list = [ self._allHomeStateColors.copy(), self._filteredData['latitude'], self._filteredData['longitude'] ] for option in self._mapHoverOptions: home_data_for_map_list += [self._filteredData[str(option)]] home_status_colors_formap = pd.concat(home_data_for_map_list, axis=1) initialDamageStateData = self._filteredData[ 'damage_state_start'].replace(self._damageStates) home_status_colors_formap = pd.concat( [home_status_colors_formap, initialDamageStateData], axis=1) home_status_colors_formap['y'] = np.nan #dummy column home_status_colors_formap.columns = home_status_colors_formap.columns.astype( str) mapsource = ColumnDataSource(home_status_colors_formap) circle = Circle(x="longitude", y="latitude", size='damage_state_start', fill_color="y", fill_alpha=0.8, line_color=None) mapplot.add_glyph(mapsource, circle) ## LINE GRAPH ## # LINE GRAPH - CURRENT TIME INDICATOR # # Generate a vertical bar to indicate current time within the line graph # Line is generated to 10% above the number of homes and 10% below zero currtime_list = { 'x': [0, 0], 'y': [ int(self._filteredNumHomes * 1.1), int(self._filteredNumHomes * -0.1) ] } #dummy column for js callback for i in range(0, self._simTime): currtime_list[str(i)] = [i, i] currtime_source = ColumnDataSource(currtime_list) # LINE GRAPH - DATA # line_plot = figure(title='Overall House Status vs Time', y_range=ranges.Range1d( start=int(self._filteredNumHomes * 0.1), end=int(self._filteredNumHomes * 1.5))) all_line_data = self._stateCounts.values.tolist() day_range = np.linspace(1, self._simTime - 2, num=self._simTime - 1).tolist() for data, name, color in zip(all_line_data, self._statuses, self._colorsOnly): line_data = pd.DataFrame(data).values.tolist() line_plot.line(day_range, line_data, color=color, alpha=0.8, legend=name, line_width=2) line_plot.line(x='x', y='y', source=currtime_source, line_color='red') line_plot.legend.location = "top_center" line_plot.legend.click_policy = "hide" line_plot.legend.orientation = "horizontal" line_plot.yaxis.axis_label = "Number of Homes" line_plot.xaxis.axis_label = "Day" # Requires Bokeh 0.12.7 # Javascript callback to enable and link interactivity between the two plots. callback = CustomJS(args=dict(s1=source, s2=mapsource, s3=currtime_source), code=""" console.log(' changed selected time', cb_obj.value); var data = s1.data; var data2 = s2.data; var data3 = s3.data; data['y'] = data[cb_obj.value]; data2['y'] = data2[cb_obj.value]; data3['x'] = data3[cb_obj.value]; s1.change.emit(); s2.change.emit(); s3.change.emit(); """) ## SLIDER ## # This slider manages one callback which updates all three graphics. time_slider = Slider(start=1, end=self._simTime - 1, value=0, step=1, callback=callback, title='DAY') show( gridplot([[mapplot], [line_plot, barplot], [time_slider]], sizing_mode='stretch_both'))
from bokeh.models import WheelZoomTool, PanTool, BoxZoomTool from bokeh.models import WMTSTileSource output_file("tile_source_example.html", title="Tile Source Example") # set to roughly full extent of web mercator projection x_range = Range1d(start=-20000000, end=20000000) y_range = Range1d(start=-20000000, end=20000000) # create tile source from templated url tile_options = {} tile_options['url'] = 'http://c.tile.openstreetmap.org/{z}/{x}/{y}.png' tile_source = WMTSTileSource(**tile_options) # instantiate plot and add tile source p = Plot(x_range=x_range, y_range=y_range, plot_height=800, plot_width=800) p.add_tools(WheelZoomTool(), PanTool(), BoxZoomTool(match_aspect=True)) tile_renderer_options = {} p.add_tile(tile_source, **tile_renderer_options) doc = Document() doc.add_root(p) if __name__ == "__main__": filename = "tile_source.html" with open(filename, "w") as f: f.write(file_html(doc, INLINE, "Tile Source Example")) print("Wrote %s" % filename) view(filename)
def plot(self, source1, source2, source3, source4, source5, source6, source7, source8, source9): hover = HoverTool(tooltips=[ ("Similarity", "($x)"), ("ECF", "($y)"), ]) p = figure(title="TOPOLOGICAL FP/Tanimoto Similarity", x_axis_label="Similarity", y_axis_label="Cumulative Distribution Function", x_range=(0, 1), y_range=(0, 1), tools=[hover], plot_width=1000, plot_height=800) FDA_plot = p.line(x="x", y="y", source=source1, line_width=3, color="darkslateblue") PPI_plot = p.line(x="x", y="y", source=source2, line_width=3, color="yellowgreen") MACRO_plot = p.line(x="x", y="y", source=source3, line_width=3, color="lightsteelblue") NP_plot = p.line(x="x", y="y", source=source4, line_width=3, color="olive") PEP_FDA_plot = p.line(x="x", y="y", source=source5, line_width=3, color="darkslategray") LIN_plot = p.line(x="x", y="y", source=source6, line_width=3, color="aquamarine") LIN_NM_plot = p.line(x="x", y="y", source=source7, line_width=3, color="teal") CYC_plot = p.line(x="x", y="y", source=source8, line_width=3, color="lightpink") CYC_NM_plot = p.line(x="x", y="y", source=source9, line_width=3, color="mediumvioletred") p.add_tools(LassoSelectTool(), ZoomInTool(), ZoomOutTool(), SaveTool(), PanTool()) legend = Legend(items=[ ("FDA", [FDA_plot]), ("PPI", [PPI_plot]), ("MACRO", [MACRO_plot]), ("NP", [NP_plot]), ("PEP FDA", [PEP_FDA_plot]), ("LIN", [LIN_plot]), ("LIN NM", [LIN_NM_plot]), ("CYC", [CYC_plot]), ("CYC NM", [CYC_NM_plot]), ], location="center", orientation="vertical", click_policy="hide") p.add_layout(legend, place='right') p.xaxis.axis_label_text_font_size = "20pt" p.yaxis.axis_label_text_font_size = "20pt" p.xaxis.axis_label_text_color = "black" p.yaxis.axis_label_text_color = "black" p.xaxis.major_label_text_font_size = "18pt" p.yaxis.major_label_text_font_size = "18pt" p.title.text_font_size = "22pt" return p
def plot_power_timeline(data, **kwargs): import itertools from datetime import timedelta, datetime, date from bokeh.core.enums import Dimensions, StepMode from bokeh.transform import dodge, cumsum from bokeh.plotting import figure from bokeh.models import ColumnDataSource, OpenURL, TapTool from bokeh.models import WheelZoomTool, ResetTool, BoxZoomTool, HoverTool, PanTool, SaveTool from bokeh.models import NumeralTickFormatter, PrintfTickFormatter, Circle from bokeh.models.ranges import Range1d from bokeh import palettes, layouts from bokeh.palettes import Magma256 from datetime import date x_range = kwargs.get('x_range', None) if not x_range: x_range = (date(2020, 2, 1) - timedelta(days=1), date.today() + timedelta(days=1)) y_range = 0, 60 * 24 # Minutes of a day data['color'] = (data.power * 255).astype(int) data['color'] = data.color.apply(lambda c: Magma256[c]) data_source = ColumnDataSource(data) fig = figure(plot_height=500, plot_width=1000, title=f"Power timeline", x_axis_type='datetime', y_axis_type='linear', x_range=x_range, y_range=y_range, tools="") fig.rect(y='minutes', x='date', width=timedelta(days=1)/2, height=1, color='orange', source=data_source) from datetime import time def index_to_time(index: int): hours = index // 60 minutes = index % 60 if index == 60 * 24: return time(0, 0) return time(hours, minutes) minutes = list(range(0, 60 * 24+1, 3 * 60)) fig.yaxis.ticker = minutes fig.yaxis.major_label_overrides = {m: str(index_to_time(m)) for m in minutes} fig.output_backend = "webgl" fig.toolbar.logo = None hover_tool = HoverTool(tooltips=[('Date', '@date{%F}'), ('power', '@power')], formatters={'date': 'datetime'}) fig.add_tools(hover_tool) fig.add_tools(SaveTool()) fig.add_tools(BoxZoomTool()) fig.add_tools(PanTool()) fig.add_tools(ResetTool()) return fig
def scroll_plotter(first_source, sources): source = ColumnDataSource(data=first_source) hover = HoverTool(tooltips=[("Country", "@countries"), ("Region", "@regions")]) tools = [ hover, WheelZoomTool(), PanTool(), BoxZoomTool(), ResetTool(), SaveTool() ] f = figure(tools=tools, plot_width=1000, plot_height=650) f.scatter(x="x", y="y", size=8, color="colors", source=source, legend="regions") callback = CustomJS(args=dict(source=source, source_list=sources), code=""" var slider_value = cb_obj.value; for (var i = 0; i < source_list.length; i++) { var source_data = source.data; if (i + 1994 == slider_value) { var new_source = source_list[i][1] source_data["x"] = new_source["x"] source_data["y"] = new_source["y"] source_data["colors"] = new_source["colors"] source_data["countries"] = new_source["countries"] source_data["regions"] = new_source["regions"] break; } } // Update source.change.emit(); """) slider = Slider(start=sources[0][0], end=sources[-1][0], value=1994, step=1, title="Year") slider.js_on_change('value', callback) f.yaxis[0].axis_label = "BMI" f.xaxis[0].axis_label = "Prices per KG rice" f.x_range = DataRange1d(start=0, end=3.5) f.y_range = DataRange1d(start=20, end=30) layout = row( f, widgetbox(slider), ) output_file("Rice_vs_BMI_slider.html") layout = column(widgetbox(slider), f) save(layout)
def __init__(self, n_rectangles=1000, clear_interval=20000, **kwargs): """ kwargs are applied to the bokeh.models.plots.Plot constructor """ self.n_rectangles = n_rectangles self.clear_interval = clear_interval self.last = 0 self.source = ColumnDataSource( data=dict(start=[time() - clear_interval], duration=[0.1], key=['start'], name=['start'], color=['white'], worker=['foo'], y=[0], worker_thread=[1], alpha=[0.0])) x_range = DataRange1d(range_padding=0) y_range = DataRange1d(range_padding=0) self.root = Plot(title=Title(text="Task Stream"), id='bk-task-stream-plot', x_range=x_range, y_range=y_range, toolbar_location="above", min_border_right=35, **kwargs) self.root.add_glyph( self.source, Rect(x="start", y="y", width="duration", height=0.4, fill_color="color", line_color="color", line_alpha=0.6, fill_alpha="alpha", line_width=3)) self.root.add_layout(DatetimeAxis(axis_label="Time"), "below") ticker = BasicTicker(num_minor_ticks=0) self.root.add_layout( LinearAxis(axis_label="Worker Core", ticker=ticker), "left") self.root.add_layout( Grid(dimension=1, grid_line_alpha=0.4, ticker=ticker)) self.root.yaxis.major_label_text_alpha = 0 hover = HoverTool(point_policy="follow_mouse", tooltips=""" <div> <span style="font-size: 12px; font-weight: bold;">@name:</span> <span style="font-size: 10px; font-family: Monaco, monospace;">@duration</span> <span style="font-size: 10px;">ms</span> </div> """) self.root.add_tools(hover, BoxZoomTool(), ResetTool(reset_size=False), PanTool(dimensions="width"), WheelZoomTool(dimensions="width")) if ExportTool: export = ExportTool() export.register_plot(self.root) self.root.add_tools(export) # Required for update callback self.task_stream_index = [0]
def __init__(self, **kwargs): self.source = ColumnDataSource( data={ 'time': [], 'cpu': [], 'memory_percent': [], 'network-send': [], 'network-recv': [] }) x_range = DataRange1d(follow='end', follow_interval=30000, range_padding=0) resource_plot = Plot(x_range=x_range, y_range=Range1d(start=0, end=1), toolbar_location=None, min_border_bottom=10, **kwargs) line_opts = dict(line_width=2, line_alpha=0.8) g1 = resource_plot.add_glyph( self.source, Line(x='time', y='memory_percent', line_color="#33a02c", **line_opts)) g2 = resource_plot.add_glyph( self.source, Line(x='time', y='cpu', line_color="#1f78b4", **line_opts)) resource_plot.add_layout( LinearAxis(formatter=NumeralTickFormatter(format="0 %")), 'left') legend_opts = dict(location='top_left', orientation='horizontal', padding=5, margin=5, label_height=5) resource_plot.add_layout( Legend(items=[('Memory', [g1]), ('CPU', [g2])], **legend_opts)) network_plot = Plot(x_range=x_range, y_range=DataRange1d(start=0), toolbar_location=None, **kwargs) g1 = network_plot.add_glyph( self.source, Line(x='time', y='network-send', line_color="#a6cee3", **line_opts)) g2 = network_plot.add_glyph( self.source, Line(x='time', y='network-recv', line_color="#b2df8a", **line_opts)) network_plot.add_layout(DatetimeAxis(axis_label="Time"), "below") network_plot.add_layout(LinearAxis(axis_label="MB/s"), 'left') network_plot.add_layout( Legend(items=[('Network Send', [g1]), ('Network Recv', [g2])], **legend_opts)) tools = [ PanTool(dimensions='width'), WheelZoomTool(dimensions='width'), BoxZoomTool(), ResetTool() ] if 'sizing_mode' in kwargs: sizing_mode = {'sizing_mode': kwargs['sizing_mode']} else: sizing_mode = {} combo_toolbar = ToolbarBox(tools=tools, logo=None, toolbar_location='right', **sizing_mode) self.root = row(column(resource_plot, network_plot, **sizing_mode), column(combo_toolbar, **sizing_mode), id='bk-resource-profiles-plot', **sizing_mode) # Required for update callback self.resource_index = [0]
def bkExport(self, settings): #layer = iface.legendInterface().layers()[0] layer = settings["layer"] field = settings["field"] gdfList = [] total = float(layer.featureCount()) counter = 0 for feature in layer.getFeatures(): counter = counter + 1 self.dlg.progressBar.setValue(counter / total * 100) featJsonString = feature.geometry().geometry().asJSON(17) featJson = json.loads(featJsonString) df = {} df["geometry"] = shape(featJson) if field: df["data"] = feature[field] else: df["data"] = 0 df["class"] = -1 for hField in settings["hoverFields"]: df[hField[0]] = feature[hField[0]] gdf = gpd.GeoDataFrame([df]) gdfList.append(gdf) gdf2 = gpd.GeoDataFrame(pd.concat(gdfList, ignore_index=True)) lons, lats = self.gpd_bokeh(gdf2) data = list(gdf2["data"]) height = settings["height"] width = settings["width"] renderer = layer.rendererV2() if renderer.type() == 'singleSymbol': print "singleSymbol" color = renderer.symbol().color().name() color_mapper = CategoricalColorMapper(factors=[-1], palette=[color]) elif renderer.type() == 'categorizedSymbol': print "categorizedSymbol" categories = renderer.categories() for i in xrange(len(categories)): if categories[i].value(): try: gdf2["class"][( gdf2["data"] == categories[i].value())] = i except: gdf2["class"][(gdf2["data"] == float( categories[i].value()))] = i colorPalette = [ symbol.color().name() for symbol in renderer.symbols() ] color_mapper = CategoricalColorMapper(factors=sorted( list(gdf2["class"].unique())), palette=colorPalette) elif renderer.type() == 'graduatedSymbol': print "graduatedSymbol" ranges = renderer.ranges() gdf2["class"] = map(renderer.legendKeyForValue, gdf2["data"]) colorPalette = [ symbol.color().name() for symbol in renderer.symbols() ] color_mapper = CategoricalColorMapper(factors=sorted( list(gdf2["class"].unique())), palette=colorPalette) else: print "otherSymbols" if settings["toolbar_location"] == "none": TOOLS = "" else: TOOLS = "pan,wheel_zoom,box_zoom,reset,hover,save" colorClass = list(gdf2["class"]) source = ColumnDataSource( data=dict(x=lons, y=lats, data=data, category=colorClass)) for hField in settings["hoverFields"]: source.add(gdf2[hField[0]], name=hField[0]) if settings["GoogleEnabled"]: print("Enable Google") map_options = GMapOptions( lat=np.nanmean([val for sublist in lats for val in sublist]), lng=np.nanmean([val for sublist in lons for val in sublist]), map_type=settings["googleMapType"], zoom=settings["zoomLevel"]) plot = GMapPlot(plot_width=width, plot_height=height, x_range=DataRange1d(), y_range=DataRange1d(), map_options=map_options, api_key=settings["GMAPIKey"]) source_patches = source patches = Patches(xs='x', ys='y', fill_alpha=settings["alpha"] / 100.0, line_color=settings["outlineColor"], line_width=settings["outlineWidth"], fill_color={ 'field': 'category', 'transform': color_mapper }) patches_glyph = plot.add_glyph(source_patches, patches) plot.add_tools(PanTool(), WheelZoomTool(), BoxSelectTool(), HoverTool()) else: #plot_width=width, plot_height=height, plot = figure(title=settings["title"], tools=TOOLS, plot_width=width, plot_height=height, x_axis_location=None, y_axis_location=None) plot.grid.grid_line_color = None plot.patches('x', 'y', source=source, fill_alpha=settings["alpha"] / 100.0, line_color=settings["outlineColor"], line_width=settings["outlineWidth"], fill_color={ 'field': 'category', 'transform': color_mapper }) plot.border_fill_color = settings["border_fill_color"] plot.background_fill_color = settings["background_fill_color"] plot.background_fill_alpha = settings["background_fill_alpha"] plot.outline_line_alpha = settings["outline_line_alpha"] plot.outline_line_color = settings["outline_line_color"] plot.outline_line_width = settings["outline_line_width"] if settings["toolbar_location"] == "none": plot.toolbar_location = None else: plot.toolbar_location = settings["toolbar_location"] plot.min_border_left = settings["min_border_left"] plot.min_border_right = settings["min_border_right"] plot.min_border_top = settings["min_border_top"] plot.min_border_bottom = settings["min_border_bottom"] plot.sizing_mode = settings["sizing_mode"] if settings["hoverFields"]: hover = plot.select_one(HoverTool) hover.point_policy = "follow_mouse" hover.tooltips = [ #(field, "@data") #("(Long, Lat)", "($x, $y)"), ] for hField in settings["hoverFields"]: temp = "@" + hField[0] hover.tooltips.append((hField[1], temp)) if settings["BokehJS"] == "CDN": print("CDN") html = file_html(plot, CDN, "my plot") elif settings["BokehJS"] == "INLINE": print("INLINE") html = file_html(plot, INLINE, "my plot") with open(self.settings["outputFile"] + "/map.html", "w") as my_html: my_html.write(html) settings["layer"] = settings["layer"].name() print settings with open(self.settings["outputFile"] + "/settings.json", 'w') as fp: json.dump(settings, fp)
# plotting for i in ['ov', 'mech', 'acc']: full_names = {'ov': 'Overall', 'mech': 'Mechanical', 'acc': 'Accidents'} # plot tools hover = HoverTool(tooltips=[ ("GP", "@year @country"), ("Started", "@started"), ("Retired overall", '@retired_overall'), ("Mechanical", '@retired_mech'), ("Accident", '@retired_accident'), ("Misc", '@retired_misc'), ]) tools = [ PanTool(), BoxZoomTool(), WheelZoomTool(), ResetTool(), SaveTool(), hover ] p = figure(title=f"Retirement percentage, {full_names[i]}", x_axis_label='Date', y_axis_label='Retirement percentage[%]', plot_width=1280, plot_height=720, x_axis_type='datetime', tools=tools) p.circle('date', f'ret_{i}_perc',
def do_plot(self, inputDir, plotOutDir, plotOutFileName, simDataFile, validationDataFile, metadata): if not os.path.isdir(inputDir): raise Exception, "inputDir does not currently exist as a directory" ap = AnalysisPaths(inputDir, variant_plot=True) variants = sorted(ap._path_data['variant'].tolist() ) # Sorry for accessing private data if len(variants) <= 1: return all_cells = sorted( ap.get_cells(variant=variants, seed=[0], generation=[0])) if not os.path.exists(plotOutDir): os.mkdir(plotOutDir) #make structures to hold mean flux values mean_fluxes = [] BURN_IN_STEPS = 20 n_variants = 0 IDs = [] #Puts you into the specific simulation's data. Pull fluxes from here #TODO LEARN HOW TO PULL FLUXES FROM LISTENER FILE (see kineticsflux comparison) for variant, simDir in zip(variants, all_cells): sim_data = cPickle.load(open(ap.get_variant_kb(variant), "rb")) simOutDir = os.path.join(simDir, "simOut") #crafting area enzymeKineticsReader = TableReader( os.path.join(simOutDir, "FBAResults")) # "EnzymeKinetics")) actualFluxes = enzymeKineticsReader.readColumn( "reactionFluxes") #"actualFluxes") IDs = enzymeKineticsReader.readAttribute("reactionIDs") enzymeKineticsReader.close() actualAve = np.mean(actualFluxes[BURN_IN_STEPS:, :], axis=0) mean_fluxes.append(actualAve) n_variants = n_variants + 1 ###Plot the fluxes plt.figure(figsize=(8.5, 11)) #Generalizred plotting for j in range(0, n_variants): for k in range(0, n_variants): if j <= k: continue plt.subplot(n_variants - 1, n_variants - 1, j + k) plt.plot(np.log10(mean_fluxes[j][:]), np.log10(mean_fluxes[k][:]), 'o') plt.plot([-12, 0], [-12, 0], color='k', linestyle='-', linewidth=2) plt.xlabel('Variant ' + str(j) + ' Flux') plt.ylabel('Variant ' + str(k) + ' Flux') plt.ylim((-11, 0)) plt.xlim((-11, 0)) exportFigure(plt, plotOutDir, plotOutFileName, metadata) plt.close("all") #nifty fun tool # Bokeh if len(mean_fluxes) < 2: return # Plot first metabolite to initialize plot settings x = np.log10(mean_fluxes[0][:]) y = np.log10(mean_fluxes[1][:]) source = ColumnDataSource(data=dict(x=x, y=y, rxn=IDs)) hover = HoverTool(tooltips=[ ("ID", "@rxn"), ]) TOOLS = [ hover, BoxZoomTool(), LassoSelectTool(), PanTool(), WheelZoomTool(), ResizeTool(), UndoTool(), RedoTool(), "reset" ] p = figure( x_axis_label="Variant 0 Flux", y_axis_label="Variant 1 Flux", width=800, height=800, tools=TOOLS, ) p.circle( 'x', 'y', size=5, source=source ) #np.log10(mean_fluxes[0][:]),np.log10(mean_fluxes[1][:]), size=10) p.line([-12, 0], [-12, 0], color="firebrick", line_width=2) if not os.path.exists(os.path.join(plotOutDir, "html_plots")): os.makedirs(os.path.join(plotOutDir, "html_plots")) bokeh.io.output_file(os.path.join(plotOutDir, "html_plots", plotOutFileName + ".html"), title=plotOutFileName, autosave=False) bokeh.io.save(p) bokeh.io.curstate().reset()
source = ColumnDataSource(data=dict(x=x, y=y)) xdr = DataRange1d() ydr = DataRange1d() plot = Plot(x_range=xdr, y_range=ydr, min_border=50) line_glyph = Line(x="x", y="y", line_color="blue") line = plot.add_glyph(source, line_glyph) plot.add_layout(LinearAxis(), 'above') plot.add_layout(LinearAxis(), 'below') plot.add_layout(LinearAxis(), 'left') plot.add_layout(LinearAxis(), 'right') pan = PanTool() wheel_zoom = WheelZoomTool() preview_save = PreviewSaveTool() plot.add_tools(pan, wheel_zoom, preview_save) from bokeh.core.enums import LegendLocation for location in LegendLocation: legend = Legend(legends=[(location, [line])], location=location) plot.add_layout(legend) legend = Legend(legends=[("x=100px, y=150px", [line])], location=(100, 150)) plot.add_layout(legend) doc = Document()
def makeplot_2(df, FC_P, PV_P, Inhibitor): df = df[df['Kinase'] != ''] # user define CV value: Rows Above CV_P filtered out df.loc[(df['Fold_change'] > FC_P) & (df['p_value'] < PV_P), 'color'] = "Blue" # upregulated #df.loc - Selects single row or subset of rows from the DataFrame by label df.loc[(df['Fold_change'] <= FC_P) & (df['p_value'] < PV_P), 'color'] = "Purple" # downregulated df['color'].fillna('grey', inplace=True) df["log_pvalue"] = -np.log10(df['p_value']) df["log_FC"] = np.log2(df['Fold_change']) df.head() output_notebook() category = 'Substrate' category_items = df[category].unique() title = Inhibitor + " :Data with identified kinases" #feeding data into ColumnDataSource source = ColumnDataSource(df) #Editing the hover that need to displayed while hovering hover = HoverTool( tooltips=[('Kinase', '@Kinase'), ('Substrate', '@Substrate'), ('Sub_gene', '@Sub_gene'), ('Phosphosite', '@Phosphosite'), ('Fold_change', '@Fold_change'), ('p_value', '@p_value')]) #tools that are need to explote data tools = [ hover, WheelZoomTool(), PanTool(), BoxZoomTool(), ResetTool(), SaveTool() ] #finally making figure with scatter plot pp = figure( tools=tools, title=title, plot_width=700, plot_height=400, toolbar_location='right', toolbar_sticky=False, ) pp.scatter(x='log_FC', y='log_pvalue', source=source, size=10, color='color') #displaying the graph return (pp)
def plot_df(data, day, hour, num_centroids): """This function takes a dataframe and plots the centroids vs the Lats, Longs of the incident data-points Inputs: - Dataframe - Day in string format - Hour in int format - Number of centroids wanted Output: A plot of lats and longs, overlayed on a map of Austin, filtered based on the hour and the day put into the function """ # Using the function created before, applying the proper format to the dataframe for modeling new_df = filter_df(data, day, hour) # Create a dataframe that has one count per FIPS_geoID # This will be used for the hover tool so that only one total count is shown when one hovers # Includes Lat, Long, count, size df_count = new_df.groupby(['FIPS_geoID', 'Lat', 'Long', 'size'])['Count_per_FIPS'].count() \ .reset_index(name='count').sort_values('count') # Modeling the data with KMeans X = np.array(new_df[['Lat', 'Long']]) model = KMeans(n_init=100, n_clusters=num_centroids, max_iter=400, tol=1e-8) model.fit(X) centroids = model.cluster_centers_ # Defining the Lat,Long to pass into Google maps API cent_lats = list(centroids[:, 0]) cent_longs = list(centroids[:, 1]) # Actual Lat,Long of incidents incident_lats = list(df_count['Lat']) incident_longs = list(df_count['Long']) # Normalized FIPS count to alter plot point size based on count of incidents # df_size = new_df['size'].values # Geoplotting!!!!!!!! map_options = GMapOptions(lat=30.2672, lng=-97.7431, map_type="roadmap", zoom=11) plot = GMapPlot( x_range=Range1d(), y_range=Range1d(), map_options=map_options) plot.title.text = "Austin (Day: {}, Hour: {})".format(day, hour) # 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: plot.api_key = os.environ['GOOGLE_API_KEY'] completed_source = ColumnDataSource(data=dict( lat=cent_lats, lon=cent_longs, )) completed_dots = Circle(x="lon", y="lat", size=55, fill_color="blue", fill_alpha=0.2, line_color=None) plot.add_glyph(completed_source, completed_dots) completed_source = ColumnDataSource(data=dict( lat=incident_lats, lon=incident_longs, size=df_count['size'].values, count=df_count['count'].values)) # Size of lat,long plots will be determind from the normalized count column # If size < .11 then the plot will be size=2 ... otherwise it will multiply by 28 completed_dots = Circle(x="lon", y="lat", size="size", fill_color="red", fill_alpha=0.8, line_color=None) plot.add_glyph(completed_source, completed_dots) # Hover tool implementation hover = HoverTool(tooltips=[ ("Incident Count", "@count")]) # The P variables below are changing the location of my centroids, not sure what's going on here # p = figure(plot_width=200, plot_height=200, tools=[hover],title="Mouse over the dots") # p.circle('lon', 'lat', size=5, source=completed_source) # show(p) plot.add_tools(PanTool(), WheelZoomTool(), BoxSelectTool(), hover) show(plot)
def create(palm): fit_max = 1 fit_min = 0 doc = curdoc() # THz calibration plot scan_plot = Plot( title=Title(text="THz calibration"), x_range=DataRange1d(), y_range=DataRange1d(), plot_height=PLOT_CANVAS_HEIGHT, plot_width=PLOT_CANVAS_WIDTH, toolbar_location='right', ) # ---- tools scan_plot.toolbar.logo = None scan_plot.add_tools(PanTool(), BoxZoomTool(), WheelZoomTool(), ResetTool()) # ---- axes scan_plot.add_layout(LinearAxis(axis_label='Stage delay motor'), place='below') scan_plot.add_layout(LinearAxis(axis_label='Energy shift, eV', major_label_orientation='vertical'), place='left') # ---- grid lines scan_plot.add_layout(Grid(dimension=0, ticker=BasicTicker())) scan_plot.add_layout(Grid(dimension=1, ticker=BasicTicker())) # ---- circle cluster glyphs scan_circle_source = ColumnDataSource(dict(x=[], y=[])) scan_plot.add_glyph(scan_circle_source, Circle(x='x', y='y', line_alpha=0, fill_alpha=0.5)) # ---- circle glyphs scan_avg_circle_source = ColumnDataSource(dict(x=[], y=[])) scan_plot.add_glyph( scan_avg_circle_source, Circle(x='x', y='y', line_color='purple', fill_color='purple')) # ---- line glyphs fit_line_source = ColumnDataSource(dict(x=[], y=[])) scan_plot.add_glyph(fit_line_source, Line(x='x', y='y', line_color='purple')) # THz calibration folder path text input def path_textinput_callback(_attr, _old, _new): update_load_dropdown_menu() path_periodic_update() path_textinput = TextInput(title="THz calibration path:", value=os.path.join(os.path.expanduser('~')), width=510) path_textinput.on_change('value', path_textinput_callback) # THz calibration eco scans dropdown def scans_dropdown_callback(_attr, _old, new): scans_dropdown.label = new scans_dropdown = Dropdown(label="ECO scans", button_type='default', menu=[]) scans_dropdown.on_change('value', scans_dropdown_callback) # ---- eco scans periodic update def path_periodic_update(): new_menu = [] if os.path.isdir(path_textinput.value): for entry in os.scandir(path_textinput.value): if entry.is_file() and entry.name.endswith('.json'): new_menu.append((entry.name, entry.name)) scans_dropdown.menu = sorted(new_menu, reverse=True) doc.add_periodic_callback(path_periodic_update, 5000) # Calibrate button def calibrate_button_callback(): palm.calibrate_thz( path=os.path.join(path_textinput.value, scans_dropdown.value)) fit_max_textinput.value = str( np.ceil(palm.thz_calib_data.index.values.max())) fit_min_textinput.value = str( np.floor(palm.thz_calib_data.index.values.min())) update_calibration_plot() def update_calibration_plot(): scan_plot.xaxis.axis_label = '{}, {}'.format(palm.thz_motor_name, palm.thz_motor_unit) scan_circle_source.data.update( x=np.repeat(palm.thz_calib_data.index, palm.thz_calib_data['peak_shift'].apply(len)).tolist(), y=np.concatenate( palm.thz_calib_data['peak_shift'].values).tolist(), ) scan_avg_circle_source.data.update( x=palm.thz_calib_data.index.tolist(), y=palm.thz_calib_data['peak_shift_mean'].tolist()) x = np.linspace(fit_min, fit_max, 100) y = palm.thz_slope * x + palm.thz_intersect fit_line_source.data.update(x=np.round(x, decimals=5), y=np.round(y, decimals=5)) calib_const_div.text = """ thz_slope = {} """.format(palm.thz_slope) calibrate_button = Button(label="Calibrate THz", button_type='default', width=250) calibrate_button.on_click(calibrate_button_callback) # THz fit maximal value text input def fit_max_textinput_callback(_attr, old, new): nonlocal fit_max try: new_value = float(new) if new_value > fit_min: fit_max = new_value palm.calibrate_thz( path=os.path.join(path_textinput.value, scans_dropdown.value), fit_range=(fit_min, fit_max), ) update_calibration_plot() else: fit_max_textinput.value = old except ValueError: fit_max_textinput.value = old fit_max_textinput = TextInput(title='Maximal fit value:', value=str(fit_max)) fit_max_textinput.on_change('value', fit_max_textinput_callback) # THz fit maximal value text input def fit_min_textinput_callback(_attr, old, new): nonlocal fit_min try: new_value = float(new) if new_value < fit_max: fit_min = new_value palm.calibrate_thz( path=os.path.join(path_textinput.value, scans_dropdown.value), fit_range=(fit_min, fit_max), ) update_calibration_plot() else: fit_min_textinput.value = old except ValueError: fit_min_textinput.value = old fit_min_textinput = TextInput(title='Minimal fit value:', value=str(fit_min)) fit_min_textinput.on_change('value', fit_min_textinput_callback) # Save calibration button def save_button_callback(): palm.save_thz_calib(path=path_textinput.value) update_load_dropdown_menu() save_button = Button(label="Save", button_type='default', width=250) save_button.on_click(save_button_callback) # Load calibration button def load_dropdown_callback(_attr, _old, new): palm.load_thz_calib(os.path.join(path_textinput.value, new)) update_calibration_plot() def update_load_dropdown_menu(): new_menu = [] calib_file_ext = '.palm_thz' if os.path.isdir(path_textinput.value): for entry in os.scandir(path_textinput.value): if entry.is_file() and entry.name.endswith((calib_file_ext)): new_menu.append( (entry.name[:-len(calib_file_ext)], entry.name)) load_dropdown.button_type = 'default' load_dropdown.menu = sorted(new_menu, reverse=True) else: load_dropdown.button_type = 'danger' load_dropdown.menu = new_menu doc.add_next_tick_callback(update_load_dropdown_menu) doc.add_periodic_callback(update_load_dropdown_menu, 5000) load_dropdown = Dropdown(label="Load", menu=[], width=250) load_dropdown.on_change('value', load_dropdown_callback) # Calibration constants calib_const_div = Div(text=""" thz_slope = {} """.format(0)) # assemble tab_layout = column( row( scan_plot, Spacer(width=30), column( path_textinput, scans_dropdown, calibrate_button, fit_max_textinput, fit_min_textinput, row(save_button, load_dropdown), calib_const_div, ), )) return Panel(child=tab_layout, title="THz Calibration")
from bokeh.plotting import figure from time import time from datetime import datetime import pandas as pd #bokeh serve --allow-websocket-origin=localhost:5000 sensor_control.py #get start time of script start_time = time() #connect to server and generate csv file filename_all_data = r'/opt/webapps/sensor_surveillance/data/sc_all_data.csv' #this file contains all data filename_new_data = r'/opt/webapps/sensor_surveillance/data/sc_new_data.csv' #this file contains newest data for streaming it #create figure f_photo = figure(tools=[PanTool(), WheelZoomTool(), ResetTool(), SaveTool()], output_backend='webgl') hover = HoverTool(tooltips=[('Date', '@date')]) f_photo.add_tools(hover) f_photo.toolbar.logo = None f_aux = figure(tools=[PanTool(), WheelZoomTool(), ResetTool(), SaveTool()], output_backend='webgl') hover = HoverTool(tooltips=[('Date', '@date')]) f_aux.add_tools(hover)
def plot_total_all_time(result): color_palette = ["#EF476F", "#FFD166", "#06D6A0", "#118AB2", "#073B4C"] x = [] y = [] colors = [] for participant, value, color in zip(result.keys(), result.values(), color_palette): x.append(participant) y.append(value["total_all_time"]) colors.append(color) tools = [ WheelZoomTool(), PanTool(), BoxZoomTool(), ResetTool(), SaveTool() ] fig = figure( x_range=x, plot_height=300, plot_width=300, sizing_mode="scale_height", title="Total messages sent", toolbar_location="below", tools=tools, toolbar_sticky=False, background_fill_color="beige", background_fill_alpha=0.25, ) data = {"x_values": x, "y_values": y, "color_values": colors} source = ColumnDataSource(data=data) fig.vbar(x="x_values", top="y_values", width=0.8, color="color_values", legend_field="x_values", source=source) # adds the count above the bar chart labels = LabelSet(x='x_values', y='y_values', text='y_values', level='glyph', x_offset=-13.5, y_offset=0, source=source, render_mode='canvas') fig.add_layout(labels) # title styling fig.title.align = "center" fig.title.text_font_size = "14pt" # y-axis styling fig.yaxis.axis_label = "Count" fig.yaxis.axis_label_text_font_size = "12pt" fig.yaxis.axis_label_text_font_style = "bold" fig.y_range.start = 0 # x axis styling fig.xaxis.axis_label = "Sender" fig.xaxis.axis_label_text_font_size = "12pt" fig.xaxis.axis_label_text_font_style = "bold" # legend styling fig.legend.location = "top_left" # shows the actual numerical value instead of scientific fig.left[0].formatter.use_scientific = False output_file("total_messages_sent.html") return fig
def std_distance_time_plot(nx, ny, source, x_range=None, output_backend=DEFAULT_BACKEND): # avoid range errors if len(source[N.ACTIVE_TIME_S].dropna()) < 2: return None groups = [ group for statistic, group in related_statistics(source, N.ACTIVE_TIME) ] if not groups: # original monochrome plot return multi_dot_plot(nx, ny, N.TIME, [N.ACTIVE_TIME_H, N.ACTIVE_DISTANCE_KM], source, ['black', 'grey'], alphas=[1, 0.5], x_range=x_range, rescale=True) times = [f'{N.ACTIVE_TIME}:{group}' for group in groups] distances = [f'{N.ACTIVE_DISTANCE}:{group}' for group in groups] time_y_range = make_range(source[N.ACTIVE_TIME_H]) distance_y_range = make_range(source[N.ACTIVE_DISTANCE_KM]) colours = list(evenly_spaced_hues(len(groups))) tooltip_names = [ N.ACTIVE_TIME_H, N.ACTIVE_DISTANCE_KM, N.ACTIVITY_GROUP, N.LOCAL_TIME ] tooltip_names += [ name for name in like(N._delta(N.FITNESS_ANY), source.columns) if ':' not in name ] tooltip_names += [ name for name in like(N._delta(N.FATIGUE_ANY), source.columns) if ':' not in name ] tools = [ PanTool(dimensions='width'), ZoomInTool(dimensions='width'), ZoomOutTool(dimensions='width'), ResetTool(), HoverTool(tooltips=[tooltip(name) for name in tooltip_names], names=['with_hover']) ] f = figure(output_backend=output_backend, plot_width=nx, plot_height=ny, x_axis_type='datetime', tools=tools) f.yaxis.axis_label = f'lines - {N.ACTIVE_TIME_H}' f.y_range = time_y_range f.extra_y_ranges = {N.ACTIVE_DISTANCE: distance_y_range} f.add_layout( LinearAxis(y_range_name=N.ACTIVE_DISTANCE, axis_label=f'dots - {N.ACTIVE_DISTANCE_KM}'), 'right') plotter = comb_plotter() for time, colour, group in zip(times, colours, groups): time_h = N._slash(time, U.H) source[time_h] = source[time] / 3600 source[N.ACTIVITY_GROUP] = group plotter(f, x=N.TIME, y=time_h, source=source, color=colour, alpha=1) plotter = dot_plotter() for distance, colour, group in zip(distances, colours, groups): distance_km = N._slash(distance, U.KM) source[distance_km] = source[distance] source[N.ACTIVITY_GROUP] = group plotter(f, x=N.TIME, y=distance_km, source=source, color=colour, alpha=1, name='with_hover', y_range_name=N.ACTIVE_DISTANCE) f.xaxis.axis_label = N.TIME f.toolbar.logo = None if ny < 300: f.toolbar_location = None if x_range: f.x_range = x_range return f
def _heatmap_summary(pvals, coefs, plot_width=1200, plot_height=400): """ Plots heatmap of coefficients colored by pvalues Parameters ---------- pvals : pd.DataFrame Table of pvalues where rows are balances and columns are covariates. coefs : pd.DataFrame Table of coefficients where rows are balances and columns are covariates. plot_width : int, optional Width of plot. plot_height : int, optional Height of plot. Returns ------- bokeh.charts.Heatmap Heatmap summarizing the regression statistics. """ c = coefs.reset_index() c = c.rename(columns={'index': 'balance'}) # fix alpha in fdr to account for the number of covariates def fdr(x): return multipletests(x, method='fdr_bh', alpha=0.05 / pvals.shape[1])[1] cpvals = pvals.apply(fdr, axis=0) # log scale for coloring log_p = -np.log10(cpvals + 1e-200) log_p = log_p.reset_index() log_p = log_p.rename(columns={'index': 'balance'}) p = pvals.reset_index() p = p.rename(columns={'index': 'balance'}) cp = cpvals.reset_index() cp = cp.rename(columns={'index': 'balance'}) cm = pd.melt(c, id_vars='balance', var_name='Covariate', value_name='Coefficient') pm = pd.melt(p, id_vars='balance', var_name='Covariate', value_name='Pvalue') cpm = pd.melt(cp, id_vars='balance', var_name='Covariate', value_name='Corrected_Pvalue') logpm = pd.melt(log_p, id_vars='balance', var_name='Covariate', value_name='log_Pvalue') m = pd.merge(cm, pm, left_on=['balance', 'Covariate'], right_on=['balance', 'Covariate']) m = pd.merge(m, logpm, left_on=['balance', 'Covariate'], right_on=['balance', 'Covariate']) m = pd.merge(m, cpm, left_on=['balance', 'Covariate'], right_on=['balance', 'Covariate']) hover = HoverTool(tooltips=[( "Pvalue", "@Pvalue"), ("Corrected Pvalue", "@Corrected_Pvalue"), ("Coefficient", "@Coefficient")]) N, _min, _max = len(palette), m.log_Pvalue.min(), m.log_Pvalue.max() X = pd.Series(np.arange(len(pvals.index)), index=pvals.index) Y = pd.Series(np.arange(len(pvals.columns)), index=pvals.columns) m['X'] = [X.loc[i] for i in m.balance] m['Y'] = [Y.loc[i] for i in m.Covariate] # fill in nans with zero. Sometimes the pvalue calculation fails. m = m.fillna(0) for i in m.index: x = m.loc[i, 'log_Pvalue'] ind = int(np.floor((x - _min) / (_max - _min) * (N - 1))) m.loc[i, 'color'] = palette[ind] source = ColumnDataSource(ColumnDataSource.from_df(m)) hm = figure(title='Regression Coefficients Summary', plot_width=1200, plot_height=400, tools=[ hover, PanTool(), BoxZoomTool(), WheelZoomTool(), ResetTool(), SaveTool() ]) hm.rect(x='X', y='Y', width=1, height=1, fill_color='color', line_color="white", source=source) Xlabels = pd.Series(pvals.index, index=np.arange(len(pvals.index))) Ylabels = pd.Series( pvals.columns, index=np.arange(len(pvals.columns)), ) hm.xaxis[0].ticker = FixedTicker(ticks=Xlabels.index) hm.xaxis.formatter = FuncTickFormatter(code=""" var labels = %s; return labels[tick]; """ % Xlabels.to_dict()) hm.yaxis[0].ticker = FixedTicker(ticks=Ylabels.index) hm.yaxis.formatter = FuncTickFormatter(code=""" var labels = %s; return labels[tick]; """ % Ylabels.to_dict()) return hm
map_options = GMapOptions(lat=0, lng=0, zoom=5) plot = GMapPlot(x_range=Range1d(), y_range=Range1d(), map_options=map_options) plot.title.text = 'Something interesting here' plot.api_key = input('Paste API key here: ') source = ColumnarDataSource(data=dict(lat=lats, long=lons)) circle = Circle(x='lon', y='lat', size=10, fill_color='red') plot.add_glyph(source, circle) plot.add_tools(PanTool(), WheelZoomTool(), BoxSelectTool()) output_file('gmap_plot.html') #------------------- the_keyword = b'TRUTH' key = hashlib.sha3_256(the_keyword) key_bytes = key.digest() fernet_key = base64.urlsafe_b64encode(key_bytes) print(fernet_key)
def make_weight_graph(stock_name=None): """ """ try: API_URL = 'https://api.iextrading.com/1.0' res = requests.get(f'{ API_URL }/stock/{ stock_name }/chart/5y') data = res.json() except JSONDecodeError: abort(404) df = pd.DataFrame(data) # df['date'] = pd.to_datetime(df['date']) df['adjVolume'] = 6 * df['volume'] // df['volume'].mean() df = df[['date', 'vwap', 'volume', 'adjVolume']] seqs = np.arange(df.shape[0]) df['seqs'] = pd.Series(seqs) source = ColumnDataSource(df) colors = [ "#75968f", "#a5bab7", "#c9d9d3", "#e2e2e2", "#dfccce", "#ddb7b1", "#cc7878", "#933b41", "#550b1d" ] mapper = LinearColorMapper(palette=colors, low=df.adjVolume.min(), high=df.adjVolume.max()) hover = HoverTool(tooltips=[ ('Date', '@date'), ('Vwap', '@vwap'), ('Volume', '@volume'), ], names=["circle"]) TOOLS = [ hover, BoxZoomTool(), PanTool(), ZoomInTool(), ZoomOutTool(), ResetTool() ] p = figure(plot_width=1000, plot_height=800, title=f'5 year weighted performace of {stock_name}', tools=TOOLS, toolbar_location='above') p.circle(x="seqs", y="vwap", source=source, size='adjVolume', fill_color=transform('adjVolume', mapper), name="circle") color_bar = ColorBar(color_mapper=mapper, location=(0, 0), ticker=BasicTicker(desired_num_ticks=len(colors)), formatter=PrintfTickFormatter(format="%s%%")) p.add_layout(color_bar, 'right') p.axis.axis_line_color = None p.axis.major_tick_line_color = None p.axis.major_label_text_font_size = "5pt" p.axis.major_label_standoff = 0 p.xaxis.major_label_orientation = 1.0 script, div = components(p) return script, div, stock_name
def fit(self, cv_results, estimeted_end_time): cv_results, cv_score_std, param_dists = self._init_cv_results( cv_results) if self.bokeh_handle is None: if cv_results is None: return # mk bokeh source self.cv_src, cv_hover = self._mk_score_source( cv_results, xcol=NoteBookVisualizer.time_col, score_cols=[ NoteBookVisualizer.score_cols[i] for i in self.data_types ], hover_cols=self.all_param_cols) self.end_time_src = ColumnDataSource(data=dict(text=[ "This search end time(estimated): " + estimeted_end_time ])) self.cv_score_std_src = ColumnDataSource(data=cv_score_std) self.best_src = self._mk_score_source( cv_results, xcol=NoteBookVisualizer.time_col, score_cols=["best_" + i for i in self.data_types]) self.param_srcs = dict() for key in param_dists.keys(): self.param_srcs[key] = ColumnDataSource(data=param_dists[key]) # CV Score transition cv_p = figure( title="CV Score transition", x_axis_label="time", y_axis_label="score", x_axis_type="datetime", plot_width=int(NoteBookVisualizer.display_width / 2), plot_height=275, toolbar_location="above", tools=[SaveTool(), ResetTool(), PanTool(), WheelZoomTool()]) for data_type in self.data_types: if data_type == "valid": cv_p = self._add_line( cv_p, xcol=NoteBookVisualizer.time_col, ycol=NoteBookVisualizer.score_cols[data_type], score_source=self.cv_src, color=NoteBookVisualizer.colors[data_type], legend=data_type) else: cv_p = self._add_line( cv_p, xcol=NoteBookVisualizer.time_col, ycol=NoteBookVisualizer.score_cols[data_type], score_source=self.cv_src, score_std_source=self.cv_score_std_src, color=NoteBookVisualizer.colors[data_type], legend=data_type) display_etime = LabelSet(x=0, y=0, x_offset=80, y_offset=20, x_units="screen", y_units="screen", render_mode="canvas", text="text", source=self.end_time_src, text_font="segoe ui", text_font_style="italic", background_fill_color="white", background_fill_alpha=0.5) cv_p.add_layout(display_etime) cv_p.add_tools(cv_hover) cv_p.legend.location = "top_left" cv_p.xaxis.minor_tick_line_color = None cv_p.yaxis.minor_tick_line_color = None cv_p = self._arrange_fig(cv_p) # Best Score transition best_p = figure( title="Best Score transition", x_axis_label="time", y_axis_label="score", x_range=cv_p.x_range, y_range=cv_p.y_range, x_axis_type="datetime", plot_width=int(NoteBookVisualizer.display_width / 2), plot_height=275, toolbar_location="above", tools=[PanTool(), WheelZoomTool(), SaveTool(), ResetTool()]) for data_type in self.data_types: best_p = self._add_line( best_p, xcol=NoteBookVisualizer.time_col, ycol="best_" + data_type, score_source=self.best_src, color=NoteBookVisualizer.colors[data_type], legend=data_type) best_p.legend.location = "top_left" best_p.xaxis.minor_tick_line_color = None best_p.yaxis.minor_tick_line_color = None best_p = self._arrange_fig(best_p) # Param distributions param_vbar_ps = dict() param_hist_ps = dict() tmp = list(self.param_cols) if st.FEATURE_SELECT_PARAMNAME_PREFIX in self.param_srcs.keys(): tmp = [st.FEATURE_SELECT_PARAMNAME_PREFIX] + tmp for param_col in tmp: if "label" in list(param_dists[param_col].keys()): # Bar graph param_vbar_ps[param_col] = figure( title=param_col, y_axis_label="frequency", plot_width=int(NoteBookVisualizer.display_width / NoteBookVisualizer.n_col_param), plot_height=int(NoteBookVisualizer.display_width / NoteBookVisualizer.n_col_param), #x_range=FactorRange(factors=self.param_srcs[param_col].data["x"]), y_range=DataRange1d(min_interval=1.0, start=0, default_span=1.0), toolbar_location="above", tools=[ SaveTool(), HoverTool(tooltips=[("label", "@label"), ("top", "@top")]) ]) param_vbar_ps[param_col].vbar( x="x", top="top", source=self.param_srcs[param_col], width=0.5, bottom=0, color="#9467bd", fill_alpha=0.5) labels = LabelSet(x="x", y=0, level="glyph", text="label", text_align="center", text_font="segoe ui", text_font_style="normal", text_font_size="8pt", x_offset=0, y_offset=0, source=self.param_srcs[param_col], render_mode="canvas") param_vbar_ps[param_col].add_layout(labels) param_vbar_ps[ param_col].xaxis.major_label_text_font_size = "0pt" param_vbar_ps[param_col].xaxis.major_tick_line_color = None param_vbar_ps[param_col].xaxis.minor_tick_line_color = None param_vbar_ps[param_col].yaxis.minor_tick_line_color = None param_vbar_ps[param_col] = self._arrange_fig( param_vbar_ps[param_col]) else: # Histgram param_hist_ps[param_col] = figure( title=param_col, y_axis_label="frequency", plot_width=int(NoteBookVisualizer.display_width / NoteBookVisualizer.n_col_param), plot_height=int(NoteBookVisualizer.display_width / NoteBookVisualizer.n_col_param), y_range=DataRange1d(min_interval=1.0, start=0), toolbar_location="above", tools=[ SaveTool(), HoverTool( tooltips=[("left", "@left"), ("right", "@right"), ("top", "@top")]) ]) param_hist_ps[param_col].quad( top="top", bottom=0, left="left", right="right", source=self.param_srcs[param_col], color="#17becf", fill_alpha=0.5) param_hist_ps[param_col].xaxis.minor_tick_line_color = None param_hist_ps[param_col].yaxis.minor_tick_line_color = None param_hist_ps[param_col] = self._arrange_fig( param_hist_ps[param_col]) title = Div(text=NoteBookVisualizer.title.replace( "TEXT", self.model_id), width=int(NoteBookVisualizer.display_width)) scores_headline = Div(text=NoteBookVisualizer.headline.replace( "TEXT", " Score History"), width=int(NoteBookVisualizer.display_width * 0.9)) params_headline = Div(text=NoteBookVisualizer.headline.replace( "TEXT", " Parameter History"), width=int(NoteBookVisualizer.display_width * 0.9)) self.p = layouts.layout([title, [scores_headline]]+[[cv_p, best_p]]+[[params_headline]]+\ [list(param_vbar_ps.values())[i:i+NoteBookVisualizer.n_col_param] for i in range(0, len(param_vbar_ps), NoteBookVisualizer.n_col_param)]+\ [list(param_hist_ps.values())[i:i+NoteBookVisualizer.n_col_param] for i in range(0, len(param_hist_ps), NoteBookVisualizer.n_col_param)]) self.bokeh_handle = show(self.p, notebook_handle=True) else: # update bokeh src self.end_time_src.patch({ "text": [(0, "This search end time(estimated): " + estimeted_end_time)] }) if len(cv_results) != len( self.cv_src.data[NoteBookVisualizer.time_col]): self.cv_src.stream(cv_results[list( self.cv_src.data.keys())].iloc[-1:].to_dict(orient="list"), rollover=NoteBookVisualizer.stream_rollover) self.best_src.stream( cv_results[list( self.best_src.data.keys())].iloc[-1:].to_dict( orient="list"), rollover=NoteBookVisualizer.stream_rollover) push_notebook(handle=self.bokeh_handle) self._update_cv_score_std_src(cv_score_std) self._update_param_srcs(param_dists) if self.savepath is not None: self._save_graph(search_algo=str( cv_results["search_algo"].iloc[0]), n_iter=int(cv_results["index"].iloc[-1]))
"rgb(0, 100, 120)", "green", "blue", "#2c7fb8", "#2c7fb8", "rgba(120, 230, 150, 0.5)", "rgba(120, 230, 150, 0.5)" ])) plot = MyPlot(gradient_angle=45) circle = Circle(x="x", y="y", radius=0.2, fill_color="color", line_color="black") circle_renderer = plot.add_glyph(source, circle) plot.add_layout(LinearAxis(), 'below') plot.add_layout(LinearAxis(), 'left') tap = TapTool(renderers=[circle_renderer], callback=Popup(message="Selected color: @color")) plot.add_tools(PanTool(), WheelZoomTool(), tap) doc = Document() doc.add_root(plot) if __name__ == "__main__": doc.validate() filename = "custom.html" with open(filename, "w") as f: f.write(file_html(doc, INLINE, "Demonstration of user-defined models")) print("Wrote %s" % filename) view(filename)
df_deep = df[df['depths'] >= 300] eq_info_shallow = ColumnDataSource(df_shallow) eq_info_middle = ColumnDataSource(df_middle) eq_info_deep = ColumnDataSource(df_deep) return (eq_info_shallow, eq_info_middle, eq_info_deep) ## range bounds supplied in web mercator coordinates x_range, y_range = ((-18706892.5544, 21289852.6142), (-7631472.9040, 12797393.0236)) p = figure(width=1280, height=720, x_range=x_range, y_range=y_range) ## Styling p.add_tile(tile_provider) p.tools = [PanTool(), WheelZoomTool(), BoxZoomTool(), ResetTool(), SaveTool()] hover = HoverTool(tooltips=""" <div> <div> <span style="font-size: 12px; font-weight: bold;">@event_text</span> </div> <div> <span style="font-size: 10px; color: #696;">Event time: @event_time</span><br> <span style="font-size: 10px; color: #696;">Coordinates: (@longitudes,@latitudes)</span><br> <span style="font-size: 10px; color: #696;">Magnitude: @magnitudes @magnitude_type</span><br> <span style="font-size: 10px; color: #696;">Depth: @depths km</span> </div> </div> """) p.add_tools(hover) p.toolbar_location = 'above'