def colorPalette(self, size=None): # https://bokeh.pydata.org/en/latest/docs/reference/palettes.html # https://bokeh.pydata.org/en/latest/docs/reference/colors.html color = list(d3['Category10'][10]) # [ 'orangered', 'cornflowerblue', ] # color = list(Spectral9).reverse() if size is None: return color elif size <= len(color): return color[0:size] elif size <= 256: return linear_palette(plasma(256), size) else: return linear_palette(plasma(256) + viridis(256) + magma(256), size)
def prep_palette(self, pname, binverse=False): """ Prepares a palette based on a name :param pname: :return: """ res = palettes.grey(256) if pname == 'Greys256': res = palettes.grey(256) elif pname == 'Inferno256': res = palettes.inferno(256) elif pname == 'Magma256': res = palettes.magma(256) elif pname == 'Plasma256': res = palettes.plasma(256) elif pname == 'Viridis256': res = palettes.viridis(256) elif pname == 'Cividis256': res = palettes.cividis(256) elif pname == 'Turbo256': res = palettes.turbo(256) elif pname == 'Bokeh8': res = palettes.small_palettes['Bokeh'][8] elif pname == 'Spectral11': res = palettes.small_palettes['Spectral'][11] elif pname == 'RdGy11': res = palettes.small_palettes['RdGy'][11] elif pname == 'PiYG11': res = palettes.small_palettes['PiYG'][11] if binverse: res = res[::-1] return res
def __init__(self, data_df, bins_text_list=None, bins_list=20): # If bins is an int, it defines the number of equal-width bins in the given range (10, by default). # If bins is a sequence, it defines the bin edges, including the rightmost edge, allowing for non-uniform bin widths. hist, edges = np.histogram(data_df, bins=bins_list) bin_list_len = len(hist) bottom_list = [0] * bin_list_len fill_color_list = plasma(bin_list_len) fill_color_list.reverse() line_color_list = ['black'] * bin_list_len id_list = [-1] * bin_list_len if bins_text_list is None: bins_text_list = [""] * bin_list_len bins_text_list[0] = "0 - " + str('{0:.2f}'.format(edges[1])) for idx in range(1, bin_list_len): bins_text_list[idx] = str('{0:.2f}'.format(edges[idx])) + " - " + str('{0:.2f}'.format(edges[idx + 1])) data_histogram_cds = { 'bottom': bottom_list, 'top': hist, 'left': edges[:-1], 'right': edges[1:], 'fill_color': fill_color_list, 'line_color': line_color_list, 'bins_text': bins_text_list, 'id': id_list } self.df_histogram_cds = pd.DataFrame(data_histogram_cds)
def test_cmap_generator_function(): assert pal.viridis(256) == pal.Viridis256 assert pal.magma(256) == pal.Magma256 assert pal.plasma(256) == pal.Plasma256 assert pal.inferno(256) == pal.Inferno256 assert pal.gray(256) == pal.Greys256 assert pal.grey(256) == pal.Greys256
def chart1(df): xdata, ydata, xrng, yrng = set_up(df.index, df.stack(), truncated=False) marg = 0.005 yrng = (yrng[0] - marg, yrng[1] + marg) p = figure( width=1000, height=500, title="Monetary Shock: March 3rd, 2020", x_axis_label="Time of Day", x_axis_type="datetime", # y_axis_label = '', y_range=yrng, x_range=xrng, ) p.line(xrng, [0, 0], color="black") p.line([mtg_date, mtg_date], [0, 100], color="black") p.line(xdata, 1, color="black", line_dash="dashed") colors = list(plasma(len(tks))[::-1]) for t in tks: p.line(xdata, df[t], color=colors.pop(), legend_label="${}".format(t)) p.xaxis[0].ticker.desired_num_ticks = 10 p.legend.location = "bottom_right" p.ygrid.grid_line_color = None # p.yaxis.formatter=NumeralTickFormatter(format="0.0%") export_png(p, filename="imgs/chart1.png") return p
def lineplot( data, x, y, hue=None, trials=None, aggregate=True, legend_pos="top_left", y_axis_format="00.00", title="Plot", y_axis_scale="linear", ): # set the figure fig = set_figure(title, y_axis_format=y_axis_format, y_axis_scale=y_axis_scale) # get the mean of each event y_mean = transform(data, [x, hue], y) # get the names of each event we're plotting hues = y_mean[hue].unique() palette = plasma(len(hues)) # iterate through events and create a line for each for hue_name, color in zip(hues, palette): """ if trials is None: # truncate to the slowest trial df = data.loc[data[hue] == hue_name] min_idx = df.groupby('trial')[x].max().min() df = df.loc[data[x] <= min_idx] """ hue_mean = y_mean.loc[y_mean[hue] == hue_name] if aggregate: fig.line( x=x, y=y, legend=hue_name, source=hue_mean, line_width=4, color=color, alpha=1, ) if trials: fig = add_trials(data, x, y, hue, trials, hue_name, fig, color, aggregate) else: y_std = transform(data, [x, hue], y, transformation="std") hue_std = y_std.loc[y_std[hue] == hue_name].copy() fig = add_band(x, y, hue_mean, hue_std, hue_name, fig, color) # additional settings fig.legend.location = legend_pos fig.legend.click_policy = "hide" return fig
def test_cmap_generator_function(): assert pal.viridis(256) == pal.Viridis256 assert pal.magma(256) == pal.Magma256 assert pal.plasma(256) == pal.Plasma256 assert pal.inferno(256) == pal.Inferno256 assert pal.gray(256) == pal.Greys256 assert pal.grey(256) == pal.Greys256 assert pal.turbo(256) == pal.Turbo256 assert pal.diverging_palette(pal.Reds9, pal.Greys9, n=18, midpoint=0.5) == pal.Reds9 + pal.Greys9[::-1]
def genColors(n, ptype='magma'): """ """ from bokeh.palettes import magma, inferno, plasma, viridis if ptype == 'magma': return magma(n) elif ptype == 'inferno': return inferno(n) elif ptype == 'plasma': return plasma(n) else: return viridis(n)
def handle_color(c, use_color_mapper): ''' Be able to automap colors using mapper, or take given rgb values (or maybe allow matplotlib color api as well?) ''' c = [float(x) for x in c] if type(c[0]) == int or type(c[0]) == float and use_color_mapper: #print("MAPPED") mapper = LinearColorMapper(palette=plasma(256), low=min(c), high=max(c)) return mapper
def plot_subsystems(model,time_data,compact=True): t = time_data.loc['t'] if compact: p = bp.figure(width=1000) else: p = bp.figure(width=1000, height = 800) subsystems = list(set((x.subsystem for x in model.reactions if x.subsystem is not None and x.subsystem))) all_enz = dict() for sub in subsystems: these_enzymes = get_enzymes_of_subsystem(model, sub) if len(these_enzymes) ==0: continue all_enz[sub] = get_prot_total(time_data, these_enzymes) data = pd.DataFrame.from_dict(all_enz, orient = 'columns') data = data.reindex(data.max().sort_values(ascending=False).index, axis=1) data.to_csv('tmp.csv') if compact: chosen_subs = data.columns[:10] colors = Category10[len(chosen_subs)] else: chosen_subs = data.columns colors = plasma(len(chosen_subs)) data = data[chosen_subs] data['t'] = t last_y = 0*t times = list(t) + list(t[::-1]) legend_it = [] for e,sub in enumerate(chosen_subs): this_y = data[sub] + last_y ys = list(this_y) + list(last_y[::-1]) c = p.patch(x = times, y = ys, color = colors[e])#, legend = sub) legend_it.append((sub, [c])) last_y = this_y p.legend.location = 'top_left' legend = Legend(items=legend_it[::-1], location=(0, 0)) p.add_layout(legend, 'right') return p
def update(): df,text = select_data() runner_select.options = [str(i) for i in set(df['Name'])] df['color'] = plasma(len(df['Name'])) #df[df['Name'].isin(runner_select.value)]['color']='#ff6600' #print df Comparison_Source.data = dict( gender = df['Gender'], name = df['Name'], division = df['Div'], gun_time = df['Gun_Seconds'], net_time = df['Net_Seconds'], color = df['color'], pace = df['Pace'] ) Text_Source.data=dict(text=[text])
def get_sequence_colors(seq): """Get colors for a sequence""" from bokeh.palettes import brewer, viridis, plasma from Bio.PDB.Polypeptide import aa1 pal = plasma(20) pal.append('white') aa1 = list(aa1) aa1.append('-') pcolors = {i: j for i, j in zip(aa1, pal)} text = list(seq) clrs = {'A': 'red', 'T': 'green', 'G': 'orange', 'C': 'blue', '-': 'white'} try: colors = [clrs[i] for i in text] except: colors = [pcolors[i] for i in text] return colors
def visualise(tweet_df): x = tweet_df['x_coord'].values y = tweet_df['y_coord'].values fav_count = tweet_df['tweet_favorite_count'].values sizes = 10*(1+(fav_count-fav_count.min())/fav_count.max()) desc = tweet_df['tweet_text'].values source = ColumnDataSource(data=dict(x=x, y=y, desc=desc, sizes = sizes)) hover = HoverTool(tooltips=[ ("index", "$index"), ("(x,y)", "(@x, @y)"), ('desc', '@desc'), ]) mapper = LinearColorMapper(palette=plasma(256), low=min(y), high=max(y)) p = figure(plot_width=1000, plot_height=600, tools=[hover], title="Scatter Plot") p.circle('x', 'y', size='sizes', source=source, fill_color=transform('y', mapper)) output_file('../output/plot.html') show(p)
def visualize(self): # add axes titles ''' # Seaborn viz ax = sns.scatterplot(x = 0, y = 1, hue = 'clus_label', data = self.pca_frame) plt.title('Example Plot') plt.xlabel('PCA1') plt.ylabel('PCA2') plt.show() ''' list_x = list(self.pca_frame[0].values) list_y = list(self.pca_frame[1].values) names = self.lookup.set_index(self.index_col) names = names.reindex(index = self.pca_frame.index) names.reset_index(inplace = True) # desc = list(self.pca_frame.index.values) desc = list(names[self.name_col].values) labels = list(self.pca_frame['clus_label'].values) source = ColumnDataSource(data=dict(x=list_x, y=list_y, desc=desc, color=labels)) hover = HoverTool(tooltips=[ # ("index", "$index"), # ("(PCA1, PCA2)", "(@x, @y)"), (self.index_col, '@desc'), ]) zoom = BoxZoomTool() pan = PanTool() wheel = WheelZoomTool() mapper = LinearColorMapper(palette=plasma(256), low=min(labels), high=max(labels)) # mapper = CategoricalColorMapper(palette=plasma(256), low=min(labels), high=max(labels)) p = figure(plot_width=1000, plot_height=600, tools=[hover, zoom, pan, wheel], title="Clustering Test: " + self.index_col) p.circle('x', 'y', size=10, source=source, color=transform('color', mapper)) # fill_color arg is okay but looks worse output_file('cluster_viz_' + self.index_col + '.html') show(p)
def process_color(self): logger.info("Processing Colors:") print("Processing Colors:") fill_color_list = plasma(len(self.config.bins_count)) fill_color_list.reverse() row_idx = 0 colours = [] for idx in range(0, self.df.shape[0]): count = self.df['count'][idx] for idx2 in range(0, len(self.config.bins_count) - 1): if count < self.config.bins_count[idx2]: colour_value = fill_color_list[idx2 - 1] break else: colour_value = fill_color_list[idx2] colours.append(colour_value) print_progress_to_console(row_idx) row_idx += 1 print("") self.df['color'] = colours
def simple_plot(data, x, y, trials): fig = set_figure(title="Simple Plot", y_axis_format="0.0000[00]") trial_vals = data[trials].unique() trial_names = [f"trial {trial_id}" for trial_id in trial_vals] if len(Colorblind8) < len(trial_vals): palette = plasma(len(trials)) else: palette = Colorblind8 for trial, color, legend in zip(trial_vals, palette, trial_names): df = data.loc[data[trials] == trial] fig.line(x=df[x], y=df[y], legend=legend, line_width=4, color=color, alpha=1) # additional settings fig.legend.click_policy = "hide" fig.legend.location = "top_left" return fig
from bokeh.palettes import plasma, small_palettes from bokeh.models import ( FixedTicker, Button, ColumnDataSource, PanTool, Scroll, RadioButtonGroup, RadioGroup, Arrow, NormalHead, HoverTool) from pysodium import crypto_sign_keypair from utils import bfs, randrange from swirld import Node R_COLORS = small_palettes['Greens'][9] shuffle(R_COLORS) def round_color(r): return R_COLORS[r % 9] I_COLORS = plasma(256) def idx_color(r): return I_COLORS[r % 256] class App: def __init__(self, n_nodes): self.i = 0 kps = [crypto_sign_keypair() for _ in range(n_nodes)] stake = {kp[0]: 1 for kp in kps} network = {} self.nodes = [Node(kp, network, n_nodes, stake) for kp in kps] for n in self.nodes: network[n.pk] = n.ask_sync self.ids = {kp[0]: i for i, kp in enumerate(kps)}
second_departures = ColumnDataSource(data=dict(hours=[], departures=[])) # Sexes men, women, others = csvReader.get_sexes(-1) sexes = [men, women, others] genders = ["Men", "Women", "Others"] sexes = list(map(int, sexes)) x = dict(zip(genders, sexes)) data = pd.Series(x).reset_index(name='value').rename(columns={'index': 'sex'}) data['angle'] = data['value'] / data['value'].sum() * 2 * pi data['color'] = ['#E5827E', '#F6A16C', '#F7E44E'] # END DATA SOURCES # Color mappers circle_mapper = linear_cmap(field_name='lat', palette=plasma(256), low=min(merc_x_list), high=max(merc_x_list)) segment_mapper = linear_cmap(field_name='x0', palette=plasma(256), low=min(merc_x_list), high=max(merc_x_list)) # Plot 1 (Map) p_map = figure(x_range=merc_x_range, y_range=merc_y_range, x_axis_type="mercator", y_axis_type="mercator", plot_width=1440, plot_height=1080, title="Station locations in New York",
def make_plot(df, corp, color_palette): palette_list = [ palettes.viridis(100), palettes.inferno(100), palettes.magma(100), palettes.plasma(100), ] colors = list(reversed(palette_list[color_palette])) mapper = LinearColorMapper(palette=colors, low=0, high=100) TOOLS = "hover,save,pan,box_zoom,reset,wheel_zoom" TOOLTIPS = """ <div> <div> <span style="font-size: 20px; font-weight: bold;">score: @c%</span> </div> <div> <span style="font-size: 16px; font-style: italic;">@a & @b</span> </div> </div> """ # tooltips=[('score','@c%'), ('doc_1', '@a'), ('doc_2', '@b')]) hm = figure( x_range=corp, y_range=list(reversed(corp)), x_axis_location="above", plot_width=900, plot_height=900, tools=TOOLS, toolbar_location="below", tooltips=TOOLTIPS, ) hm.grid.grid_line_color = None hm.axis.axis_line_color = None hm.axis.major_tick_line_color = None hm.axis.major_label_text_font_size = "8pt" hm.axis.major_label_standoff = 0 hm.xaxis.major_label_orientation = pi / 3 hm.rect( x="a", y="b", source=df, width=1, height=1, line_color="#ffffff", fill_color={ "field": "c", "transform": mapper }, ) color_bar = ColorBar( color_mapper=mapper, formatter=PrintfTickFormatter(format="%d%%"), major_label_text_font_size="10pt", label_standoff=10, border_line_color=None, location=(0, 0), ) hm.add_layout(color_bar, "right") js_resources = INLINE.render_js() css_resources = INLINE.render_css() script, div = components(hm) return js_resources, css_resources, script, div
try: freq[pwlen] = freq[pwlen] + 1 if maxh < freq[pwlen]: maxh = freq[pwlen] if maxlen < pwlen: maxlen = pwlen except: #don't plot outliers az = 0 #print "Discarding outlier " + pw #get an appropriate palette, and reverse the order cm = plasma(maxlen + 2) mcm = cm[::-1] source = ColumnDataSource(data=dict(lengths=pos, counts=freq, color=mcm)) p = figure(x_range=(0, len(freq)), y_range=(0, maxh), plot_height=600, title="Passwords by Length", toolbar_location=None, tools="") p.xaxis.axis_label = 'Length' p.yaxis.axis_label = 'Count' p.vbar(x='lengths',
legend = "Women",alpha=.3, size=10) plot_three .square(together[together['Gender']=='Male']['Gun_Seconds'], together[together['Gender']=='Male']['Net_Gun_Diff'], color='#0000ff',legend="Men",alpha=.3 ,size=10) ####PLOT FIVE#### COMPARE DIVISIONS WOMEN plot_five = figure(plot_width=425,plot_height=400) #plot_five = Scatter(females, x='Net_Seconds', y='Pace', color='Div', # title="Net Seconds v. Pace, colored by Division", legend="top_left", # xlabel="Net Seconds", ylabel="Pace",plot_width=900,plot_height=500) plot_five.xaxis.formatter = FuncTickFormatter.from_py_func(seconds_formatted) colors = dict(zip(set(females['Div']),plasma(len(set(females['Div']))))) colorsm = dict(zip(set(males['Div']),plasma(len(set(males['Div']))))) #print colors plot_five.xaxis.axis_label = 'Net Seconds' plot_five.yaxis.axis_label = 'Division' plot_five.title = "Women by Divsion" females['color'] = map(lambda i: colors[i], females['Div']) #females['Rank'] = females['Net_Seconds'].rank() plot_five.triangle(females['Net_Seconds'],females['Div'],color=females['color'],alpha=.3,size=10) #plot_five.square(males['Div'],males['Place'],color='#0000ff',legend="Men",alpha=.3,size=10) ####PLOT Six#### COMPARE DIVISIONS MALE plot_six = figure(plot_width=425,plot_height=400) colors = dict(zip(set(males['Div']),plasma(len(set(males['Div']))))) plot_six.title = "Men by Divsion"
def CircuitStats(race, y0, yf): """Creates a series of graphs with stats of the chosen race Using the data frame generates by the function Circuits, we use the Bokeh library to create a series of graphs with information about the chosen race. Arguments race -- The name of the race desired, Brazilian, French, Italian... y0 -- The first year of the time range yf -- The last year of the time range (inclusive) Example CircuitStats('Italian', 1950, 2017) -- Returns the informations about the Italian race from 1950 to 2017 (inclusive). """ df = Circuits(race, y0, yf) df["date"] = df.index df2 = df.loc[:, "first (constructor)"].value_counts().to_frame() df2["constructor"] = df2.index df2["temp"] = range(0, len(df2)) df2.set_index("temp", drop = True, inplace = True) df2["color"] = palettes.plasma(len(df2)) source = ColumnDataSource(df) source2 = ColumnDataSource(df2) output_file(race + ".html") title_str = "lap times for the " + race + " grand prix" p = figure(title = title_str, x_axis_label = 'year', y_axis_label = "seconds", x_axis_type = "datetime") tips1 = [("lap time", "@pole"), ("driver", "@{pole position}"), ("driver", "@{pole position (constructor)}")] tips2 = [("lap time", "@fastest"), ("driver", "@{fastest lap}"), ("constructor", "@{fastest lap (constructor)}")] p.line("date", "pole", line_color = "red", legend = "pole's time", source = source) c1 = p.circle("date", "pole", fill_color = "red", size = 8, source = source) c1_hover = HoverTool(renderers=[c1], tooltips=tips1) p.add_tools(c1_hover) p.line("date", "fastest", line_color = "blue", legend = "fastest lap time", source = source) c2 = p.circle("date", "fastest", fill_color = "blue", size = 8, source = source) c2_hover = HoverTool(renderers=[c2], tooltips=tips2) p.add_tools(c2_hover) q = figure(title = "Winners (constructor)", x_range = df2["constructor"]) q.xaxis.major_label_orientation = np.pi/3 q.vbar(x = "constructor", top = "first (constructor)", width = 0.9, legend = "Number of wins, by constructors", color = "color", source = source2) grid = gridplot([[p, q]]) show(grid) return None
from math import sin from random import random from bokeh.io import output_file, show from bokeh.models import ColumnDataSource, HoverTool, LinearColorMapper from bokeh.palettes import plasma from bokeh.plotting import figure from bokeh.transform import transform list_x = list(range(100)) list_y = [random() + sin(i / 20) for i in range(100)] desc = [str(i) for i in list_y] source = ColumnDataSource(data=dict(x=list_x, y=list_y, desc=desc)) hover = HoverTool(tooltips=[ ("index", "$index"), ("(x,y)", "(@x, @y)"), ('desc', '@desc'), ]) mapper = LinearColorMapper(palette=plasma( 256), low=min(list_y), high=max(list_y)) p = figure(plot_width=400, plot_height=400, tools=[hover], title="Belgian test") p.circle('x', 'y', size=10, source=source, fill_color=transform('y', mapper)) output_file('test.html') show(p)
Scroll, RadioButtonGroup, RadioGroup, Arrow, NormalHead, HoverTool) from pysodium import crypto_sign_keypair from utils import bfs, randrange from swirld import Node R_COLORS = small_palettes['Greens'][9] shuffle(R_COLORS) def round_color(r): return R_COLORS[r % 9] I_COLORS = plasma(256) def idx_color(r): return I_COLORS[r % 256] class App: def __init__(self, n_nodes): self.i = 0 kps = [crypto_sign_keypair() for _ in range(n_nodes)] stake = {kp[0]: 1 for kp in kps} network = {} self.nodes = [Node(kp, network, n_nodes, stake) for kp in kps] for n in self.nodes:
def get(self, locations): headers = {'Content-Type': 'text/html'} key = 'derp' if request.args: key = request.args.get('key', 0) wapi = wApi(key) wapi.set_granularity('hourly') locations = locations.split(';') temps_h = [] temps_d = [] def getForecastFromWeatherBit(location, granularity): wapi.set_granularity(granularity) forecast = wapi.get_forecast(city=location, units='I') if granularity == 'hourly': att = 'temp' else: att = 'max_temp' t = forecast.get_series([att]) dict_t = {} for num, temp in enumerate(t): if granularity == 'hourly' or num > 3: idx = datetime.fromtimestamp(datetime.timestamp(pytz.utc.localize(temp['datetime']))).replace(hour=0, minute=0, second=0, microsecond=0) else: if num <= 3: idx = datetime.fromtimestamp( datetime.timestamp( pytz.utc.localize( temp['datetime']))).replace(hour=0, minute=0, second=0, microsecond=0) #.replace(second=0, microsecond=0, minute=0, hour=temps_h[-1].iloc[-1].name.hour)+timedelta(hours = 2) ##hourly forecasts have been removed from weatherbit free tier dict_t.update({idx: { 'temp': temp[att] } }) df = pd.DataFrame.from_dict(dict_t, orient='index') return df for location in locations: # temps_h.append(getForecastFromWeatherBit(location,'hourly'))##hourly forecasts have been removed from weatherbit free tier # print(temps_h[-1].iloc[-1].name) temps_d.append(getForecastFromWeatherBit(location,'daily')[0:16]) # tempH = pd.concat(temps_h,axis=1) ##hourly forecasts have been removed from weatherbit free tier # tempH.columns = locations ##hourly forecasts have been removed from weatherbit free tier tempD = pd.concat(temps_d,axis=1) tempD.columns = locations colors = plasma(len(locations*2)) p = figure(width=950, height=600, x_axis_type="datetime") for num, location in enumerate(locations, start=0): # p.line(tempH.index.values, tempH[0:][location], legend=location, color=colors[num*2+1], line_width=5)##hourly forecasts have been removed from weatherbit free tier # p.circle(tempH.index.values, tempH[0:][location], color=colors[num*2],size=8)##hourly forecasts have been removed from weatherbit free tier p.line(tempD.index.values, tempD[0:][location], legend=location, color=colors[num*2+1], line_dash=[10,2], line_width=3) p.circle(tempD.index.values, tempD[0:][location], color=colors[num*2],size=8) p.xaxis.formatter=DatetimeTickFormatter( hours=["%I:00 %p"], days=["%A %m-%d"], months=["%A %m-%d"], years=["%A %m-%d"] ) p.xaxis[0].ticker.desired_num_ticks = len(tempD) ## len(tempH) ##hourly forecasts have been removed from weatherbit free tier p.xaxis.major_label_orientation = math.pi/4 p.xaxis.major_label_text_color = '#FFFFFF' p.yaxis.major_label_text_color = '#FFFFFF' p.xaxis.axis_line_color = '#FFFFFF' p.yaxis.axis_line_color = '#FFFFFF' p.xaxis.major_tick_line_color = '#FFFFFF' p.yaxis.major_tick_line_color = '#FFFFFF' p.yaxis.minor_tick_line_color = '#FFFFFF' p.xgrid.grid_line_color = '#333333' p.ygrid.grid_line_color = '#333333' p.toolbar.logo = None p.border_fill_color = '#191919' p.background_fill_color = '#191919' # p.border_fill_color = '#949494' # p.background_fill_color = "#949494" p.legend.background_fill_alpha = 0.7 # output_notebook(hide_banner=True) # show(p) script, div = components(p) return make_response(render_template('index.html', script=script, div=div))
def bokeh_averaged(self, whiskers=False): """ Comment """ if self.predictions is None: self.predict_crossvalidate() df = pd.DataFrame( np.array([[ int(nm.split('_')[0]) for nm in self.slave_dataset.index.values ], self.predictions, self.labels_df.values, self.slave_dataset.loc[:, 'temperature'].values]).T, columns=['ID', 'Predicted', 'Measured', 'Temperature']) cat_eles = self.slave_dataset.loc[:, 'Element Dictionary'] vals = [ ''.join('{}({})'.format(key, str(int(val))) for key, val in x) for x in cat_eles ] df['Name'] = vals tools = "pan,wheel_zoom,box_zoom,reset,save".split(',') hover = HoverTool(tooltips=[('Name', '@Name'), ("ID", "@ID"), ('T', '@Temperature')]) tools.append(hover) unique_temps = len(df['Temperature'].unique()) max_temp = df['Temperature'].max() min_temp = df['Temperature'].min() if max_temp == min_temp: df['color'] = pals.plasma(5)[4] else: pal = pals.plasma(unique_temps + 1) df['color'] = [ pal[i] for i in [ int(unique_temps * (float(x) - min_temp) / (max_temp - min_temp)) for x in df['Temperature'] ] ] unique_names = np.unique(df.loc[:, 'Name'].values) final_df = pd.DataFrame() for nm in unique_names: nmdf = df.loc[df.loc[:, 'Name'] == nm] unique_temp = np.unique(nmdf.loc[:, 'Temperature'].values) for temperature in unique_temp: tdf = nmdf.loc[nmdf.loc[:, 'Temperature'] == temperature] add_df = tdf.iloc[0, :].copy() add_df['Measured'] = tdf['Measured'].mean() add_df['Measured Standard Error'] = tdf['Measured'].sem() add_df['Upper'] = tdf['Measured'].mean() + tdf['Measured'].sem( ) add_df['Lower'] = tdf['Measured'].mean() - tdf['Measured'].sem( ) add_df['n Samples'] = tdf['Measured'].count() final_df = pd.concat([final_df, add_df], axis=1) df = final_df.transpose() p = figure(tools=tools, toolbar_location="above", logo="grey", plot_width=600, plot_height=600, title=self.svnm) p.x_range = Range1d(0, 1) p.y_range = Range1d(0, 1) p.background_fill_color = "#dddddd" p.xaxis.axis_label = "Predicted Conversion" p.yaxis.axis_label = "Measured Conversion" p.grid.grid_line_color = "white" source = ColumnDataSource(df) p.circle("Predicted", "Measured", size=8, source=source, color='color', line_color="black", fill_alpha=0.8) if whiskers: p.add_layout( Whisker(source=source, base="Predicted", upper="Upper", lower="Lower", level="overlay")) output_file("{}\\{}_avg.html".format(self.svfl, self.svnm), title="stats.py") save(p)
def scatter_plot_interactive(x, y, labels, colors=None, multi_color_names=None, title=None, x_label=None, y_label=None, plot_size=(600, 600), use_color_mapper=True, show_color_mouseover=False): ''' A 2D bokeh scatter plot with colors and labels so you can mouse over to see the label info. Parameters ---------- x : list of floats/ints x positions y : list of floats/ints y positions labels : list of strings Any textual info you want to be associated with each scatter plot point colors : list of floats/ints or list of lists of floats/ints If `multi_color_names` is `None` (no input provided) the colors must be a list of floats or ints. If `multi_color_names` is not none, then colors must be a list of the same length as multi_color_names, and contain lists of floats/ints (of the same length as x and y) multi_color_names : `None` or list of strings : optional If No input provided, or None, then this does nothing. But if it is not none, it must be a list of strings. This triggers a special js function which will add a selection box above the plot. The selection box is a dropdown menu with elements, the memebers of which are the strings/elements of this list. title : str : optional The title provided to the plot x_label : str : optional The label for the x axis y_label : str : optional the label for the y axis plot_size : tuple of ints : optional Passing a tuple of two ints will determine the size of the plot. The default is `(600, 600)` which coorsponds to width and heigth respectively. use_color_mapper : bool : optional A boolean to indicate whether or not to remap the provided colors (Not currently functional) show_color_mouseover : bool : optional A boolean, if true the color value will be displayed next to the point when the point is moused over Returns ------- p : bokeh plot object The plot that has been built from the input fields. Will need to be passed into `show` to display Notes ----- This function is still in development and needs to break more often and more specifically. But hopefully it still makes it easy to bring in basic yet informative scatter plots into notebooks for data exploration. ''' def handle_color(c, use_color_mapper): ''' Be able to automap colors using mapper, or take given rgb values (or maybe allow matplotlib color api as well?) ''' c = [float(x) for x in c] if type(c[0]) == int or type(c[0]) == float and use_color_mapper: #print("MAPPED") mapper = LinearColorMapper(palette=plasma(256), low=min(c), high=max(c)) return mapper def normalize_colors(c): ''' map every float in colors(a list of colors) to a 0-1 range ''' print(min(c), max(c)) if min(c) == max(c): return [0 for x in c] nc = [(i - min(c)) / (max(c) - min(c)) for i in c] return nc # # Check if plot size is defined assert type(plot_size) == tuple assert len(plot_size) == 2 assert type(plot_size[0]) == int assert type(plot_size[1]) == int # defined_size = True # # TODO: Add error info # else: # plot_size = (600,600) # Handle color options if not isinstance(multi_color_names, type(None)): # Test assertions # . . . # Assign colors color_d = {} for i in range(len(multi_color_names)): colors[i] = normalize_colors(colors[i]) c = colors[i] cname = multi_color_names[i] color_d[ cname] = c #transform('colors', handle_color(c, use_color_mapper)) select = Select(title="Coloring:", value=multi_color_names[0], options=multi_color_names) #select.js data = dict(x=x, y=y, labels=labels, color=colors[multi_color_names.index(select.value)]) for key in color_d: data['c_' + key] = color_d[ key] #transform(color_d[key], handle_color(color_d[key], use_color_mapper)) source = ColumnDataSource(data) #source.data['multi_color_namess'] = color_d else: colors = [float(x) for x in colors] if type(colors[0]) == int or type( colors[0]) == float and use_color_mapper: #print("MAPPED") mapper = LinearColorMapper(palette=plasma(256), low=min(colors), high=max(colors)) #print(type(colors[0])) source = ColumnDataSource( data=dict(x=x, y=y, labels=labels, color=colors)) tooltips = [ ("index", "$index"), ("(x,y)", "(@x, @y)"), ('labels', '@labels'), ] if show_color_mouseover: tooltips.append(('color', '@color')) hover = HoverTool(tooltips=tooltips) #print(hover) p = figure(plot_width=plot_size[0], plot_height=plot_size[1], tools=[ hover, TapTool(), BoxSelectTool(), BoxZoomTool(), ResetTool(), PanTool() ], title=title, x_axis_label=x_label, y_axis_label=y_label) callback = CustomJS(args=dict(source=source, p=p), code=""" var data = source.data; var f = cb_obj.value; var color = data['c_'+f]; data['color'] = color; source.change.emit(); p.change.emit(); """) #select.on_change(p.circle('x', 'y', size=10,source=source)) if not isinstance(multi_color_names, type(None)): #print(select.value) #print(color_d[select.value]) select.js_on_change('value', callback) c = transform('color', handle_color(color_d[select.value], use_color_mapper)) p.circle('x', 'y', size=10, source=source, fill_color=c) layout = column(select, p) return layout else: #p.add_tools(HoverTool(tooltips=hover), TapTool(), BoxSelectTool(), BoxZoomTool(), ResetTool(), PanTool()) p.circle('x', 'y', size=10, source=source, fill_color=transform('color', mapper)) return p
def visualise_clusters_umap(data): print("[INFO] preparing UMAP visualisation") encodings = data['face_encoding'].tolist() reducer = umap.UMAP(n_neighbors=2, min_dist=0.2, metric='euclidean', random_state=42).fit(encodings) embedding = reducer.transform(encodings) df = pd.DataFrame(embedding, columns=('x', 'y')) df['class'] = data['name'] df['image'] = data['image'] df['clusterID'] = [str(x + 1) for x in list(data['HDBSCAN_clusters'])] df['probability'] = data['probability'] df['prediction'] = data['prediction'] n = len(list(np.unique(data['HDBSCAN_clusters']))) if n < 255: pal = plasma(n) else: pal = Plasma256 datasource = ColumnDataSource(df) color_mapping = CategoricalColorMapper(factors=[ str(x + 1) for x in list(np.unique(data['HDBSCAN_clusters'])) ], palette=pal) plot_figure = figure(title='UMAP projection', plot_width=600, plot_height=600, tools=('pan, wheel_zoom, reset')) plot_figure.add_tools( HoverTool(tooltips=""" <div> <div> <img src='@image' style='float:left; width:100px;height:100px; margin: 5px 5px 5px 5px'/> </div> <div> <span style='font-size: 12px'>@class</span> </div> <div> <span style='font-size: 12px'>@clusterID</span> </div> <div> <span style='font-size: 12px'>@probability</span> </div> <div> <span style='font-size: 12px'>@prediction</span> </div> </div> """)) plot_figure.circle('x', 'y', source=datasource, color=dict(field='clusterID', transform=color_mapping), line_alpha=0.6, fill_alpha=0.6, size=4) # output_notebook() output_file("data/umap_clusters.html", title="FAME results: UMAP clusters", mode='inline') # show(plot_figure) save(plot_figure)
try: freq[pwlen] = freq[pwlen] + 1 if maxh < freq[pwlen]: maxh = freq[pwlen] if maxlen < pwlen: maxlen = pwlen except: #don't plot outliers az = 0 #print "Discarding outlier " + pw #get an appropriate palette, and reverse the order cm = plasma(maxlen) mcm = cm[::-1] source = ColumnDataSource(data=dict(lengths=pos, counts=freq, color=mcm)) p = figure(x_range=(0, len(freq)), y_range=(0, maxh), plot_height=600, title="Passwords by Length", toolbar_location=None, tools="") p.xaxis.axis_label = 'Length' p.yaxis.axis_label = 'Count' p.vbar(x='lengths',