def __init__(self, signal_label, title='ECG', min_range=-1, max_range=1, plot_width=425, plot_height=200): self.data_queue = queue.Queue(maxsize=1000) self.ts_queue = queue.Queue(maxsize=1000) self.sig_label = signal_label self.ecg_patient_id = None self.ecg_maxLength = 600 self.ecg_time = 0 self.ecg_xdata = [] self.ecg_ydata = [] #self.ecg_xdata = collections.deque(maxlen=self.ecg_maxLength) #self.ecg_ydata = collections.deque(maxlen=self.ecg_maxLength) self.ecgFig = figure(plot_width=plot_width, plot_height=plot_height, toolbar_location=None) self.ecgFig.title.text = title self.ecgFig.title.align = "center" self.ecgFig.outline_line_width = 1 self.ecgFig.outline_line_color = "black" self.ecgFig.y_range = Range1d(start=min_range, end=max_range) self.ecgFig.ygrid.grid_line_color = "red" self.ecgFig.ygrid.grid_line_alpha = 0.3 self.ecgFig.xgrid.grid_line_color = "red" self.ecgFig.xgrid.grid_line_alpha = 0.3 self.ecgFig.ygrid.minor_grid_line_color = 'red' self.ecgFig.ygrid.minor_grid_line_alpha = 0.1 self.ecgFig.xgrid.minor_grid_line_color = 'red' self.ecgFig.xgrid.minor_grid_line_alpha = 0.1 self.ecgFig.xaxis.major_label_text_font_size = '0pt' ## at a frequency of 100Hz, every 20 point is equal to 0.2 sec ## and ECG chart has a major tick every 0.2 sec, with minor ## ticks every 0.04 sec (5 minor ticks per major tick) xticker = BasicTicker() xticker.desired_num_ticks = int(self.ecg_maxLength / 20) xticker.num_minor_ticks = 5 self.ecgFig.xgrid.ticker = xticker self.ecgGraph = self.ecgFig.line(self.ecg_xdata, self.ecg_ydata, line_width=1, line_color="black") self.initialData = True self.period_value = 0 self.initValue = 0 self.gain = 1
def create_confusion_matrix(df): """Crea tabla de matriz de confusión. Parameters: data_dict (dict): Diccionario con los datos a mostrar en la visualización. Returns: Figure: Gráfica de importancia de predictores. """ # Tranformar el DataFrame en un stack data_dict = df.stack().rename("value").reset_index() # Paleta de colores # colors = ['#f7fbff','#deebf7','#c6dbef','#9ecae1','#6baed6','#4292c6','#2171b5','#08519c','#08306b'] colors = [ '#f7fbff', '#deebf7', '#c6dbef', '#9ecae1', '#6baed6', '#4292c6', '#2171b5', '#08519c' ] # Had a specific mapper to map color with value mapper = LinearColorMapper(palette=colors, low=data_dict.value.min(), high=data_dict.value.max()) # Define a figure p = figure(plot_height=270, x_range=list(data_dict.Actual.drop_duplicates()), y_range=list(reversed(data_dict.Prediction.drop_duplicates())), toolbar_location=None, tools="", x_axis_location="above", x_axis_label="Actual Label", y_axis_label="Predicted Label", sizing_mode='stretch_width', output_backend="webgl") p.xaxis.axis_line_color = None p.yaxis.axis_line_color = None p.xaxis.major_label_orientation = np.pi / 4 # Create rectangle for heatmap p.rect(x="Actual", y="Prediction", width=1, height=1, source=ColumnDataSource(data_dict), line_color=None, fill_color=transform('value', mapper)) p.text(x="Actual", y="Prediction", text='value', text_align="center", text_baseline="middle", source=ColumnDataSource(data_dict)) p.border_fill_color = bokeh_utils.BACKGROUND_COLOR p.background_fill_color = bokeh_utils.BACKGROUND_COLOR # Add legend color_bar = ColorBar(color_mapper=mapper, location=(0, 0), ticker=BasicTicker(desired_num_ticks=len(colors))) color_bar.background_fill_color = bokeh_utils.BACKGROUND_COLOR p.add_layout(color_bar, 'right') return p
def plot_stand_hour(apple_watch): """ Generate grid heat map of stand hour labels for a given hour and date, grouped by start timestamp. :param apple_watch: instance of apple watch data object :return: None """ logger.info('Loading and Generating Stand Hour Heat Map') df = apple_watch.load_stand_hour_data() df = df[(df['start_timestamp'] > START_DATE) & (df['start_timestamp'] < END_DATE)] df['date'] = list( map(lambda d: d.strftime('%m/%d/%y'), df['start_timestamp'])) df['hour'] = list( map(lambda d: int(d.strftime('%H')), df['start_timestamp'])) df['stand_hour'] = list( map(lambda label: 1 if label == 'Stood' else 0, df['stand_hour'])) dates = df['date'].unique() # create heat map of hourly counts grouped by date # follow example from https://bokeh.pydata.org/en/latest/docs/gallery/unemployment.html TOOLS = "hover,save,pan,box_zoom,reset,wheel_zoom" colors = ["grey", "green"] mapper = LinearColorMapper(palette=colors) source = ColumnDataSource(df) plot = figure(title="Apple Watch Stand Hour Data", x_range=[str(h) for h in range(24)], y_range=list(dates), x_axis_location="below", plot_width=1000, plot_height=600, tools=TOOLS, toolbar_location='above', sizing_mode='scale_both') plot.grid.grid_line_color = None plot.axis.axis_line_color = None plot.axis.major_tick_line_color = None plot.axis.major_tick_line_dash_offset = -1 plot.xaxis.axis_label_text_font_size = '14pt' plot.xaxis.major_label_text_font_size = '12pt' plot.yaxis.axis_label_text_font_size = '14pt' plot.yaxis.major_label_text_font_size = '12pt' plot.title.text_font_size = '16pt' plot.rect(x='hour', y='date', width=1, height=1, source=source, fill_color={ 'field': 'stand_hour', 'transform': mapper }, line_color=None) color_bar = ColorBar(color_mapper=mapper, major_label_text_font_size="10pt", ticker=BasicTicker(desired_num_ticks=len(colors)), label_standoff=6, border_line_color=None, location=(0, 0)) plot.add_layout(color_bar, 'right') plot.select_one(HoverTool).tooltips = [('date', '@date'), ('hour', '@hour'), ('value', '@stand_hour')] if SHOW_PLOTS: show(plot, browser='chrome') save_plot(plot, 'stand_hour') # clear output mode for next plot reset_output() # save dataframe df.to_csv('apple_watch_data/stand_hour.csv', index=False)
def plot_el_visualization(sequences_flanked): output = pd.DataFrame(index = ['A','C','G','T'] , columns = [i+1 for i in range(80)]) sequences_unflanked = population_remove_flank(sequences_flanked) el_list , el= get_ordered_snpdev([sequences_flanked[0]]) #only the first element s = sequences_unflanked[0] loc_list = get_map(s) el_map = dict(zip(loc_list, el_list)) for i in el_map : output.loc[i] = el_map[i] output = output.fillna(el[0]) output.columns = [ i for i in s] cmap_list = plt.colormaps() #with st.beta_container() : #select_cmap = st.beta_expander('Expression', expanded=True) #with select_cmap : # cmap = st.selectbox('Please select your preferred colormap', cmap_list , index = 18) ###Plot with Bokeh ###Bokeh imports from math import pi from bokeh.io import show from bokeh.models import BasicTicker, ColorBar, LinearColorMapper, PrintfTickFormatter from bokeh.plotting import figure ### output.columns = output.columns +[str(i+1) for i in range(len(output.columns))] output.columns.name = 'sequence' output.index.name = 'base' df = pd.DataFrame(output.stack(), columns=['Expression']).reset_index() df.columns = ['base' , 'position' , 'Expression'] ### maxima=df.loc[df.Expression.idxmax()] maxima.name='Max' minima=df.loc[df.Expression.idxmin()] minima.name='Min' ### # this is the colormap from the original NYTimes plot colors = ["#75968f", "#a5bab7", "#c9d9d3", "#e2e2e2", "#dfccce", "#ddb7b1", "#cc7878", "#933b41", "#550b1d"] #bokeh.palettes.__palettes__ palette = sns.color_palette("Spectral_r" , n_colors = 256 ).as_hex()#'Blues256' if cmap_range =="Absolute" : mapper = LinearColorMapper(palette= palette, low=3, high=16) if cmap_range == 'Relative' : mapper = LinearColorMapper(palette=palette, low=df.Expression.min(), high=df.Expression.max()) TOOLS = "hover,save,pan,box_zoom,reset,wheel_zoom,tap" p = figure( x_range=list(output.columns), y_range=list(output.index.values), x_axis_location="above", plot_width=1000, plot_height=200, #1000,200 tools=TOOLS, toolbar_location='below', tooltips=[('Mutation', '@position@base'), ('Expression', '@Expression')]) p.grid.grid_line_color = None p.axis.axis_line_color = None p.axis.major_tick_line_color = None p.axis.major_label_text_font_size = "9px" p.axis.major_label_standoff = 0 p.xaxis.major_label_orientation = pi / 3 ### Shade max and min base_y_dict = {'A':0 , 'C' : 1 , 'G' : 2, 'T' : 3} max_x = int(re.split('(\d+)',str(maxima['position']))[1])-1 max_y = base_y_dict[maxima['base']] min_x = int(re.split('(\d+)',str(minima['position']))[1])-1 min_y = base_y_dict[minima['base']] from bokeh.models import BoxAnnotation line_color = 'black' box_max = BoxAnnotation(left=max_x, right=max_x+1, bottom = max_y, top = max_y+1 , line_dash = "solid" , line_width = 2 , line_color = line_color,line_alpha =1 , fill_alpha=0) p.add_layout(box_max) box_min = BoxAnnotation(left=min_x, right=min_x+1, bottom = min_y, top = min_y+1 , line_dash = "dotted" , line_width = 2 , line_color = line_color, line_alpha = 1, fill_alpha=0) p.add_layout(box_min) source = ColumnDataSource(df) tmp_download_link = download_link(output, 'output.csv', 'Click here to download the results as a CSV') renderer= p.rect(x="position", y="base", width=1, height=1, source=source, fill_color={'field': 'Expression', 'transform': mapper}, line_color=None, ) #renderer.nonselection_glyph = Null #p.add_glyph( ) color_bar = ColorBar(color_mapper=mapper, major_label_text_font_size="9px", ticker=BasicTicker(desired_num_ticks=len(colors)), formatter=PrintfTickFormatter(format="%d"), label_standoff=6, border_line_color=None, location=(0,0)) p.add_layout(color_bar, 'right') p.output_backend="svg" #st.bokeh_chart(p , use_container_width=0) # show the plot return s,tmp_download_link,maxima,minima,df,source,p
def make_plot(src, src_img): """Create a figure object to host the plot. """ fig = figure( plot_width=800, plot_height=400, title='Convergence', x_axis_label='n', y_axis_label='', ) fig.line( 'n_samples', 'means', source=src, line_color='blue', alpha=0.8, legend='Empirical average', ) fig.line( 'n_samples', 0.0, source=src, line_color='black', line_dash='dashed', alpha=0.5, legend='True mean', ) band = Band(base='n_samples', lower='lower', upper='upper', source=src, level='underlay', fill_alpha=0.15, fill_color='red', line_width=1, line_color='black', ) fig.add_layout(band) fig.legend.click_policy = 'hide' fig.legend.location = 'top_right' fig_img = figure( plot_width=500, plot_height=500, title='Correlation matrix', ) color_mapper = LinearColorMapper( palette='Viridis256', low=0.0, high=1.0, ) fig_img.image(image='image', source=src_img, x=0, y=0, dw=1, dh=1, color_mapper=color_mapper, ) fig_img.axis.visible = False color_bar = ColorBar(color_mapper=color_mapper, ticker=BasicTicker(), label_standoff=12, border_line_color=None, location=(0, 0), ) fig_img.add_layout(color_bar, 'right') return fig, fig_img
def Intensity_Plot(data, TOOLS): dataI = data columns = ['e1', 'e2', 'e3', 'pASite', 'e4'] array_1 = [] for k in range(len(dataI)): if (k + 6 >= len(dataI)): array_1.append("N/A") else: s = "" list1 = [] for l in range(k, k + 6): list1.append(dataI.Base[l]) s = s.join(list1) array_1.append(s) sequences = [] for n in range(len(dataI) * len(columns)): sequences.append(n) temporary = 0 for q in range(len(array_1)): for d in range(len(columns)): sequences[temporary] = array_1[q] temporary = temporary + 1 Sequences = pd.DataFrame(sequences, columns=['Sequence']) dataI['Position'] = dataI['Position'].astype(str) dataI = dataI.set_index('Position') dataI = dataI.drop(columns=['Base']) dataI.columns.name = 'Values' Position = list(dataI.index) Position = list(map(int, Position)) Values = list(dataI.columns) df_I = pd.DataFrame(dataI.stack(), columns=['score']).reset_index() df_complete = pd.concat([df_I, Sequences], axis=1) Magma256.reverse() mapper = LinearColorMapper(palette=Magma256, high=df_complete.score.max(), low=df_complete.score.min()) p = figure(title="Heatmap of Sites", x_range=(0, len(Position)), y_range=Values, x_axis_location="above", sizing_mode='stretch_both', tools=TOOLS, toolbar_location='below', tooltips=[('Position', '@Position'), ('Score', '@score'), ('Sequence', '@Sequence')]) p.grid.grid_line_color = None p.axis.axis_line_color = None p.axis.major_tick_line_color = None p.axis.major_label_text_font_size = "10pt" p.axis.major_label_standoff = 0 p.xaxis.major_label_orientation = pi / 3 p.rect(x='Position', y='Values', width=1, height=1, source=df_complete, fill_color={ 'field': 'score', 'transform': mapper }, line_color=None) color_bar = ColorBar(color_mapper=mapper, major_label_text_font_size="10pt", ticker=BasicTicker(), label_standoff=6, border_line_color=None, location=(0, 0)) p.add_layout(color_bar, 'right') script_heat, div_heat = components(p) return (p)
def heat(): topics = topics_df.topic.unique() checked = request.form.getlist('checkbox') type = request.form.get('law') if type == 'Became Law': df = topics_df[topics_df.track == 'Became Law'] else: df = topics_df if checked: mask = eval('|'.join([f"(df.topic=='{i}')" for i in checked])) time_df = df[mask].groupby([df['date'].dt.strftime('%Y-%m'), 'topic']).size().unstack(fill_value=0) else: time_df = df.groupby([df['date'].dt.strftime('%Y-%m'), 'topic']).size().unstack(fill_value=0) time_df.index.name = 'Months' time_df.columns.name = 'Topics' time_df = time_df.stack().rename("value").reset_index() colors = [ '#000000', '#330000', '#6F0606', '#C32B2B', '#F31E1E', '#FF9406', '#FC9207', '#F5C88D', '#FFFF01', '#FAF44B', '#F0F974', '#DCDCC8', '#F7F7EB' ] mapper = LinearColorMapper(palette=colors, low=time_df.value.min(), high=time_df.value.max()) TOOLS = "hover,save,pan,box_zoom,reset,wheel_zoom" p = figure(plot_width=1000, plot_height=500, x_range=list(time_df.Months.drop_duplicates()), y_range=list(time_df.Topics.drop_duplicates()), tools=TOOLS, toolbar_location='below', tooltips=[('Month', '@Months'), ('Bills', '@value')], x_axis_location="below") # Create rectangle for heatmap p.rect(x="Months", y="Topics", width=1, height=1, source=ColumnDataSource(time_df), line_color=None, fill_color=transform('value', mapper)) # Add legend color_bar = ColorBar(color_mapper=mapper, location=(0, 0), ticker=BasicTicker(desired_num_ticks=len(colors))) p.background_fill_color = '#DEDDDA' p.border_fill_color = '#DEDDDA' p.add_layout(color_bar, 'right') p.xaxis.major_label_orientation = 0.8 javascript, div = components(p) return render_template('heatmap.html', topics=topics, javascript=javascript, div=div)
def make_heatmap(data, title, outfile): """ Create a heatmap for a distance DataFrame @args: - the DataFrame - a title for the plot - a path for the output file *.html @output: - the heatmap is stored in the outfile """ output_file(outfile, title=title) l1 = list(data.columns) l2 = list(data.index) # reshaping df = pandas.DataFrame(data.stack(), columns=['dist']).reset_index() df = df.fillna(0) # color setting colormap = cm.get_cmap("BuPu") colors = [mpl.colors.rgb2hex(m) for m in colormap(np.arange(colormap.N))] mapper = LinearColorMapper(palette=colors, low=df.dist.min(), high=df.dist.max()) source = ColumnDataSource(df) TOOLS = "hover,save,pan,box_zoom,reset,wheel_zoom" p = figure( title=title, x_range=l1, y_range=sorted(l2, reverse=True), # x_axis_location="above", plot_width=2000, plot_height=1700, # lang # x_axis_location="above", plot_width=3500, plot_height=3000, # treebanks # x_axis_location="above", plot_width=1200, plot_height=900, # treebanks romanes x_axis_location="above", plot_width=800, plot_height=600, # langs romanes tools=TOOLS, toolbar_location='below') p.grid.grid_line_color = None p.axis.axis_line_color = None p.axis.major_tick_line_color = None p.axis.major_label_text_font_size = "15pt" p.axis.major_label_standoff = 0 p.xaxis.major_label_orientation = pi / 3 p.rect(x="level_1", y="level_0", width=1, height=1, source=source, fill_color={ 'field': 'dist', 'transform': mapper }, line_color=None) color_bar = ColorBar(color_mapper=mapper, major_label_text_font_size="15pt", ticker=BasicTicker(desired_num_ticks=10), label_standoff=8, border_line_color=None, location=(0, 0)) p.add_layout(color_bar, 'right') p.select_one(HoverTool).tooltips = [ ('l1', '@level_0'), ('l2', '@level_1'), ('distance', '@dist'), ] # automatically opens show(p) print("done..")
def mark_gaia_sources(self): """Mark Gaia sources within a given TPF or postcard. Hover over the points to reveal the source's TIC ID, Gaia ID, Tmag, and Gmag. Also crossmatches with TIC and identifies closest TIC object. """ from bokeh.models import (ColumnDataSource, HoverTool, BasicTicker, Slider, Button, Label, LinearColorMapper, Span, ColorBar) from bokeh.plotting import figure, show, output_file from bokeh.palettes import Viridis256 def create_labels(gaia_ra, gaia_dec): ticLabel, tmagLabel = np.zeros(len(gaia_id)), np.zeros( len(gaia_id)) for i in range(len(gaia_ra)): tic, tmag, sep = tic_from_coords([gaia_ra[i], gaia_dec[i]]) if sep < 1.0: ticLabel[i] = tic tmagLabel[i] = tmag[0] return ticLabel, tmagLabel center = [self.header['CEN_RA'], self.header['CEN_DEC']] center_xy = WCS(self.header).all_world2pix(center[0], center[1], 1) sources = cone_search(pos=center, r=0.5, service='Mast.Catalogs.GaiaDR2.Cone') sources_xy = WCS(self.header).all_world2pix(sources['ra'], sources['dec'], 1) new_coords = [] for i in range(len(sources_xy[0])): xy = [sources_xy[0][i], sources_xy[1][i]] coords_corr = use_pointing_model(xy, self.pointing_model[0]) new_coords.append([coords_corr[0][0], coords_corr[0][1]]) new_coords = np.array(new_coords) # Finds sources that lie in the frame in_frame = np.where( (new_coords[:, 0] >= center_xy[0] - self.obj.dimensions[1] / 2.) & (new_coords[:, 0] <= center_xy[0] + self.obj.dimensions[1] / 2.) & (new_coords[:, 1] >= center_xy[1] - self.obj.dimensions[2] / 2.) & (new_coords[:, 1] <= center_xy[1] + self.obj.dimensions[2] / 2.)) gaia_pos_x = new_coords[:, 1][in_frame] - center_xy[ 1] + self.obj.dimensions[1] / 2. gaia_pos_y = new_coords[:, 0][in_frame] - center_xy[ 0] + self.obj.dimensions[2] / 2. gaia_g_mag = sources['phot_g_mean_mag'][in_frame] gaia_id = sources['source_id'][in_frame] ticLabel, tmagLabel = create_labels(sources['ra'][in_frame], sources['dec'][in_frame]) # Creates hover label hover = HoverTool() hover.tooltips = [('TIC', ''), ('T_mag', ''), ('Gaia ID', ''), ('G_mag', '')] x_range = (-0.5, self.obj.dimensions[1] - 0.5) y_range = (-0.5, self.obj.dimensions[2] - 0.5) p = figure(x_range=x_range, y_range=y_range, toolbar_location='above', plot_width=500, plot_height=450) color_mapper = LinearColorMapper(palette='Viridis256', low=np.min(self.flux[0]), high=np.max(self.flux[0])) tpf_img = p.image(image=[self.flux[0]], x=-0.5, y=-0.5, dw=self.obj.dimensions[1], dh=self.obj.dimensions[2], color_mapper=color_mapper) # Sets the placement of the tick labels p.xaxis.ticker = np.arange(0, self.obj.dimensions[1]) p.yaxis.ticker = np.arange(0, self.obj.dimensions[2]) # Sets the tick labels x_overrides, y_overrides = {}, {} x_list = np.arange(int(center_xy[0] - self.obj.dimensions[1] / 2), int(center_xy[0] + self.obj.dimensions[1] / 2), 1) y_list = np.arange(int(center_xy[1] - self.obj.dimensions[2] / 2), int(center_xy[1] + self.obj.dimensions[2] / 2), 1) for i in range(9): ind = str(i) x_overrides[i] = str(x_list[i]) y_overrides[i] = str(y_list[i]) p.xaxis.major_label_overrides = x_overrides p.yaxis.major_label_overrides = y_overrides p.xaxis.axis_label = 'Pixel Column Number' p.yaxis.axis_label = 'Pixel Row Number' # Sets the color bar color_bar = ColorBar(color_mapper=color_mapper, location=(0, 0), border_line_color=None, ticker=BasicTicker(), title='Intensity', title_text_align='left') p.add_layout(color_bar, 'right') # Adds points onto image source = ColumnDataSource(data=dict(x=gaia_pos_x, y=gaia_pos_y, tic=ticLabel, tmag=tmagLabel, gaia=gaia_id, gmag=gaia_g_mag)) s = p.circle('x', 'y', size=8, source=source, line_color=None, selection_color='red', nonselection_fill_alpha=0.0, nonselection_line_alpha=0.0, nonselection_line_color=None, fill_color='black', hover_alpha=0.9, hover_line_color='white') # Activates hover feature p.add_tools( HoverTool(tooltips=[("TIC ID", "@tic"), ("TESS Tmag", "@tmag"), ("Gaia ID", "@gaia"), ("Gaia Gmag", "@gmag")], renderers=[s], mode='mouse', point_policy='snap_to_data')) show(p) return
plot.add_tools( PanTool(), BoxZoomTool(), WheelZoomTool(maintain_focus=False), SaveTool(), ResetTool(), ) plot.add_layout( LinearAxis(axis_label="pulse_id", formatter=BasicTickFormatter(use_scientific=False)), place="below", ) plot.add_layout(LinearAxis(axis_label="Hitrate"), place="left") plot.add_layout(Grid(dimension=0, ticker=BasicTicker())) plot.add_layout(Grid(dimension=1, ticker=BasicTicker())) step_fast_source = ColumnDataSource(dict(x=[], y=[])) step_fast = plot.add_glyph( step_fast_source, Step(x="x", y="y", mode="after", line_color="steelblue", line_width=2)) step_fast_lon_source = ColumnDataSource(dict(x=[], y=[])) step_fast_lon = plot.add_glyph( step_fast_lon_source, Step(x="x", y="y", mode="after", line_color="steelblue", line_width=2,
def cloud(full_output): """ Plotting the cloud input from ``picaso``. The plot itselfs creates maps of the wavelength dependent single scattering albedo and cloud opacity as a function of altitude. Parameters ---------- full_output Returns ------- A row of two bokeh plots with the single scattering and optical depth map """ cols = colfun1(200) color_mapper = LinearColorMapper(palette=cols, low=0, high=1) dat01 = full_output['layer']['cloud'] #PLOT W0 scat01 = np.flip(dat01['w0'], 0) #[0:10,:] xr, yr = scat01.shape f01a = figure(x_range=[0, yr], y_range=[0, xr], x_axis_label='Wavelength (micron)', y_axis_label='Pressure (bar)', title="Single Scattering Albedo", plot_width=300, plot_height=300) f01a.image(image=[scat01], color_mapper=color_mapper, x=0, y=0, dh=xr, dw=yr) color_bar = ColorBar( color_mapper=color_mapper, #ticker=LogTicker(), label_standoff=12, border_line_color=None, location=(0, 0)) f01a.add_layout(color_bar, 'left') #PLOT OPD scat01 = np.flip(dat01['opd'] + 1e-60, 0) xr, yr = scat01.shape cols = colfun2(200)[::-1] color_mapper = LogColorMapper(palette=cols, low=1e-3, high=10) f01 = figure(x_range=[0, yr], y_range=[0, xr], x_axis_label='Wavelength (micron)', y_axis_label='Pressure (bar)', title="Cloud Optical Depth Per Layer", plot_width=300, plot_height=300) f01.image(image=[scat01], color_mapper=color_mapper, x=0, y=0, dh=xr, dw=yr) color_bar = ColorBar(color_mapper=color_mapper, ticker=LogTicker(), label_standoff=12, border_line_color=None, location=(0, 0)) f01.add_layout(color_bar, 'left') #PLOT G0 scat01 = np.flip(dat01['g0'] + 1e-60, 0) xr, yr = scat01.shape cols = colfun3(200)[::-1] color_mapper = LinearColorMapper(palette=cols, low=0, high=1) f01b = figure(x_range=[0, yr], y_range=[0, xr], x_axis_label='Wavelength (micron)', y_axis_label='Pressure (bar)', title="Assymetry Parameter", plot_width=300, plot_height=300) f01b.image(image=[scat01], color_mapper=color_mapper, x=0, y=0, dh=xr, dw=yr) color_bar = ColorBar(color_mapper=color_mapper, ticker=BasicTicker(), label_standoff=12, border_line_color=None, location=(0, 0)) f01b.add_layout(color_bar, 'left') #CHANGE X AND Y AXIS TO BE PHYSICAL UNITS #indexes for pressure plot pressure = [ "{:.1E}".format(i) for i in full_output['layer']['pressure'][::-1] ] #flip since we are also flipping matrices wave = ["{:.2F}".format(i) for i in 1e4 / full_output['wavenumber']] nwave = len(wave) npres = len(pressure) iwave = np.array(range(nwave)) ipres = np.array(range(npres)) #set how many we actually want to put on the figure #hard code ten on each.. iwave = iwave[::int(nwave / 10)] ipres = ipres[::int(npres / 10)] pressure = pressure[::int(npres / 10)] wave = wave[::int(nwave / 10)] #create dictionary for tick marks ptick = {int(i): j for i, j in zip(ipres, pressure)} wtick = {int(i): j for i, j in zip(iwave, wave)} for i in [f01a, f01, f01b]: i.xaxis.ticker = iwave i.yaxis.ticker = ipres i.xaxis.major_label_overrides = wtick i.yaxis.major_label_overrides = ptick return row(f01a, f01, f01b)
def plot_cld_input(nwno, nlayer, filename=None, df=None, pressure=None, wavelength=None, **pd_kwargs): """ This function was created to investigate CLD input file for PICASO. The plot itselfs creates maps of the wavelength dependent single scattering albedo and cloud opacity and assymetry parameter as a function of altitude. Parameters ---------- nwno : int Number of wavenumber points. For runs from Ackerman & Marley, this will always be 196. nlayer : int Should be one less than the number of levels in your pressure temperature grid. Cloud opacity is assigned for slabs. file : str , optional (Optional)Path to cloud input file df : str (Optional)Dataframe of cloud input file wavelength : array , optional (Optional) this allows you to reset the tick marks to wavelengths instead of indicies pressure : array, optional (Optional) this allows you to reset the tick marks to pressure instead of indicies pd_kwargs : kwargs Pandas key word arguments for `pandas.read_csv` Returns ------- Three bokeh plots with the single scattering, optical depth, and assymetry maps """ if (pressure is not None): pressure_label = 'Pressure (units by user)' else: pressure_label = 'Pressure Grid, TOA ->' if (wavelength is not None): wavelength_label = 'Wavelength (units by user)' else: wavelength_label = 'Wavenumber Grid' cols = colfun1(200) color_mapper = LinearColorMapper(palette=cols, low=0, high=1) if not isinstance(filename, type(None)): dat01 = pd.read_csv(filename, **pd_kwargs) elif not isinstance(df, type(None)): dat01 = df #PLOT W0 scat01 = np.flip(np.reshape(dat01['w0'].values, (nlayer, nwno)), 0) xr, yr = scat01.shape f01a = figure(x_range=[0, yr], y_range=[0, xr], x_axis_label=wavelength_label, y_axis_label=pressure_label, title="Single Scattering Albedo", plot_width=300, plot_height=300) f01a.image(image=[scat01], color_mapper=color_mapper, x=0, y=0, dh=xr, dw=yr) color_bar = ColorBar( color_mapper=color_mapper, #ticker=LogTicker(), label_standoff=12, border_line_color=None, location=(0, 0)) f01a.add_layout(color_bar, 'left') #PLOT OPD scat01 = np.flip(np.reshape(dat01['opd'].values, (nlayer, nwno)), 0) xr, yr = scat01.shape cols = colfun2(200)[::-1] color_mapper = LogColorMapper(palette=cols, low=1e-3, high=10) f01 = figure(x_range=[0, yr], y_range=[0, xr], x_axis_label=wavelength_label, y_axis_label=pressure_label, title="Cloud Optical Depth Per Layer", plot_width=300, plot_height=300) f01.image(image=[scat01], color_mapper=color_mapper, x=0, y=0, dh=xr, dw=yr) color_bar = ColorBar(color_mapper=color_mapper, ticker=LogTicker(), label_standoff=12, border_line_color=None, location=(0, 0)) f01.add_layout(color_bar, 'left') #PLOT G0 scat01 = np.flip(np.reshape(dat01['g0'].values, (nlayer, nwno)), 0) xr, yr = scat01.shape cols = colfun3(200)[::-1] color_mapper = LinearColorMapper(palette=cols, low=0, high=1) f01b = figure(x_range=[0, yr], y_range=[0, xr], x_axis_label=wavelength_label, y_axis_label=pressure_label, title="Assymetry Parameter", plot_width=300, plot_height=300) f01b.image(image=[scat01], color_mapper=color_mapper, x=0, y=0, dh=xr, dw=yr) color_bar = ColorBar(color_mapper=color_mapper, ticker=BasicTicker(), label_standoff=12, border_line_color=None, location=(0, 0)) f01b.add_layout(color_bar, 'left') #CHANGE X AND Y AXIS TO BE PHYSICAL UNITS #indexes for pressure plot if (pressure is not None): pressure = ["{:.1E}".format(i) for i in pressure[::-1] ] #flip since we are also flipping matrices npres = len(pressure) ipres = np.array(range(npres)) #set how many we actually want to put on the figure #hard code ten on each.. ipres = ipres[::int(npres / 10)] pressure = pressure[::int(npres / 10)] #create dictionary for tick marks ptick = {int(i): j for i, j in zip(ipres, pressure)} for i in [f01a, f01, f01b]: i.yaxis.ticker = ipres i.yaxis.major_label_overrides = ptick if (wavelength is not None): wave = ["{:.2F}".format(i) for i in wavelength] nwave = len(wave) iwave = np.array(range(nwave)) iwave = iwave[::int(nwave / 10)] wave = wave[::int(nwave / 10)] wtick = {int(i): j for i, j in zip(iwave, wave)} for i in [f01a, f01, f01b]: i.xaxis.ticker = iwave i.xaxis.major_label_overrides = wtick return row(f01a, f01, f01b)
def create_plot(): s1 = ColumnDataSource(data=dict(y=prov_types, x=[0] * 89)) # Type of Providers s2 = ColumnDataSource(data=dict(y=['Female', 'Male'], x=[0, 0])) # Gender of Providers callback = CustomJS(args=dict(source=data1, s1=s1, s2=s2), code=""" var i = source.selected.indices var d1 = s1.data; d1['x'] = cb_data.source.data['Types'][i] var d2 = s2.data; console.log(cb_data.source.data['Gender'][i]) d2['x'] = cb_data.source.data['Gender'][i] s1.change.emit(); s2.change.emit(); """) color_mapper = LinearColorMapper(palette=palette, low=min(dfnew['n']), high=max(dfnew['n'])) p = figure(title="Number of Physicians in Each Category", toolbar_location="left", plot_width=320, plot_height=320, x_range=(0.5, 5.5), y_range=(0.5, 5.5), x_axis_label='Standardized Payments', y_axis_label='Beneficiaries', match_aspect=True, aspect_scale=1) p.rect('mmedpay', 'mmedbenif', width=1, height=1, source=data1, fill_alpha=1, line_color="#FFFFFF", line_width=1.25, fill_color={ 'field': 'n', 'transform': color_mapper }) color_bar = ColorBar(color_mapper=color_mapper, ticker=BasicTicker(), label_standoff=12, border_line_color=None, location=(0, 0)) p.add_tools(TapTool(callback=callback)) p.outline_line_alpha = 0 p.xgrid.grid_line_color = None p.ygrid.grid_line_color = None p.yaxis.axis_line_color = None p.yaxis.major_tick_line_color = None p.yaxis.minor_tick_line_color = None p.yaxis.major_tick_out = 0 p.ygrid.grid_line_color = None p.xaxis.axis_line_color = None p.xaxis.minor_tick_line_color = None p.xaxis.major_tick_line_color = None # # Legend # p.legend.location = "center_right" # p.legend.orientation = "vertical" p.xaxis.major_label_overrides = { 1: 'Lowest', 2: 'Low', 3: 'Medium', 4: 'High', 5: 'Highest' } p.yaxis.major_label_overrides = { 1: 'Lowest', 2: 'Low', 3: 'Medium', 4: 'High', 5: 'Highest' } q1 = figure( title="Type of Providers", x_range=prov_types, #y_range=(0,200), plot_width=1000, plot_height=650, x_axis_label='Type of Providers', y_axis_label='Number of Providers') q1.vbar(x='y', top='x', width=0.5, source=s1, color='#EC8400') q1.add_layout(color_bar, 'left') q1.outline_line_alpha = 0 q1.yaxis.axis_line_color = "#a7a7a7" q1.yaxis.major_tick_line_color = "#a7a7a7" q1.yaxis.minor_tick_line_color = None q1.y_range.start = 0 q1.yaxis.major_tick_out = 0 q1.xaxis.major_label_orientation = math.pi / 2 q1.xaxis.axis_line_color = "#a7a7a7" q1.xaxis.minor_tick_line_color = None q1.xaxis.major_tick_line_color = None q1.xgrid.grid_line_color = None q2 = figure(title="Providers Gender", x_range=['Female', 'Male'], plot_width=350, plot_height=350, x_axis_label='% of Ratings Given', y_axis_label="Ratings", match_aspect=True, aspect_scale=1) q2.vbar(x='y', top='x', width=0.5, source=s2, color='#EC8400') q2.outline_line_alpha = 0 q2.yaxis.axis_line_color = "#a7a7a7" q2.yaxis.major_tick_line_color = "#a7a7a7" q2.yaxis.minor_tick_line_color = None q2.y_range.start = 0 q2.yaxis.major_tick_out = 0 q2.xaxis.axis_line_color = "#a7a7a7" q2.xaxis.minor_tick_line_color = None q2.xaxis.major_tick_line_color = None q2.xgrid.grid_line_color = None return row(column(p, q2), q1)
def plot_ptable_trend( data_elements=["Rb", "S", "Se"], data_list=[10, 20, 30], input_file=None, output_html="ptable.html", bokeh_palette="Plasma256", cmap=plasma, log_scale=0, width=1050, alpha=0.65, cbar_height=520, cbar_font="14pt", save_plot=True, ): """ Generate periodic table chemical trends. Either provide a file or list of data_elements, &data_list. Note that Bokeh already provided a periodic table. This module will take your data to color code them. See an example: https://www.nature.com/articles/s41598-019-45028-y Fig. 3 Forked from https://github.com/arosen93/ptable_trends """ output_file(output_html) # Define number of and groups period_label = ["1", "2", "3", "4", "5", "6", "7"] group_range = [str(x) for x in range(1, 19)] if input_file is not None: data_elements = [] data_list = [] f = open(input_file, "r") lines = f.read().splitlines() f.close() for i in lines: data_elements.append(i.split()[0]) data_list.append(i.split()[1]) data = [float(i) for i in data_list] if len(data) != len(data_elements): raise ValueError("Unequal number of atomic elements and data points") # lanthanides = [x.lower() for x in elements["symbol"][56:70].tolist()] # actinides = [x.lower() for x in elements["symbol"][88:102].tolist()] period_label.append("blank") period_label.append("La") period_label.append("Ac") count = 0 for i in range(56, 70): elements.period[i] = "La" elements.group[i] = str(count + 4) count += 1 count = 0 for i in range(88, 102): elements.period[i] = "Ac" elements.group[i] = str(count + 4) count += 1 # Define matplotlib and bokeh color map if log_scale == 0: color_mapper = LinearColorMapper(palette=bokeh_palette, low=min(data), high=max(data)) norm = Normalize(vmin=min(data), vmax=max(data)) elif log_scale == 1: for i in range(len(data)): if data[i] < 0: raise ValueError("Entry for element " + data_elements[i] + " is negative but" " log-scale is selected") color_mapper = LogColorMapper(palette=bokeh_palette, low=min(data), high=max(data)) norm = LogNorm(vmin=min(data), vmax=max(data)) color_scale = ScalarMappable(norm=norm, cmap=cmap).to_rgba(data, alpha=None) # Define color for blank entries blank_color = "#c4c4c4" color_list = [] for i in range(len(elements)): color_list.append(blank_color) # Compare elements in dataset with elements in periodic table for i in range(len(data)): element_entry = elements.symbol[elements.symbol.str.lower() == data_elements[i].lower()] if not element_entry.empty: element_index = element_entry.index[0] else: print("WARNING: Invalid chemical symbol: " + data_elements[i]) if color_list[element_index] != blank_color: print("WARNING: Multiple entries for element " + data_elements[i]) color_list[element_index] = to_hex(color_scale[i]) # Define figure properties for visualizing data source = ColumnDataSource(data=dict( group=[str(x) for x in elements["group"]], period=[str(y) for y in elements["period"]], sym=elements["symbol"], atomic_number=elements["atomic number"], type_color=color_list, )) # Plot the periodic table p = figure(x_range=group_range, y_range=list(reversed(period_label)), tools="save") p.plot_width = width p.outline_line_color = None p.toolbar_location = "above" p.rect( "group", "period", 0.9, 0.9, source=source, alpha=alpha, color="type_color", ) p.axis.visible = False text_props = { "source": source, "angle": 0, "color": "black", "text_align": "left", "text_baseline": "middle", } x = dodge("group", -0.4, range=p.x_range) y = dodge("period", 0.3, range=p.y_range) p.text(x=x, y="period", text="sym", text_font_style="bold", text_font_size="15pt", **text_props) p.text(x=x, y=y, text="atomic_number", text_font_size="9pt", **text_props) color_bar = ColorBar( color_mapper=color_mapper, ticker=BasicTicker(desired_num_ticks=10), border_line_color=None, label_standoff=6, major_label_text_font_size=cbar_font, location=(0, 0), orientation="vertical", scale_alpha=alpha, width=8, ) if cbar_height is not None: color_bar.height = cbar_height p.add_layout(color_bar, "right") p.grid.grid_line_color = None if save_plot: save(p) else: show(p) return p
def GENE(self): def gcw(*args, **kwargs): return self.ccwidget('geneview_' + args[0], *args[1:], **kwargs, setnamer='gene') #widgets w = dict( gene=gcw('gene_name', 'text'), warn=widgets.HTML(), ) # data samples = self.counttable.columns nosamples = len(samples) #gene mapper gmapper = CMAPPER(self, name='gene') gmapper.method = 'gene' defgene = self.counttable.std(1).sort_values().tail(1).index[0] gmapper.value = defgene w['gene'].value = defgene #color mapper cmapper = CMAPPER(self, name='color') cmapper.method = 'metadata' cmapper.value = 'plate' #sort order smapper = CMAPPER(self, name='sort') smapper2 = CMAPPER(self, name='sort 2') def get_sort_order(): sdf = pd.DataFrame({ 1: smapper.score.sort_values(), 2: smapper2.score.sort_values() }) sdf = sdf.sort_values(by=[1, 2]) return sdf.index sort_order = get_sort_order() pdata = ColumnDataSource( dict( x=pd.Series(range(nosamples), index=sort_order), y=gmapper.score.loc[sort_order], score=cmapper.score.loc[sort_order], )) pdata2 = pd.DataFrame( dict( x=pd.Series(range(nosamples), index=sort_order), y=gmapper.score.loc[sort_order], score=cmapper.score.loc[sort_order], )) bfigure = bokeh_figure( plot_width=FIGSIZE[0], plot_height=int(FIGSIZE[1] * 0.8), # tools = bokeh_tools, y_range=bmodels.Range1d(gmapper.min(), gmapper.max()), toolbar_sticky=False, toolbar_location='left', title='geneplot') #bbar = bokeh_chart.Bar(pdata2, 'x', values='y', group='plate') bfigure.title.text_color = 'darkgrey' bfigure.title.text_font_style = 'normal' bfigure.title.text_font_size = "12px" bplot = bfigure.vbar(x='x', width=0.5, bottom=0, top='y', source=pdata, legend='score', color=dict(field='score', transform=cmapper.colormapper)) blegend = bfigure.legend[0].items[0] bcolorbar = ColorBar(color_mapper=gmapper.colormapper, ticker=BasicTicker(), formatter=BasicTickFormatter(precision=1), label_standoff=10, border_line_color=None, location=(0, 0)) null_colorbar_mapper = LinearColorMapper(palette='Inferno256', low=0, high=0) if cmapper.discrete: #remove ColorBar bcolorbar.color_mapper = null_colorbar_mapper else: #remove legend bfigure.legend[0].items.pop( ) #remove legend - we can add this later again bfigure.add_layout(bcolorbar, 'right') # # display widgets display(ilabel('gene', w['gene'])) cmapper.display() smapper.display() smapper2.display() display(w['warn']) #for k, v in bplot.data_source.data.items(): # print(k, v.shape, v.dropna().shape) bhandle = bokeh_io.show(bfigure, notebook_handle=True) #bhandle = bokeh_io.show(bbar, notebook_handle=True) def warn(message): w['warn'].value = '<b>{}</b>'.format(message) def on_gene_change(*args): gene = w['gene'].value if not gene in self.counttable.index: warn("gene {} is not in current counttable".format(gene)) return sortorder = get_sort_order() gmapper.value = gene yval = gmapper.score.loc[sortorder] bplot.data_source.data['y'] = yval bokeh_io.push_notebook(handle=bhandle) def on_sort_change(*args): order = get_sort_order() d = bplot.data_source.data d['x'].index = order d['y'] = d['y'].loc[order] d['score'] = d['score'].loc[order] bokeh_io.push_notebook(handle=bhandle) def on_color_change(*args): order = get_sort_order() score = cmapper.score score = score.loc[order] bplot.data_source.data['score'] = score bplot.glyph.fill_color['transform'] = cmapper.colormapper cm = cmapper.colormapper self._cm = cm if cmapper.discrete: warn('discrete') bcolorbar.color_mapper = null_colorbar_mapper if not bfigure.legend[0].items: bfigure.legend[0].items.append(blegend) else: warn('cont') bcolorbar.color_mapper = cmapper.colormapper if bfigure.legend[0].items: bfigure.legend[0].items.pop() bokeh_io.push_notebook(handle=bhandle) smapper.on_change = on_sort_change smapper2.on_change = on_sort_change cmapper.on_change = on_color_change w['gene'].on_submit(on_gene_change) on_gene_change on_color_change on_sort_change
def analytics(): uniqueDates = set() dynamodb = boto3.resource('dynamodb', region_name='us-west-1') table = dynamodb.Table('CSCE-678-Spark') response = table.query(KeyConditionExpression=Key('key').eq('historic-tweet-per-day'))["Items"][0] negativeTweets = json.loads(response["neg-tweet-dict"]) positiveTweets = json.loads(response["pos-tweet-dict"]) negativeTweetDates = set(negativeTweets.keys()) positiveTweetDates = set(positiveTweets.keys()) uniqueDates = list(negativeTweetDates.intersection(positiveTweets)) dateRanges = set() for date in uniqueDates: dt = datetime.strptime(date, '%Y-%m-%d') start = dt - timedelta(days=dt.weekday()) end = start + timedelta(days=6) start = start.strftime('%Y-%m-%d') end = end.strftime('%Y-%m-%d') range = str(start) + " : " + str(end) dateRanges.add(range) dateRanges = list(dateRanges) dateRanges.sort() data = {"dateRanges" : dateRanges, "Positive" : len(dateRanges) * [0], "Negative": len(dateRanges) * [0]} for date in uniqueDates: dt = datetime.strptime(date, '%Y-%m-%d') start = dt - timedelta(days=dt.weekday()) end = start + timedelta(days=6) start = start.strftime('%Y-%m-%d') end = end.strftime('%Y-%m-%d') range = str(start) + " : " + str(end) data["Positive"][dateRanges.index(range)] += positiveTweets[date] data["Negative"][dateRanges.index(range)] += negativeTweets[date] source = ColumnDataSource(data=data) maxValue = max(max(data["Positive"]), max(data["Negative"])) p = figure(x_range=dateRanges, y_range=(0, int(1.1 * maxValue)), plot_height=600, plot_width=800, title="No. of Positive & Negative tweets by week", toolbar_location=None, tools="") p.vbar(x=dodge("dateRanges", -0.25, range=p.x_range), top="Positive", width=0.2, source=source, color="#718dbf", legend_label="Positive") p.vbar(x=dodge("dateRanges", 0.0, range=p.x_range), top="Negative", width=0.2, source=source, color="#e84d60", legend_label="Negative") p.x_range.range_padding = 0.1 p.xgrid.grid_line_color = None p.legend.location = "top_left" p.legend.orientation = "horizontal" p.left[0].formatter.use_scientific = False p.yaxis.axis_label = "No. of tweets" p.xaxis.axis_label = "Week" p.yaxis[0].formatter = NumeralTickFormatter(format='0.0 a') p.add_tools(HoverTool(tooltips=[('Date Range', "@dateRanges"), ('No. of tweets', '$y{(0 a)}')], mode='vline')) script, div = components(p) uniqueDates.sort() data2 = {"uniqueDates" : uniqueDates, "Positive" : [], "Negative": []} colors = bokeh.palettes.d3['Category10'][10] for date in uniqueDates: data2["Positive"].append(positiveTweets[date]) data2["Negative"].append(negativeTweets[date]) maxValue = max(max(data2["Positive"]), max(data2["Negative"])) uniqueDates = [datetime.strptime(date, '%Y-%m-%d') for date in uniqueDates] p2 = figure(y_range=(0, int(1.1 * maxValue)), x_axis_type='datetime', plot_height=600, plot_width=800, title="No. of Positive & Negative tweets by day", toolbar_location=None, tools="") p2.line(uniqueDates, data2["Positive"], legend="Positive", line_color=colors[2], line_width = 3, alpha=0.8) p2.line(uniqueDates, data2["Negative"], legend="Negative", line_color=colors[1], line_width = 3, alpha=0.8) p2.legend.location = "top_left" p2.legend.orientation = "horizontal" p2.left[0].formatter.use_scientific = False p2.yaxis.axis_label = "No. of tweets" p2.xaxis.axis_label = "Date" p2.yaxis[0].formatter = NumeralTickFormatter(format='0 a') p2.xaxis.formatter = DatetimeTickFormatter(days=["%Y-%m-%d"]) p2.add_tools(HoverTool(tooltips=[('Date', "$x{%F}"), ('No. of tweets', '$y{(0 a)}')], formatters={'$x': 'datetime'}, mode='vline')) script2, div2 = components(p2) response = table.query(KeyConditionExpression=Key('key').eq('history-topic-modelling-frequency-weight-sw'))["Items"][0] topics10 = json.loads(response["topics-10"]) topics = topics10.keys() data3 = {"words": [], "weights": [], "frequencies": [], "scaled": [], "topic": []} for topic in topics: res = topics10[topic] for key in res: if key in data3["words"]: index = data3["words"].index(key) data3["weights"][index] = max(data3["weights"][index], res[key][1]) data3["scaled"][index] = data3["weights"][index] * 2000 else: data3["words"].append(key) data3["weights"].append(res[key][1]) if res[key][0] is None or str(res[key][0]) == "null": data3["frequencies"].append(0) else: data3["frequencies"].append(res[key][0][0]) data3["scaled"].append(res[key][1] * 2000) data3["topic"].append(str(topic)) zipped = list(zip(data3["frequencies"], data3["weights"], data3["words"], data3["scaled"], data3["topic"])) zipped.sort() data3["frequencies"], data3["weights"], data3["words"], data3["scaled"], data3["topic"] = zip(*zipped) source = ColumnDataSource(data=data3) p3 = figure(x_range = (0, max(data3["frequencies"])), y_range = data3["words"], plot_height=600, plot_width=1600) color_mapper = LinearColorMapper(palette = Viridis256, low = min(data3["weights"]), high = max(data3["weights"])) color_bar = ColorBar(color_mapper = color_mapper, location = (0, 0),ticker = BasicTicker()) p3.add_layout(color_bar, 'right') p3.scatter(x = 'frequencies', y = 'words', size = 'scaled', fill_color = transform('weights', color_mapper), source = source) p3.add_tools(HoverTool(tooltips = [('Topic', '@topic'), ('Word', '@words'), ('Frequency', '@frequencies')])) p3.below[0].formatter.use_scientific = False script3, div3 = components(p3) print(print(bokeh.__version__)) return render_template('index.html', sent_div=div, sent_script=script, sent_div2=div2, sent_script2=script2, sent_script3=script3, sent_div3=div3, data="")
def plot_map(pollutant, date, all_pollutants_df): ''' Output a map of all the counties and display the pollutant levels on a specific day ''' from bokeh.sampledata.us_counties import data as counties #The Bokeh counties data has 58 counties listed in CA, but EPA never has all 58 counties #for the pollutants. Here I am copying all the lat/long data for the counties that I have====== #from original BOKEH SAMPLE counties = { code: county for code, county in counties.items() if county["state"] == "ca" } county_xs = [county["lons"] for county in counties.values()] county_ys = [county["lats"] for county in counties.values()] county_names = [county['name'] for county in counties.values()] my_counties = all_pollutants_df[pollutant].columns #Change Log/Lat to use my_counties county_xs_new = [] county_ys_new = [] for c in my_counties: county_xs_new.append(county_xs[county_names.index(c)]) county_ys_new.append(county_ys[county_names.index(c)]) #Plot ========= from bokeh.io import show, export_png # export_png is dependent on selenium. run: # conda install selenium phantomjs pillow from bokeh.models import LinearColorMapper, BasicTicker, ColorBar from bokeh.palettes import Viridis6 as palette from bokeh.plotting import figure color_mapper = LinearColorMapper(palette=palette) data = dict( x=county_xs_new, y=county_ys_new, name=my_counties, rate=list(all_pollutants_df[pollutant].loc[date]), ) TOOLS = "pan,wheel_zoom,reset,hover,save" pollutant_info = { 'co': ['ppm', 'Carbon monoxide'], 'no2': ['ppb', 'Nitrogen dioxide (NO2)'], 'ozone': ['ppm', 'Ozone'], 'pb': ['ug/m3', 'Lead'], 'pm2_5': ['ug/m3', 'PM2.5'], 'pm10': ['ug/m3', 'PM10'], 'so2': ['ppb', 'Sulfur dioxide'] } p = figure(title="California %s, %s" % (pollutant_info[pollutant][1], date), tools=TOOLS, x_axis_location=None, y_axis_location=None, tooltips=[("Name", "@name"), ("%s" % pollutant_info[pollutant][1], "@rate %s" % pollutant_info[pollutant][0]), ("(Long, Lat)", "($x, $y)")]) p.grid.grid_line_color = None p.hover.point_policy = "follow_mouse" p.patches('x', 'y', source=data, fill_color={ 'field': 'rate', 'transform': color_mapper }, fill_alpha=0.7, line_color="white", line_width=0.5) color_bar = ColorBar(color_mapper=color_mapper, ticker=BasicTicker(), label_standoff=12, border_line_color=None, location=(0, 0)) p.add_layout(color_bar, 'right') #export_png(p,'Results/CA_ozone_%s.png'%t.date()) show(p)
def plot_interactive(R, ix, raw_input, features, save_to="jet_0.html"): r = (R[ix]["node"]).clone() r[torch.isnan(r)] = 0 val = r.detach().cpu().numpy() data = pd.DataFrame(val, columns=features) data.columns.name = "feature" data.index.name = "particle" df = pd.DataFrame(data.stack(), columns=['relevance']).reset_index() raw = raw_input[ix].x sort_idx = torch.argsort(raw[:, 0]) raw = raw[sort_idx] df["raw data"] = raw.reshape(-1, 1).clone().detach().numpy() df["particle"] = df["particle"].astype(str) df.drop_duplicates(['particle', 'feature'], inplace=True) data = data.T.reset_index().drop_duplicates( subset='feature').set_index('feature').T source = ColumnDataSource(df) colors = [] cmap = sns.diverging_palette(240, 10, as_cmap=True) for i in range(cmap.N): rgba = cmap(i) colors.append(matplotlib.colors.rgb2hex(rgba)) scale = max(np.abs(df.relevance.min()), np.abs(df.relevance.max())) mapper = LinearColorMapper(palette=colors, low=-scale, high=scale) if R[ix]['label'][:, 1] > 0: title_str = "Node Relevance Heatmap for Higgs boson Signal" else: title_str = "Node Relevance Heatmap for background" subtitle_str = "prediction:{}".format( R[ix]["pred"].detach().cpu().numpy().round(4)[0]) p = figure(x_range=[str(i) for i in data.index], y_range=list(reversed(data.columns)), tools=["hover", "save"]) p.add_layout(Title(text=subtitle_str, text_font_style="italic"), 'above') p.add_layout(Title(text=title_str, text_font_size="12pt"), 'above') p.rect(x="particle", y="feature", width=1, height=1, source=source, line_color='white', fill_color=transform('relevance', mapper)) p.hover.tooltips = [("particle", "@particle"), ("feature", "@feature"), ("relevance score", "@relevance"), ("input data", "@{raw data}")] color_bar = ColorBar(color_mapper=mapper, ticker=BasicTicker(desired_num_ticks=10), location=(0, 0), formatter=PrintfTickFormatter(format="%d")) p.add_layout(color_bar, 'right') save(p, save_to)
def main(filename, src, dst, outputFileName): df = pd.read_csv(filename) df = df.replace(nan, 'unknown') valuetype = 'Flows' ########################################################################################### ##################################### CREATE DF ####################################### Csrcval = src Cdstval = dst dfcountry = df[(df['src_country'] == Csrcval) & (df['dst_country'] == Cdstval)] c_ind = list(dfcountry['src_org'].unique()) c_col = list(dfcountry['dst_org'].unique()) c_dfFlows = pd.DataFrame(index=c_ind, columns=c_col, data=0) c_dfPkts = pd.DataFrame(index=c_ind, columns=c_col, data=0) c_dfOcts = pd.DataFrame(index=c_ind, columns=c_col, data=0) dftest = dfcountry.groupby(['src_org', 'dst_org']).size() for x in dftest.index.values.tolist(): c_dftemp = dfcountry[(dfcountry['src_org'] == x[0]) & (dfcountry['dst_org'] == x[1])] print(f'Pkts from {x[0]} to {x[1]}') c_dfPkts.at[[x[0]], [x[1]]] = c_dftemp['dPkts'].sum() print(c_dftemp['dPkts'].sum()) print(f'Bytes from {x[0]} to {x[1]}') c_dfOcts.at[[x[0]], [x[1]]] = c_dftemp['dOctets'].sum() print(c_dftemp['dOctets'].sum()) print(f'Flows from {x[0]} to {x[1]}') c_dfFlows.at[[x[0]], [x[1]]] = len(c_dftemp) print(len(c_dftemp)) # rename column c_dfPkts.columns.name = Cdstval c_dfOcts.columns.name = Cdstval c_dfFlows.columns.name = Cdstval # create df to be visualized dfViz = pd.DataFrame(c_dfFlows.stack(), columns=['value']).reset_index() dfViz.columns = ['src_org', 'dst_org', 'value'] dfViz2 = pd.DataFrame(c_dfPkts.stack(), columns=['value']).reset_index() dfViz2.columns = ['src_org', 'dst_org', 'value'] dfViz3 = pd.DataFrame(c_dfOcts.stack(), columns=['value']).reset_index() dfViz3.columns = ['src_org', 'dst_org', 'value'] ########################################################################################### ##################################### COLOR MAP ######################################## colors = RdBu(500) c_mapper = LinearColorMapper(palette=colors, low=dfViz.value.min(), high=dfViz.value.max()) c_mapper2 = LinearColorMapper(palette=colors, low=dfViz2.value.min(), high=dfViz2.value.max()) c_mapper3 = LinearColorMapper(palette=colors, low=dfViz3.value.min(), high=dfViz3.value.max()) ########################################################################################### ##################################### LIST CHECK ####################################### srclist = list(dfViz.src_org.unique()) dstlist = list(dfViz.dst_org.unique()) if len(srclist) == 1 and len(dstlist) == 1: return 1 else: ########################################################################################### ####################################### PLOT 1 ######################################## TOOLS = "hover,save,pan,box_zoom,reset,wheel_zoom,tap" c_p = figure(title="Interactive Visualization", x_range=c_ind, y_range=c_col, x_axis_location="above", plot_width=900, plot_height=800, tools=TOOLS, toolbar_location='below', tooltips=[('Source', '@src_org'), ('Destination', '@dst_org'), (valuetype, '@value')]) c_p.title.align = 'center' c_p.grid.grid_line_color = None c_p.axis.axis_line_color = 'white' c_p.axis.major_tick_line_color = None c_p.axis.major_label_text_font_size = "10pt" c_p.axis.major_label_standoff = 0 c_p.xaxis.axis_label = 'Source' c_p.yaxis.axis_label = 'Destination' c_p.xaxis.major_label_orientation = pi / 3 c_heatmap = c_p.rect(x='src_org', y='dst_org', width=1, height=1, source=dfViz, fill_color={ 'field': 'value', 'transform': c_mapper }, line_color='white') c_color_bar = ColorBar(color_mapper=c_mapper, major_label_text_font_size="10pt", ticker=BasicTicker(desired_num_ticks=10), label_standoff=12, border_line_color=None, location=(0, 0)) c_color_bar.formatter.use_scientific = False c_p.add_layout(c_color_bar, 'right') ########################################################################################### ####################################### PLOT 2 ######################################## valuetype = 'dPkts' c_p2 = figure(title="Interactive Visualization", x_range=c_ind, y_range=c_col, x_axis_location="above", plot_width=900, plot_height=800, tools=TOOLS, toolbar_location='below', tooltips=[('Source', '@src_org'), ('Destination', '@dst_org'), (valuetype, '@value')]) c_p2.title.align = 'center' c_p2.grid.grid_line_color = None c_p2.axis.axis_line_color = 'white' c_p2.axis.major_tick_line_color = None c_p2.axis.major_label_text_font_size = "10pt" c_p2.axis.major_label_standoff = 0 c_p2.xaxis.axis_label = 'Source' c_p2.yaxis.axis_label = 'Destination' c_p2.xaxis.major_label_orientation = pi / 3 c_heatmap2 = c_p2.rect(x='src_org', y='dst_org', width=1, height=1, source=dfViz2, fill_color={ 'field': 'value', 'transform': c_mapper2 }, line_color='white') c_color_bar2 = ColorBar(color_mapper=c_mapper2, major_label_text_font_size="10pt", ticker=BasicTicker(desired_num_ticks=10), label_standoff=12, border_line_color=None, location=(0, 0)) c_color_bar2.formatter.use_scientific = False c_p2.add_layout(c_color_bar2, 'right') ########################################################################################### ####################################### PLOT 3 ######################################## valuetype = 'dOctets' c_p3 = figure(title="Interactive Visualization", x_range=c_ind, y_range=c_col, x_axis_location="above", plot_width=900, plot_height=800, tools=TOOLS, toolbar_location='below', tooltips=[('Source', '@src_org'), ('Destination', '@dst_org'), (valuetype, '@value')]) c_p3.title.align = 'center' c_p3.grid.grid_line_color = None c_p3.axis.axis_line_color = 'white' c_p3.axis.major_tick_line_color = None c_p3.axis.major_label_text_font_size = "10pt" c_p3.axis.major_label_standoff = 0 c_p3.xaxis.axis_label = 'Source' c_p3.yaxis.axis_label = 'Destination' c_p3.xaxis.major_label_orientation = pi / 3 c_heatmap3 = c_p3.rect(x='src_org', y='dst_org', width=1, height=1, source=dfViz3, fill_color={ 'field': 'value', 'transform': c_mapper3 }, line_color='white') c_color_bar3 = ColorBar(color_mapper=c_mapper2, major_label_text_font_size="10pt", ticker=BasicTicker(desired_num_ticks=10), label_standoff=12, border_line_color=None, location=(0, 0)) c_color_bar3.formatter.use_scientific = False c_p3.add_layout(c_color_bar3, 'right') ########################################################################################### ###################################### BUTTONS ######################################## homeButton = Button(label="Back to Homepage") col = column(c_p, c_p2, c_p3) radioBtn = RadioButtonGroup( labels=["All", "Flows", "Packets", "Bytes"], active=0, width=300) radioCallback = CustomJS(args=dict(plots=[c_p, c_p2, c_p3], col=col, radioBtn=radioBtn), code=""" const children = [] if (radioBtn.active == 0) { children.push(plots[0]) children.push(plots[1]) children.push(plots[2]) } if (radioBtn.active == 1) { children.push(plots[0]) } if (radioBtn.active == 2) { children.push(plots[1]) } if (radioBtn.active == 3) { children.push(plots[2]) } col.children = children """) radioBtn.js_on_change('active', radioCallback) homeButtonCallback = CustomJS(code=""" window.location.href = "http://engineering.utep.edu:62432"; """) homeButton.js_on_event(events.ButtonClick, homeButtonCallback) jsonString = dfViz.to_json(orient='table', index=False) callback = CustomJS(args=dict(plots=[c_p, c_p2, c_p3], src=srclist, dst=dstlist, file=filename, dataframe=jsonString), code=""" // the event that triggered the callback is cb_obj: // The event type determines the relevant attributes // console.log('Tap event occurred at x-position: ' + cb_obj.x) // console.log('Tap event occurred at y-position: ' + cb_obj.y) // console.log('Tap event occurred at y-position: ' + parseInt(cb_obj.y)) console.log('Tap event occurred at x-position: ' + src[parseInt(cb_obj.x)]); console.log('Tap event occurred at y-position: ' + dst[parseInt(cb_obj.y)]); console.log('Tap event occurred at value:'); df = JSON.parse(dataframe); console.log(df); // console.log(df.data[4]); function findObjectByKey(array, key1, value1, key2, value2) { for (var i = 0; i < array.length; i++) { // console.log(i); // console.log(array[i][key1]+' : '+value1+' VS '+array[i][key2]+' : '+ value2); if (array[i][key1] === value1 && array[i][key2] === value2) { return array[i]; } } return null; } var obj = findObjectByKey(df.data, 'src_org', src[parseInt(cb_obj.x)], 'dst_org', dst[parseInt(cb_obj.y)]); console.log(obj.value); if (obj.value == 0) { alert('Selected pair has no communication'); plots[0].reset.emit(); plots[1].reset.emit(); plots[2].reset.emit(); } else { var form = document.createElement('form'); form.setAttribute('method', 'post'); form.setAttribute('action', '/asHist'); form.style.display = 'none'; var fileName = document.createElement("textarea"); fileName.type = "textarea"; fileName.name = "FileName"; fileName.id = "FileName"; fileName.value = file; form.appendChild(fileName); var sourceVal = document.createElement("textarea"); sourceVal.type = "textarea"; sourceVal.name = "SrcVal"; sourceVal.id = "SrcVal"; sourceVal.value = src[parseInt(cb_obj.x)]; form.appendChild(sourceVal); var destVal = document.createElement("textarea"); destVal.type = "textarea"; destVal.name = "DestVal"; destVal.id = "DestVal"; destVal.value = dst[parseInt(cb_obj.y)]; form.appendChild(destVal); document.body.appendChild(form); console.log(fileName.value); console.log(sourceVal.value); console.log(destVal.value); form.submit(); plots[0].reset.emit(); plots[1].reset.emit(); plots[2].reset.emit(); } """) c_p.js_on_event('tap', callback) c_p2.js_on_event('tap', callback) c_p3.js_on_event('tap', callback) ########################################################################################### ###################################### OUTPUT ######################################### output_file("continent_test.ejs") layout = column(row(homeButton, radioBtn), col) save(layout, filename=outputFileName, title='Organization Level') return 0
toolbar_location='below', tooltips=[('date', '@Month @Year'), ('rate', '@rate%')]) p.grid.grid_line_color = None p.axis.axis_line_color = None p.axis.major_tick_line_color = None p.axis.major_label_text_font_size = "7px" p.axis.major_label_standoff = 0 p.xaxis.major_label_orientation = pi / 3 p.rect(x="Year", y="Month", width=1, height=1, source=df, fill_color={ 'field': 'rate', 'transform': mapper }, line_color=None) color_bar = ColorBar(color_mapper=mapper, major_label_text_font_size="7px", ticker=BasicTicker(desired_num_ticks=len(colors)), formatter=PrintfTickFormatter(format="%d%%"), label_standoff=6, border_line_color=None) p.add_layout(color_bar, 'right') show(p)
def bkplot(self, x, y, color='None', radii='None', ps=20, minps=0, alpha=0.8, pw=600, ph=400, palette='Inferno256', style='smapstyle', Hover=True, title='', table=False, table_width=600, table_height=150, add_colorbar=True, Periodic_color=False, return_datasrc=False, frac_load=1.0, marker=['circle'], seed=0, **kwargs): from bokeh.layouts import row, widgetbox, column, Spacer from bokeh.models import HoverTool, TapTool, FixedTicker, Circle, WheelZoomTool from bokeh.models import CustomJS, Slider, Rect, ColorBar, HoverTool, LinearColorMapper, BasicTicker from bokeh.plotting import figure import bokeh.models.markers as Bokeh_markers from bokeh.models import ColumnDataSource, CDSView, IndexFilter from bokeh.palettes import all_palettes, Spectral6, Inferno256, Viridis256, Greys256, Magma256, Plasma256 from bokeh.palettes import Spectral, Inferno, Viridis, Greys, Magma, Plasma from bokeh.models import LogColorMapper, LogTicker, ColorBar, BasicTicker, LinearColorMapper from bokeh.models.widgets import DataTable, TableColumn, NumberFormatter, Div import pandas as pd # if (title==''): title=self.name fulldata = self.pd idx = np.arange(len(fulldata)) fulldata['id'] = idx nload = int(frac_load * len(fulldata)) np.random.seed(seed) np.random.shuffle(idx) idload = np.sort(idx[0:nload]) data = self.pd.iloc[idload].copy() if palette == 'cosmo': COLORS = cosmo() else: COLORS = locals()[palette] marklist = [ 'circle', 'diamond', 'triangle', 'square', 'asterisk', 'cross', 'inverted_triangle' ] if not marker[0] in marklist: marker = marklist # TOOLS="resize,crosshair,pan,wheel_zoom,reset,tap,save,box_select,box_zoom,lasso_select" TOOLS = "pan,reset,tap,save,box_zoom,lasso_select" wheel_zoom = WheelZoomTool(dimensions='both') if Hover: proplist = [] for prop in data.columns: if prop not in [ "CV1", "CV2", "Cv1", "Cv2", "cv1", "cv2", "colors", "radii", "id" ]: proplist.append((prop, '@' + prop)) hover = HoverTool(names=["mycircle"], tooltips=[("id", '@id')]) for prop in proplist: hover.tooltips.append(prop) plot = figure(title=title, plot_width=pw, active_scroll=wheel_zoom, plot_height=ph, tools=[TOOLS, hover, wheel_zoom], **kwargs) else: plot = figure(title=title, plot_width=pw, active_scroll=wheel_zoom, plot_height=ph, tools=[TOOLS], **kwargs) # selection glyphs and plot styles mdict = { 'circle': 'Circle', 'diamond': 'Diamond', 'triangle': 'Triangle', 'square': 'Square', 'asterisk': 'Asterisk', 'cross': 'Cross', 'inverted_triangle': 'InvertedTriangle' } initial_circle = Circle(x='x', y='y') selected_circle = getattr(Bokeh_markers, mdict[marker[0]])(fill_alpha=0.7, fill_color="blue", size=ps * 1.5, line_color="blue") nonselected_circle = getattr(Bokeh_markers, mdict[marker[0]])(fill_alpha=alpha * 0.5, fill_color='colors', line_color='colors', line_alpha=alpha * 0.5) # set up variable point size if radii == 'None': r = [ps for i in range(len(data))] data['radii'] = r else: if data[radii].dtype == 'object': # Categorical variable for radii grouped = data.groupby(radii) i = 0 r = np.zeros(len(data)) for group_item in grouped.groups.keys(): r[grouped.groups[group_item].tolist()] = i**2 i = i + 2 else: r = [val for val in data[radii]] rn = self.normalize(r) rad = [minps + ps * np.sqrt(val) for val in rn] data['radii'] = rad # setup variable point color if color == 'None': c = ["#31AADE" for i in range(len(data))] data['colors'] = c datasrc = ColumnDataSource(data) getattr(plot, marker[0])(x, y, source=datasrc, size='radii', fill_color='colors', fill_alpha=alpha, line_color='colors', line_alpha=alpha, name="mycircle") renderer = plot.select(name="mycircle") renderer.selection_glyph = selected_circle renderer.nonselection_glyph = nonselected_circle else: if data[color].dtype == 'object': # Categorical variable for colors grouped = data.groupby(color) # COLORS=Spectral[len(grouped)] i = 0 nc = len(COLORS) istep = int(nc / len(grouped)) cat_colors = [] for group_item in grouped.groups.keys(): # data.loc[grouped.groups[group_item],'colors']=COLORS[i] # print(group_item,COLORS[i]) i = min(i + istep, nc - 1) cat_colors.append(COLORS[i]) #colors=[ '#d53e4f', '#3288bd','#fee08b', '#99d594'] datasrc = ColumnDataSource(data) view = [] # used_markers=[] # marker=['circle','diamond','triangle','square','asterisk','cross','inverted_triangle'] #while True: # for x in marker: # used_markers.append(x) # if len(used_markers)>len(grouped): break i = 0 #print used_markers for group_item in grouped.groups.keys(): view.append( CDSView( source=datasrc, filters=[IndexFilter(grouped.groups[group_item])])) cname = 'mycircle' + str(i) #print used_markers[i] try: mk = marker[i] except: mk = marker[0] getattr(plot, mk)(x, y, source=datasrc, size='radii', fill_color=cat_colors[i], muted_color=cat_colors[i], muted_alpha=0.2, fill_alpha=alpha, line_alpha=alpha, line_color=cat_colors[i], name=cname, legend=group_item, view=view[i]) selected_mk = getattr(Bokeh_markers, mdict[mk])(fill_alpha=0.7, fill_color="blue", size=ps * 1.5, line_color="blue", line_alpha=0.7) nonselected_mk = getattr(Bokeh_markers, mdict[mk])( fill_alpha=alpha * 0.5, fill_color=cat_colors[i], line_color=cat_colors[i], line_alpha=alpha * 0.5) renderer = plot.select(name=cname) renderer.selection_glyph = selected_mk renderer.nonselection_glyph = nonselected_mk i += 1 plot.legend.location = "top_left" plot.legend.orientation = "vertical" plot.legend.click_policy = "hide" else: if Periodic_color: # if periodic property then generate periodic color palatte blendcolor = interpolate(COLORS[-1], COLORS[0], len(COLORS) / 5) COLORS = COLORS + blendcolor groups = pd.cut(data[color].values, len(COLORS)) c = [COLORS[xx] for xx in groups.codes] data['colors'] = c datasrc = ColumnDataSource(data) getattr(plot, marker[0])(x, y, source=datasrc, size='radii', fill_color='colors', fill_alpha=alpha, line_color='colors', line_alpha=alpha, name="mycircle") renderer = plot.select(name="mycircle") renderer.selection_glyph = selected_circle renderer.nonselection_glyph = nonselected_circle color_mapper = LinearColorMapper(COLORS, low=data[color].min(), high=data[color].max()) colorbar = ColorBar(color_mapper=color_mapper, ticker=BasicTicker(), label_standoff=4, border_line_color=None, location=(0, 0), orientation="vertical") colorbar.background_fill_alpha = 0 colorbar.border_line_alpha = 0 if add_colorbar: plot.add_layout(colorbar, 'left') # Overview plot oplot = figure(title='', plot_width=200, plot_height=200, toolbar_location=None) oplot.circle(x, y, source=datasrc, size=4, fill_alpha=0.6, line_color=None, name="mycircle") orenderer = oplot.select(name="mycircle") orenderer.selection_glyph = selected_circle # orenderer.nonselection_glyph = nonselected_circle rectsource = ColumnDataSource({'xs': [], 'ys': [], 'wd': [], 'ht': []}) jscode = """ var data = source.data; var start = range.start; var end = range.end; data['%s'] = [start + (end - start) / 2]; data['%s'] = [end - start]; source.change.emit(); """ plot.x_range.callback = CustomJS(args=dict(source=rectsource, range=plot.x_range), code=jscode % ('xs', 'wd')) plot.y_range.callback = CustomJS(args=dict(source=rectsource, range=plot.y_range), code=jscode % ('ys', 'ht')) rect = Rect(x='xs', y='ys', width='wd', height='ht', fill_alpha=0.1, line_color='black', fill_color='red') oplot.add_glyph(rectsource, rect) # plot style plot.toolbar.logo = None oplot.toolbar.logo = None if style == 'smapstyle': plist = [plot, oplot] else: plist = [oplot] for p in plist: p.xgrid.grid_line_color = None p.ygrid.grid_line_color = None p.xaxis[0].ticker = FixedTicker(ticks=[]) p.yaxis[0].ticker = FixedTicker(ticks=[]) p.outline_line_width = 0 p.outline_line_alpha = 0 p.background_fill_alpha = 0 p.border_fill_alpha = 0 p.xaxis.axis_line_width = 0 p.xaxis.axis_line_color = "white" p.yaxis.axis_line_width = 0 p.yaxis.axis_line_color = "white" p.yaxis.axis_line_alpha = 0 # table if table: tcolumns = [ TableColumn(field='id', title='id', formatter=NumberFormatter(format='0')) ] for prop in data.columns: if prop not in [ "CV1", "CV2", "Cv1", "Cv2", "cv1", "cv2", "colors", 'id', "radii" ]: if data[prop].dtype == 'object': tcolumns.append(TableColumn(field=prop, title=prop)) if data[prop].dtype == 'float64': tcolumns.append( TableColumn( field=prop, title=prop, formatter=NumberFormatter(format='0.00'))) if data[prop].dtype == 'int64': tcolumns.append( TableColumn(field=prop, title=prop, formatter=NumberFormatter(format='0'))) data_table = DataTable(source=datasrc, fit_columns=True, scroll_to_selection=True, columns=tcolumns, name="Property Table", width=table_width, height=table_height) div = Div(text="""<h6><b> Property Table </b> </h6> <br>""", width=600, height=10) if return_datasrc: return plot, oplot, column(widgetbox(div), Spacer(height=10), widgetbox(data_table)), datasrc else: return plot, oplot, column(widgetbox(div), Spacer(height=10), widgetbox(data_table)) else: return plot, oplot
def create_plot(df, title, energy_unit, ylimit=None): """ :param df: :param title: string, plot title :param energy_unit: string, the unit of energy used in the database/model :param ylimit: float/int, upper limit of heatmap colorbar; optional :return: """ if df.empty: return figure() # Round hours and convert to string (required for x-axis) # TODO: figure out a way to handle subhourly data properly! df["hour_of_day"] = df["hour_of_day"].map(int).map(str) # Get list of hours and months (used in xrange/yrange) hours = list(df["hour_of_day"].unique()) months = list(reversed(df["month"].unique())) # Set up color mapper # colors = ["#75968f", "#a5bab7", "#c9d9d3", "#e2e2e2", "#dfccce", # "#ddb7b1", "#cc7878", "#933b41", "#550b1d"] colors = list(reversed(Reds[9])) high = ylimit if ylimit is not None else df.scheduled_curtailment_mwh.max() mapper = LinearColorMapper( palette=colors, low=df.scheduled_curtailment_mwh.min(), high=high ) # Set up the figure plot = figure( plot_width=800, plot_height=500, tools=["pan", "reset", "zoom_in", "zoom_out", "save", "help"], toolbar_location="below", x_axis_location="above", title=title, x_range=hours, y_range=months, ) # Plot heatmap rectangles hm = plot.rect( x="hour_of_day", y="month", width=1, height=1, source=df, fill_color={"field": "scheduled_curtailment_mwh", "transform": mapper}, line_color="white", ) # Add color bar legend color_bar = ColorBar( color_mapper=mapper, major_label_text_font_size="7pt", ticker=BasicTicker(desired_num_ticks=len(colors)), formatter=NumeralTickFormatter(format="0,0"), label_standoff=12, border_line_color=None, location=(0, 0), ) plot.add_layout(color_bar, "right") # Format Axes (labels, number formatting, range, etc.) plot.xaxis.axis_label = "Hour Ending" plot.yaxis.axis_label = "Month" plot.grid.grid_line_color = None plot.axis.axis_line_color = None plot.axis.major_tick_line_color = None plot.axis.major_label_standoff = 0 # Add HoverTool hover = HoverTool( tooltips=[ ("Month", "@month"), ("Hour", "@hour_of_day"), ("Curtailment", "@scheduled_curtailment_mwh{0,0} %s" % energy_unit), ], renderers=[hm], toggleable=True, ) plot.add_tools(hover) return plot
plot_height=450, title='7p3/2', tooltips=TOOLTIPS, x_axis_label='wavelength(nm)', y_axis_label='polarizability', # x_axis_type='linear', # y_axis_type='linear', x_axis_type=None, y_axis_type=None, tools="pan,wheel_zoom,box_zoom,zoom_in,zoom_out,save,hover,crosshair") p.x_range = Range1d(1100, 1800) p.y_range = Range1d(-5000, 5000) # ticker = SingleIntervalTicker(interval=100, desired_num_ticks=40,num_minor_ticks=10) ticker = BasicTicker(max_interval=100, min_interval=10, desired_num_ticks=10) # ticker = ContinuousTicker(desired_num_ticks=20,num_minor_ticks=10) xaxis = LinearAxis(ticker=ticker) # p.xaxis.visible = True xaxis.axis_label = "wavelength (nm)" xaxis.axis_line_width = 1 xaxis.axis_label_text_font_style = "italic" p.add_layout(xaxis, 'below') # tickery = SingleIntervalTicker(interval=1000, num_minor_ticks=20) tickery = BasicTicker(max_interval=1000, min_interval=100, desired_num_ticks=10) yaxis = LinearAxis(ticker=tickery)
def __init__(self, scheduler, width=600, **kwargs): with log_errors(): self.last = 0 self.scheduler = scheduler self.source = ColumnDataSource({ "memory": [1, 2], "memory-half": [0.5, 1], "memory_text": ["1B", "2B"], "utilization": [1, 2], "utilization-half": [0.5, 1], "worker": ["a", "b"], "gpu-index": [0, 0], "y": [1, 2], "escaped_worker": ["a", "b"], }) memory = figure( title="GPU Memory", tools="", id="bk-gpu-memory-worker-plot", width=int(width / 2), name="gpu_memory_histogram", **kwargs, ) rect = memory.rect( source=self.source, x="memory-half", y="y", width="memory", height=1, color="#76B900", ) rect.nonselection_glyph = None utilization = figure( title="GPU Utilization", tools="", id="bk-gpu-utilization-worker-plot", width=int(width / 2), name="gpu_utilization_histogram", **kwargs, ) rect = utilization.rect( source=self.source, x="utilization-half", y="y", width="utilization", height=1, color="#76B900", ) rect.nonselection_glyph = None memory.axis[0].ticker = BasicTicker(**TICKS_1024) memory.xaxis[0].formatter = NumeralTickFormatter(format="0.0 b") memory.xaxis.major_label_orientation = -math.pi / 12 memory.x_range.start = 0 for fig in [memory, utilization]: fig.xaxis.minor_tick_line_alpha = 0 fig.yaxis.visible = False fig.ygrid.visible = False tap = TapTool(callback=OpenURL( url="./info/worker/@escaped_worker.html")) fig.add_tools(tap) fig.toolbar.logo = None fig.toolbar_location = None fig.yaxis.visible = False hover = HoverTool() hover.tooltips = "@worker : @utilization %" hover.point_policy = "follow_mouse" utilization.add_tools(hover) hover = HoverTool() hover.tooltips = "@worker : @memory_text" hover.point_policy = "follow_mouse" memory.add_tools(hover) self.memory_figure = memory self.utilization_figure = utilization self.utilization_figure.y_range = memory.y_range self.utilization_figure.x_range.start = 0 self.utilization_figure.x_range.end = 100
def index(): # narr_erc_all = narr_erc.query.limit(5).all() # This returns a list of objects. Pass that list of objects to your template using jinga. # narr_erc_lat = narr_erc.query.filter_by(lat='39.2549').first() # narr_erc_date = narr_erc.query.filter(func.date(narr_erc.date) <= '1979-01-02').all() # df = pd.read_sql(session.query(narr_erc).filter(func.date(narr_erc.date) <= '1979-01-02').statement, session.bind) # df = pd.read_sql(session.query(narr_erc).filter(func.date(narr_erc.date) <= '1979-01-02').statement, session.bind) #db.session.add(narr_erc_lat_lon) #db.session.commit() # fetchall() is one way to get data from a cursor after a query # results = cur.fetchall() conn = connect_to_db() # ------------------------------------------- # # QUERY: TIME SERIES OF H500, ERC AT ONE LOCATION # # Reading data into a list object 'results' directly from postgres fire_weather_db: # cur = conn.cursor() # sql = 'select id, lat, lon, date, h500, erc from narr_erc \ # where lat = 39.2549 and lon = 236.314 \ # order by id' # df = pd.read_sql(sql, conn) # cur.close() # conn.close() # source = ColumnDataSource(df) # ------------------------------------------- # # PLOTTING H500 TIME SERIES # p = figure( # x_axis_type = 'datetime', # plot_width = 800, # plot_height = 600, # # y_range = h500_list, # title = 'H500 Time Series', # x_axis_label = 'Date', # y_axis_label = 'Geopotential height, gpm', # tools = 'pan,zoom_in,zoom_out,save,reset', # ) # # p.line( # source = source, # x = 'date', # y = 'h500', # line_color = 'green', # legend = 'H500', # line_width = 2 # ) # ------------------------------------------- # # PLOTTING ERC TIME SERIES # p = figure( # x_axis_type = 'datetime', # plot_width = 800, # plot_height = 600, # # y_range = h500_list, # title = 'ERC Time Series', # x_axis_label = 'Date', # y_axis_label = 'ERC, AU', # tools = 'pan,zoom_in,zoom_out,save,reset', # ) # # p.line( # source = source, # x = 'date', # y = 'erc', # line_color = 'red', # legend = 'ERC', # line_width=2 # ) # ------------------------------------------- # SQL QUERY: H500 CONTOUR SINGLE DATE # Reading data into a list object 'results' directly from postgres fire_weather_db: cur = conn.cursor() sql = "select id, lat, lon, date, h500, h500_grad_x, pmsl, pmsl_grad_x, pmsl_grad_y, erc from narr_erc \ where cast(date as date) = '1979-05-15' \ order by id" df = pd.read_sql(sql, conn) cur.close() conn.close() source = ColumnDataSource(df) # ------------------------------------------- # # PLOTTING NARR GRID # x = df['lon'] # y = df['lat'] # p = figure( # plot_width = 800, # plot_height = 600, # title = 'NARR Grid', # x_axis_label = 'Lon', # y_axis_label = 'Lat', # tools = 'pan,zoom_in,zoom_out,save,reset', # ) # p.circle(x, y, size=2, color="black", alpha=0.5) # ------------------------------------------- # PLOTTING H500 CONTOUR var = 'pmsl_grad_x' # e.g. 'h500', 'h500_grad_x', 'erc' var_title = 'PMSL - X Gradient' # e.g. 'H500', 'H500 - X Gradient', 'ERC' lon = df['lon'].drop_duplicates('first').to_numpy() lat = df['lat'].drop_duplicates('first').to_numpy() lonlon, latlat = np.meshgrid(lon, lat) mesh_shape = np.shape(lonlon) # Change -32767 to 0: if var == 'erc': criteria = df[df['erc'] == -32767].index df['erc'].loc[criteria] = 0 d = df[var].to_numpy().reshape(mesh_shape) var_list = df[var].values.tolist() var_min = min(var_list) var_max = max(var_list) lon_min = np.min(lon) lon_max = np.max(lon) dw = lon_max - lon_min lat_min = np.min(lat) lat_max = np.max(lat) dh = lat_max - lat_min p = figure( #toolbar_location="left", title=var_title, plot_width=580, plot_height=600, tooltips=[("lon", "$lon"), ("lat", "$lat"), ("value", "@image")], x_range=(lon_min, lon_max), y_range=(lat_min, lat_max), x_axis_label='Longitude, deg', y_axis_label='Latitude, deg') if var == 'h500_grad_x' or 'h500_grad_y' or 'pmsl_grad_x' or 'pmsl_grad_y': # Color maps that make 0 values clear color_mapper = LinearColorMapper(palette=cividis(256), low=var_min, high=var_max) else: color_mapper = LinearColorMapper(palette="Inferno256", low=var_min, high=var_max) # Decent color map: "Spectra11", "Viridis256" # Giving a vector of image data for image parameter (contour plot) p.image(image=[d], x=lon_min, y=lat_min, dw=dw, dh=dh, color_mapper=color_mapper) # p.x_range.range_padding = p.y_range.range_padding = 0 color_bar = ColorBar( color_mapper=color_mapper, ticker=BasicTicker(), label_standoff=12, border_line_color=None, location=(0, 0), ) p.add_layout(color_bar, 'right') # get state boundaries from state map data imported from Bokeh state_lats = [states[code]["lats"] for code in states] state_lons = [states[code]["lons"] for code in states] # add 360 to adjust lons to NARR grid state_lons = np.array([np.array(sublist) for sublist in state_lons]) state_lons += 360 p.patches(state_lons, state_lats, fill_alpha=0.0, line_color="black", line_width=2, line_alpha=0.3) select = Select(title="Weather Variable:", value="H500", options=[ "H500", "H500 X Gradient", "H500 Y Gradient", "PMSL", "PMSL X Gradient", "PMSL Y Gradient", "Energy Release Component" ]) # slider = Slider(start=DateTime(1979,1,2), end=DateTime(1979,12,31), value=DateTime(1979,1,2), step=1, title="Date") slider = Slider(start=1, end=365, step=10, title="Date") # def callback(attr, old, new): # points = slider.value # data_points.data = {'x': random(points), 'y': random(points)} # slider.on_change('value', callback) widget_layout = widgetbox(slider, select) layout = row(slider, p) curdoc().add_root(widget_layout) # To run on bokeh server: # bokeh serve --show fwp_app.py # # Limit the view to the min and max of the building data # p.x_range = DataRange1d(lon_min, lon_max) # p.y_range = DataRange1d(lat_min, lat_max) # p.xaxis.visible = False # p.yaxis.visible = False p.xgrid.grid_line_color = None p.ygrid.grid_line_color = None # show(p) # output_file("image.html", title="image.py example") # show(p) # open a browser script, div = components(p) # The data below is passed to add_user_fwp.html to run when localhost:5000/ is opened. # return render_template('add_user_fwp.html', narr_erc_all=narr_erc_all, narr_erc_lat=narr_erc_lat, narr_erc_date=narr_erc_date, script=script, div=div) return render_template('fwp_bokeh_render.html', script=script, div=div, widget_layout=widget_layout)
"angle": 0, "color": "black", "text_align": "left", "text_baseline": "middle" } x = dodge("group", -0.4, range=p.x_range) y = dodge("period", 0.3, range=p.y_range) p.text(x=x, y="period", text="sym", text_font_style="bold", text_font_size="16pt", **text_props) p.text(x=x, y=y, text="atomic_number", text_font_size="11pt", **text_props) color_bar = ColorBar(color_mapper=color_mapper, ticker=BasicTicker(desired_num_ticks=10), border_line_color=None, label_standoff=cbar_standoff, location=(0, 0), orientation="vertical", scale_alpha=alpha, major_label_text_font_size=str(cbar_fontsize) + "pt") if cbar_height is not None: color_bar.height = cbar_height p.add_layout(color_bar, "right") p.grid.grid_line_color = None show(p)
def __init__(self, corr_data): self.matrix_data = pd.DataFrame(columns=self.strikes, index=self.expirations) for e in self.expirations: self.matrix_data.loc[e] = np.sin(self.strikes) * random() self.matrix_data.columns.name = 'STRIKES' self.matrix_data.index.name = 'EXPIRATIONS' p = figure(x_range=(0, len(self.strikes)), y_range=(0, len(self.expirations))) self.matrix_data = self.matrix_data.stack().rename( "value").reset_index() #self.matrix_data.to_pickle("/Users/Uriel/Documents/Python/Ibis/Gemini/matrix_data.pkl") #create plot colors = [ "#75968f", "#a5bab7", "#c9d9d3", "#e2e2e2", "#dfccce", "#ddb7b1", "#cc7878", "#933b41", "#550b1d" ] mapper = LinearColorMapper(palette=colors, low=-1, high=1) colors2 = colors[::-1] self.p = figure(plot_width=600, plot_height=600, title="My plot", x_range=list(self.matrix_data.A.drop_duplicates()), y_range=list( self.matrix_data.B.drop_duplicates())[::-1], toolbar_location=None, tools="hover", tooltips=[('coef:', '@value')], x_axis_location="above") self.p.rect(x="A", y="B", width=1, height=1, source=ColumnDataSource(self.matrix_data), line_color=None, fill_color=transform('value', mapper)) self.p.text(x="A", y="B", text=str("value"), source=ColumnDataSource(self.matrix_data), text_color='black', text_font_size='8pt', x_offset=-15, text_align="left") color_bar = ColorBar(color_mapper=mapper, location=(0, 0), ticker=BasicTicker(desired_num_ticks=len(colors))) def checkbox_group_callback(attr, old, new): print(corr_data.columns[new]) self.checkbox_group = CheckboxGroup(labels=list(corr_data.columns), active=[0, 1]) #show(widgetbox(checkbox_group)) self.checkbox_group.on_change('active', checkbox_group_callback) self.p.add_layout(color_bar, 'right') layout = row(self.p, widgetbox(self.checkbox_group))
def plot_steps(apple_watch): """ Generate grid heat map of step counts for a given hour and date, grouped by start timestamp. :param apple_watch: instance of apple watch data object :return: None """ logger.info('Loading and Generating Steps Heat Map') df = apple_watch.load_step_data() df = df[(df['start_timestamp'] > START_DATE) & (df['start_timestamp'] < END_DATE)] df['date'] = list( map(lambda d: d.strftime('%m/%d/%y'), df['start_timestamp'])) df['hour'] = list( map(lambda d: int(d.strftime('%H')), df['start_timestamp'])) # group by hour and date and calculate sum of steps step_counts = df.groupby(['hour', 'date'])['steps'].agg(['sum']).reset_index() step_counts.rename(columns={'sum': 'steps'}, inplace=True) # resort by date step_counts['datetime'] = pd.to_datetime(step_counts['date']) step_counts.sort_values(by=['datetime'], inplace=True) dates = step_counts['date'].unique() # create grid heat map of hourly counts grouped by date # follow example from https://bokeh.pydata.org/en/latest/docs/gallery/unemployment.html TOOLS = "hover,save,pan,box_zoom,reset,wheel_zoom" colors = [ "#000080", "#1874CD", "#63B8FF", "#C6E2FF", "#E6E6FA", "#dfccce", "#ddb7b1", "#cc7878", "#933b41" ] mapper = LinearColorMapper(palette=colors, low=step_counts.steps.min(), high=step_counts.steps.max()) source = ColumnDataSource(step_counts) plot = figure(title="Apple Watch Hourly Step Counts", x_range=[str(h) for h in range(24)], y_range=list(dates), x_axis_location="below", plot_width=1000, plot_height=600, tools=TOOLS, toolbar_location='above', sizing_mode='scale_both') plot.grid.grid_line_color = None plot.axis.axis_line_color = None plot.axis.major_tick_line_color = None plot.xaxis.axis_label_text_font_size = '14pt' plot.xaxis.major_label_text_font_size = '12pt' plot.yaxis.axis_label_text_font_size = '14pt' plot.yaxis.major_label_text_font_size = '12pt' plot.title.text_font_size = '16pt' plot.rect(x='hour', y='date', width=1, height=1, source=source, fill_color={ 'field': 'steps', 'transform': mapper }, line_color=None) color_bar = ColorBar(color_mapper=mapper, major_label_text_font_size="10pt", ticker=BasicTicker(desired_num_ticks=len(colors)), formatter=PrintfTickFormatter(format="%d"), label_standoff=6, border_line_color=None, location=(0, 0)) plot.add_layout(color_bar, 'right') plot.select_one(HoverTool).tooltips = [('date', '@date'), ('hour', '@hour'), ('count', '@steps')] if SHOW_PLOTS: show(plot, browser='chrome') save_plot(plot, 'step_counts') # clear output mode for next plot reset_output() # save data frame df.to_csv('apple_watch_data/step_counts.csv', index=False)
def _contour_data(self): """ Create a contour plot. Parameters ---------- None Returns ------- Bokeh Image Plot """ resolution = self.resolution # Output data array initialization y_data = np.zeros((resolution, resolution, self.num_outputs)) self.input_point_list = [point.value for point in self.slider_dict.values()] # Pass the dict to make predictions and then reshape the output to # (resolution, resolution, number of outputs) y_data[:, :, :] = self._make_predictions(self._contour_data_calcs()).reshape( (resolution, resolution, self.num_outputs)) # Use the output variable to pull the correct column of data from the predicted # data (y_data) self.Z = y_data[:, :, self.output_variable] # Reshape it to be 2D self.Z = self.Z.reshape(resolution, resolution) # Update the data source with new data self.contour_plot_source.data = dict(z=[self.Z]) # Min to max of training data self.contour_x_range = xlins = self.xlins_mesh self.contour_y_range = ylins = self.ylins_mesh # Color bar formatting color_mapper = LinearColorMapper( palette="Viridis11", low=np.amin(self.Z), high=np.amax(self.Z)) color_bar = ColorBar(color_mapper=color_mapper, ticker=BasicTicker(), label_standoff=12, location=(0, 0)) # Contour Plot self.contour_plot = contour_plot = figure( match_aspect=False, tooltips=[(self.x_input_select.value, "$x"), (self.y_input_select.value, "$y"), (self.output_select.value, "@z")], tools='') contour_plot.x_range.range_padding = 0 contour_plot.y_range.range_padding = 0 contour_plot.plot_width = 600 contour_plot.plot_height = 500 contour_plot.xaxis.axis_label = self.x_input_select.value contour_plot.yaxis.axis_label = self.y_input_select.value contour_plot.min_border_left = 0 contour_plot.add_layout(color_bar, 'right') contour_plot.x_range = Range1d(min(xlins), max(xlins)) contour_plot.y_range = Range1d(min(ylins), max(ylins)) contour_plot.image(image='z', source=self.contour_plot_source, x=min(xlins), y=min(ylins), dh=(max(ylins) - min(ylins)), dw=(max(xlins) - min(xlins)), palette="Viridis11") # Adding training data points overlay to contour plot if self.is_structured_meta_model: data = self._structured_training_points() else: data = self._unstructured_training_points() if len(data): # Add training data points overlay to contour plot data = np.array(data) if self.is_structured_meta_model: self.contour_training_data_source.data = dict(x=data[:, 0], y=data[:, 1], z=self.meta_model.training_outputs[ self.output_select.value].flatten()) else: self.contour_training_data_source.data = dict(x=data[:, 0], y=data[:, 1], z=self.meta_model._training_output[ self.output_select.value]) training_data_renderer = self.contour_plot.circle( x='x', y='y', source=self.contour_training_data_source, size=5, color='white', alpha=0.50) self.contour_plot.add_tools(HoverTool(renderers=[training_data_renderer], tooltips=[ (self.x_input_select.value + " (train)", '@x'), (self.y_input_select.value + " (train)", '@y'), (self.output_select.value + " (train)", '@z'), ])) return self.contour_plot
def plotMatrix(self, npy, fileName, verbose, scale=None, residueMapName=None): # Rescales matrix if a scale/length is specified if scale is not None: npy = self.rescaleMatrix(npy, scale) # TODO: Add tool tips # Interactive Plot Tools TOOLS = 'hover,save,pan,box_zoom,reset,wheel_zoom' # Defining color values vmin = -5 vmax = 5 # New Color Map Bokeh blueRedColors = [ '#FF0000', '#FF1111', '#FF2222', '#FF3333', '#FF4444', '#FF5555', '#FF6666', '#FF7777', '#FF8888', '#FF9999', '#FFAAAA', '#FFBBBB', '#FFCCCC', '#FFDDDD', '#FFEEEE', '#FFFFFF', '#EEEEFF', '#DDDDFF', '#CCCCFF', '#BBBBFF', '#AAAAFF', '#9999FF', '#8888FF', '#7777FF', '#6666FF', '#5555FF', '#4444FF', '#3333FF', '#2222FF', '#1111FF', '#0000FF' ] #vmin = np.amin(npy) #vmax = np.amax(npy) # Reformatting data for plotting xyPairList = [None] * npy.shape[0] * npy.shape[1] # Creating list for i in range(0, npy.shape[0]): for j in range(0, npy.shape[1]): xyPairList[i + j * npy.shape[0]] = (i, j) if residueMapName is not None: # Loading map from covariance index to residue pairs distMap = np.transpose( list( np.load(residueMapName, allow_pickle=True).item().values())) # Moving residue pairs along one axis into covMapX covMapX = np.array([[(i, j) for i in distMap[0]] for j in distMap[0]]) covMapX = covMapX.reshape(covMapX.shape[0] * covMapX.shape[1], covMapX.shape[2]) # Moving residue pairs along the other axis into covMapY covMapY = np.array([[(i, j) for i in distMap[1]] for j in distMap[1]]) covMapY = covMapY.reshape(covMapY.shape[0] * covMapY.shape[1], covMapY.shape[2]) # Defining fields to be displayed in hover tooltips source = ColumnDataSource( data={ 'x': np.transpose(xyPairList)[0], 'y': np.transpose(xyPairList)[1], 'covValues': npy.flatten(), 'covMapX': covMapX, 'covMapY': covMapY }) tooltipList = [('xCoord', '@x'), ('yCoord', '@y'), ('Covariance Value', '@covValues'), ('xResiduePair', '@covMapX'), ('yResiduePair', '@covMapY')] else: # Defining fields to be displayed in hover tooltips source = ColumnDataSource( data={ 'x': np.transpose(xyPairList)[0], 'y': np.transpose(xyPairList)[1], 'covValues': npy.flatten() }) tooltipList = [('xCoord', '@x'), ('yCoord', '@y'), ('Distance Difference Value', '@covValues')] # Plotting color_mapper = LinearColorMapper(palette=blueRedColors, low=vmin, high=vmax) plot = figure(x_range=(-0.5, len(npy) - 0.5), y_range=(-0.5, len(npy) - 0.5), tools=TOOLS, toolbar_location='below', tooltips=tooltipList) plot.rect(x='x', y='y', width=1, height=1, source=source, fill_color={ 'field': 'covValues', 'transform': color_mapper }, line_color=None) color_bar = ColorBar(color_mapper=color_mapper, label_standoff=12, border_line_color=None, location=(0,0), ticker=BasicTicker(\ desired_num_ticks=len(blueRedColors))) plot.add_layout(color_bar, 'right') """ plot.add_tools(HoverTool( tooltips=[('covValue', '@covValues')] )) """ output_file(fileName + '.html') show(plot) print('Computation complete, plot outputted to: '\ + fileName + '.html')
def __init__(self, scheduler, width=600, **kwargs): with log_errors(): self.last = 0 self.scheduler = scheduler self.source = ColumnDataSource({ 'nprocessing': [1, 2], 'nprocessing-half': [0.5, 1], 'nprocessing-color': ['red', 'blue'], 'nbytes': [1, 2], 'nbytes-half': [0.5, 1], 'nbytes_text': ['1B', '2B'], 'worker': ['a', 'b'], 'y': [1, 2], 'nbytes-color': ['blue', 'blue'], 'bokeh_address': ['', ''] }) processing = figure(title='Tasks Processing', tools='', id='bk-nprocessing-plot', width=int(width / 2), **kwargs) rect = processing.rect(source=self.source, x='nprocessing-half', y='y', width='nprocessing', height=1, color='nprocessing-color') processing.x_range.start = 0 rect.nonselection_glyph = None nbytes = figure(title='Bytes stored', tools='', id='bk-nbytes-worker-plot', width=int(width / 2), **kwargs) rect = nbytes.rect(source=self.source, x='nbytes-half', y='y', width='nbytes', height=1, color='nbytes-color') rect.nonselection_glyph = None nbytes.axis[0].ticker = BasicTicker(mantissas=[1, 256, 512], base=1024) nbytes.xaxis[0].formatter = NumeralTickFormatter(format='0.0 b') nbytes.xaxis.major_label_orientation = -math.pi / 12 nbytes.x_range.start = 0 for fig in [processing, nbytes]: fig.xaxis.minor_tick_line_alpha = 0 fig.yaxis.visible = False fig.ygrid.visible = False tap = TapTool(callback=OpenURL(url='http://@bokeh_address/')) fig.add_tools(tap) fig.toolbar.logo = None fig.toolbar_location = None fig.yaxis.visible = False hover = HoverTool() hover.tooltips = "@worker : @nprocessing tasks" hover.point_policy = 'follow_mouse' processing.add_tools(hover) hover = HoverTool() hover.tooltips = "@worker : @nbytes_text bytes" hover.point_policy = 'follow_mouse' nbytes.add_tools(hover) self.processing_figure = processing self.nbytes_figure = nbytes processing.y_range = nbytes.y_range self.root = row(nbytes, processing, sizing_mode='scale_width')