def sc_plot_for_one(mdf, imdbID: str, gray_out=False): """Creates and colorizes a scatterplot of all movies in the dataset, with one IMDB entry specified as the one in question. That one has a big X over it. If gray_out is True: All the movies that are in the same genre as the specified film are colorized but everything else is grayed out. Parameters ---------- mdf : Pandas DataFrame A DataFrame merged between the movies df and the omdb data imdbID : str IMDB id, does not include tt, but is the number after Returns ------- Bokeh.Figure Description of returned object. """ # for now, this populates random values, but eventually, this should use the # first two principal components of the Doc2Vec model to populate the x and y vars mdf["x"] = pd.np.random.random(size=len(mdf)) * 10000 mdf["y"] = pd.np.random.random(size=len(mdf)) * 10000 mdf = mdf[mdf.top_genre.notna()].sample(frac=0.2) item = mdf.iloc[0].to_dict() # item = mdf.loc[mdf.imdb_id == imdbID].to_dict(orient="records")[0] # colormap = dict( # zip(mdf.top_genre.unique(), Category20[len(mdf.top_genre.unique())]) # ) colormap = dict( zip( mdf.top_genre.unique(), (np.random.choice( list( set(palettes.Magma256 + palettes.Viridis256 + palettes.cividis(18))), size=mdf.top_genre.nunique(), replace=False, )), )) genre_of_item = item["primary_genre"] color_of_item = colormap[item["primary_genre"]] if gray_out: everything_else = "gray" colormap = dict(zip(colormap.keys(), ["gray"] * len(colormap))) grayed_out_colormap[genre_of_item] = color_of_item mdf["color"] = mdf.top_genre.map(lambda x: grayed_out_colormap[x]) else: mdf["color"] = mdf.top_genre.map(lambda x: colormap[x]) p = sc_plot_genre_colors(mdf, colormap) p.circle_x(x=item["x"], y=item["y"], size=20, fill_color=color_of_item, line_color="red") return p
def prep_palette(self, pname, binverse=False): """ Prepares a palette based on a name :param pname: :return: """ res = palettes.grey(256) if pname == 'Greys256': res = palettes.grey(256) elif pname == 'Inferno256': res = palettes.inferno(256) elif pname == 'Magma256': res = palettes.magma(256) elif pname == 'Plasma256': res = palettes.plasma(256) elif pname == 'Viridis256': res = palettes.viridis(256) elif pname == 'Cividis256': res = palettes.cividis(256) elif pname == 'Turbo256': res = palettes.turbo(256) elif pname == 'Bokeh8': res = palettes.small_palettes['Bokeh'][8] elif pname == 'Spectral11': res = palettes.small_palettes['Spectral'][11] elif pname == 'RdGy11': res = palettes.small_palettes['RdGy'][11] elif pname == 'PiYG11': res = palettes.small_palettes['PiYG'][11] if binverse: res = res[::-1] return res
def plot_top_five(frequent_feat): x = list(frequent_feat.Word) y = list(frequent_feat.Mean) print(f'x for hyderabadi biryani is {x}') print(f'y is {y}') p = figure(x_range=x, plot_height=400, toolbar_location=None, tools="") p = style(p) p.vbar(x=x, top=y, width=0.3, color=cividis(5)) return p
def sc_plot_genre_colors(mdf, colormap=None): """Uses the full dataset, represents all movies in dataset using vectorized placement and colorized by genre. Parameters ---------- mdf : Pandas DataFrame A DataFrame merged between the movies df and the omdb data Returns ------- bokeh.Figure The figure object """ # for now, this populates random values, but eventually, this should use the # first two principal components of the Doc2Vec model to populate the x and y vars mdf["x"] = pd.np.random.random(size=len(mdf)) * 10000 mdf["y"] = pd.np.random.random(size=len(mdf)) * 10000 # only want to use it to plot if there is a genre attached mdf = mdf[mdf.top_genre.notna()].sample(frac=0.2) if not colormap: colormap = dict( zip( mdf.top_genre.unique(), (np.random.choice( list( set(palettes.Magma256 + palettes.Viridis256 + palettes.cividis(18))), size=mdf.top_genre.nunique(), replace=False, )), )) mdf["color"] = mdf.top_genre.map(lambda j: colormap[j]) source = ColumnDataSource(data=dict( x=mdf.x, y=mdf.y, name=mdf.parsed_title, year=mdf.year, genre=mdf.top_genre, color=mdf.color, )) TOOLTIPS = [("name", "@name"), ("year", "@year"), ("genre", "@genre")] p = figure(tooltips=TOOLTIPS, title="Movie Clusters") p.circle("x", "y", size=12, alpha=0.6, color="color", source=source) return p
def run_model(self, play_button, circles, lines): ''' generate and run the model ''' batch_size = 128 #display_step = 50 loss_arr = [] acc_arr = [] progress_bar_length = 10 self.neural_nets() self.set_optimizers() cividis_colors = cividis(128)[55:87] cividis_colors += list(reversed(cividis_colors)) cividis_colors = [item for item in cividis_colors for _ in range(4)] init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) for step in range(1, self.epochs + 1): batch_x, batch_y = mnist.train.next_batch(batch_size) sess.run(self.train_op, feed_dict={X: batch_x, Y: batch_y}) loss, acc = sess.run([self.loss_op, self.accuracy], feed_dict={ X: batch_x, Y: batch_y }) ''' if step % display_step == 0 or step == 1: print("Step " + str(step) + ", Minibatch Loss= " + \ "{:.4f}".format(loss) + ", Training Accuracy= " + \ "{:.3f}".format(acc)) ''' lines.glyph.line_color = cividis_colors[step % 256] circles.glyph.fill_color = circles.glyph.line_color = cividis_colors[ step % 256] text = "\r Bekleyiniz: [" + "+" * int(round(progress_bar_length * step/self.epochs))\ + '-' * (progress_bar_length - int(round(progress_bar_length * step/self.epochs)))\ + "] " + str(round(step/self.epochs * 100, 1)) + "%" play_button.label = text loss_arr.append(loss) acc_arr.append(acc) play_button.label = "Oynat" testing_acc = sess.run(self.accuracy, feed_dict={ X: mnist.test.images, Y: mnist.test.labels }) lines.glyph.line_color = "darkgray" circles.glyph.fill_color = circles.glyph.line_color = "lightseagreen" return testing_acc, loss_arr, acc_arr
def get_plot_script(df, ticker, name, plot_indeces=['Open', 'Close']): ''' accepts pandas dataframe with index Date and, by default, column 'Open' ''' def find_ylims(df, plot_indeces, from_date, to_date): buff = 2 #ymin = 0.01 #penny is the smallest? ymax = df[np.logical_and( df['Date'] > from_date, df['Date'] < to_date)][plot_indeces].max().max() ymin = df[np.logical_and( df['Date'] > from_date, df['Date'] < to_date)][plot_indeces].min().min() return (ymin / buff, buff * ymax) def find_xlims(from_date, to_date): ''' specify the month, hardcoded, could upgrade ''' xmin = pd.to_datetime(from_date) xmax = pd.to_datetime(to_date) return (xmin, xmax) (from_date, to_date) = (pd.to_datetime('2018-01-01'), pd.to_datetime('2018-02-01')) fig = figure(title='{} share behaviour, {}'.format(name, from_date.date()), x_axis_label='Time', y_axis_label='Value', x_axis_type='datetime', y_axis_type='log', y_range=(find_ylims(df, plot_indeces, from_date, to_date)), x_range=find_xlims(from_date, to_date)) #TODO select subframe correctly using plot_indeces... colors = cividis(len(plot_indeces)) for i in range(len(plot_indeces)): index = plot_indeces[i] fig.line(df['Date'], df[index], color=colors[i], legend=index) script, div = components(fig) return script, div
def create_plot(df, title, power_unit, tech_colors={}, tech_plotting_order={}, ylimit=None): """ :param df: :param title: string, plot title :param power_unit: string, the unit of power used in the database/model :param tech_colors: optional dict that maps technologies to colors. Technologies without a specified color map will use a default palette :param tech_plotting_order: optional dict that maps technologies to their plotting order in the stacked bar/area chart. :param ylimit: float/int, upper limit of y-axis; optional :return: """ # Re-arrange df according to plotting order for col in df.columns: if col not in tech_plotting_order: tech_plotting_order[col] = max(tech_plotting_order.values()) + 1 df = df.reindex(sorted(df.columns, key=lambda x: tech_plotting_order[x]), axis=1) # Set up data source source = ColumnDataSource(data=df) # Determine column types for plotting, legend and colors # Order of stacked_cols will define order of stacked areas in chart all_cols = list(df.columns) x_col = "x" # TODO: remove hard-coding? line_cols = ["Load", "Exports", "Storage_Charging"] stacked_cols = [c for c in all_cols if c not in line_cols + [x_col]] # Set up color scheme. Use cividis palette for unspecified colors unspecified_columns = [c for c in stacked_cols if c not in tech_colors.keys()] unspecified_tech_colors = dict(zip(unspecified_columns, cividis(len(unspecified_columns)))) colors = [] for tech in stacked_cols: if tech in tech_colors: colors.append(tech_colors[tech]) else: colors.append(unspecified_tech_colors[tech]) # Set up the figure plot = figure( plot_width=800, plot_height=500, tools=["pan", "reset", "zoom_in", "zoom_out", "save", "help"], title=title, # sizing_mode="scale_both" ) # Add stacked area chart to plot area_renderers = plot.vbar_stack( stackers=stacked_cols, x=x_col, source=source, color=colors, width=1, ) # Note: can easily change vbar_stack to varea_stack by replacing the plot # function and removing the width argument. However, hovertools don't work # with varea_stack. # Add load line chart to plot load_renderer = plot.line( x=df[x_col], y=df[line_cols[0]], line_color="black", line_width=2, name="Load" ) # Keep track of legend items and load renderers legend_items = [(x, [area_renderers[i]]) for i, x in enumerate(stacked_cols) if df[x].mean() > 0] + [("Load", [load_renderer])] load_renderers = [load_renderer] # Add 'Load + ...' lines if line_cols[1] not in df.columns: inactive_exports = True else: inactive_exports = (df[line_cols[1]] == 0).all() inactive_storage = (df[line_cols[2]] == 0).all() if inactive_exports: line_cols = [line_cols[0], line_cols[2]] else: # Add export line to plot label = "Load + Exports" exports_renderer = plot.line( x=df[x_col], y=df[line_cols[0:2]].sum(axis=1), line_color="black", line_width=2, line_dash="dashed", name=label ) legend_items.append((label, [exports_renderer])) load_renderers.append(exports_renderer) if not inactive_storage: # Add storage line to plot label = legend_items[-1][0] + " + Storage Charging" stor_renderer = plot.line( x=df[x_col], y=df[line_cols].sum(axis=1), line_color="black", line_width=2, line_dash="dotted", name=label ) legend_items.append((label, [stor_renderer])) load_renderers.append(stor_renderer) # Add Legend legend = Legend(items=legend_items) plot.add_layout(legend, 'right') plot.legend[0].items.reverse() # Reverse legend to match stacked order plot.legend.click_policy = 'hide' # Add interactivity to the legend # Note: Doesn't rescale the graph down, simply hides the area # Note2: There's currently no way to auto-size legend based on graph size(?) # except for maybe changing font size automatically? show_hide_legend(plot=plot) # Hide legend on double click # Format Axes (labels, number formatting, range, etc.) plot.xaxis.axis_label = "Hour Ending" plot.yaxis.axis_label = "Dispatch ({})".format(power_unit) plot.yaxis.formatter = NumeralTickFormatter(format="0,0") plot.y_range.end = ylimit # will be ignored if ylimit is None # Add HoverTools for stacked bars/areas for r in area_renderers: power_source = r.name hover = HoverTool( tooltips=[ ("Hour Ending", "@x"), ("Source", power_source), ("Dispatch", "@%s{0,0} %s" % (power_source, power_unit)) ], renderers=[r], toggleable=False) plot.add_tools(hover) # Add HoverTools for load lines for r in load_renderers: load_type = r.name hover = HoverTool( tooltips=[ ("Hour Ending", "@x"), (load_type, "@y{0,0} %s" % power_unit), ], renderers=[r], toggleable=False) plot.add_tools(hover) return plot
# Bokeh provides a way to create interactive plots (Enabled by JS) !pip install -q bokeh from bokeh.io import show, output_file, push_notebook, output_notebook from bokeh.models import ColumnDataSource, HoverTool, NumeralTickFormatter from bokeh.palettes import cividis from bokeh.plotting import figure from random import shuffle output_notebook () # You have can use output_file() as well xx = list(dfDpto.dptoName) yy = list(dfDpto.CEN) colorList = cividis(len(xx)) shuffle(colorList) source = ColumnDataSource( data = dict(xdata = xx , ydata=yy, color=colorList) ) tools = "pan,wheel_zoom,box_zoom,reset,save".split(',') hover = HoverTool(tooltips=[ ("Dpto:", "@xdata"), ("Censo:", "@ydata") ]) tools.append(hover) p = figure(
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)
def create_stacked_bar_plot(source, x_col, x_label=None, y_label=None, category_label="Category", category_colors={}, category_order={}, title=None, ylimit=None, sizing_mode="fixed"): """ Create a stacked bar chart from a Bokeh ColumnDataSource (CDS). The CDS should have have a "x_col" column where each element is either a string for a simple stacked bar charts or a tuple of strings for a grouped stacked bar chart. Note that strings are required for the categorical axis. All other columns are assumed to be categories that will be stacked in the bar chart. :param source: Bokeh ColumnDataSource :param x_col: str :param x_label: str, optional (defaults to x_col) :param y_label: str, optional (defaults to no label) :param category_label: str, optional :param category_colors: dict, optional, maps categories to colors. Categories without a specified color will use a default palette :param category_order: dict, optional, maps categories to their plotting order in the stacked bar chart (lower = bottom) :param title: string, optional, plot title :param ylimit: float/int, optional, upper limit of y-axis :param sizing_mode: Bokeh layout/figure sizing mode, default 'fixed' :return: """ # Determine column types for plotting, legend and colors # Order of stacked_cols will define order of stacked areas in chart cols = list(source.data.keys()) cols.remove(x_col) for col in cols: if col not in category_order: category_order[col] = max(category_order.values(), default=0) + 1 stacked_cols = sorted(cols, key=lambda x: category_order[x]) # Set up color scheme. Use cividis palette for unspecified colors unspecified_columns = [ c for c in stacked_cols if c not in category_colors.keys() ] unspecified_category_colors = dict( zip(unspecified_columns, cividis(len(unspecified_columns)))) colors = [] for column in stacked_cols: if column in category_colors: colors.append(category_colors[column]) else: colors.append(unspecified_category_colors[column]) # Determine whether we are dealing with a grouped x_axis (tuple values) grouped_x = False try: if isinstance(source.data[x_col][0], tuple): grouped_x = True except IndexError: # If there is no data, grouped_x remains False pass # Find max label length in x axis labels if grouped_x: tuples = list(zip(*source.data[x_col])) # convert to tuple of lists max_length = len(max(tuples[-1], key=len)) # look at inner-most level else: max_length = len(max(list(source.data[x_col]), key=len, default="")) # Set up the figure plot = figure( plot_width=800, plot_height=500, tools=["pan", "reset", "zoom_in", "zoom_out", "save", "help"], title=title, x_range=FactorRange( *source.data[x_col]) if grouped_x else source.data[x_col], sizing_mode=sizing_mode, ) # Add stacked area chart to plot area_renderers = plot.vbar_stack( stackers=stacked_cols, x=x_col, source=source, color=colors, width=0.8, alpha=0.7 # transparency ) # Add Legend legend_items = [(y, [area_renderers[i]]) for i, y in enumerate(stacked_cols) if np.mean(source.data[y]) > 0] legend = Legend(items=legend_items) plot.add_layout(legend, 'right') plot.legend.title = category_label plot.legend[0].items.reverse() # Reverse legend to match stacked order plot.legend.click_policy = 'hide' # Add interactivity to the legend show_hide_legend(plot=plot) # Hide legend on double click # Format Axes (labels, number formatting, range, etc.) x_label = x_col if x_label is None else x_label plot.xaxis.axis_label = x_label if max_length > 10: # Print innermost labels at angle if label is long plot.xaxis.major_label_orientation = 1 plot.xgrid.grid_line_color = None if y_label is not None: plot.yaxis.axis_label = y_label plot.yaxis.formatter = NumeralTickFormatter(format="0,0") plot.y_range.end = ylimit # will be ignored if ylimit is None # Add HoverTools for stacked bars/areas for r in area_renderers: category = r.name tooltips = [("%s" % x_label, "@{%s}" % x_col), ("%s" % category_label, category)] if y_label is None: pass elif "$" in y_label or "USD" in y_label: tooltips.append(("%s" % y_label, "@%s{$0,0}" % category)) else: tooltips.append(("%s" % y_label, "@%s{0,0}" % category)) hover = HoverTool(tooltips=tooltips, renderers=[r], toggleable=False) plot.add_tools(hover) return plot
def generate_correlation_graph(correlation_matrix_csv_path, path_to_save, title='Correlation Matrix',plot_height=1000, plot_width=1600): ## PREPARING CORRELATION MATRIX df = pd.read_csv(correlation_matrix_csv_path) df = df.set_index('Unnamed: 0').rename_axis('parameters', axis=1) df.index.name = 'level_0' ## AXIS LABELS FOR PLOT common_axes_val = list(df.index) df = pd.DataFrame(df.stack(), columns=['correlation']).reset_index() source = ColumnDataSource(df) ## FINDING LOWEST AND HIGHEST OF CORRELATION VALUES low_df_corr_min = df.correlation.min() high_df_corr_min = df.correlation.max() no_of_colors = len(df.correlation.unique()) ### PLOT PARTICULARS ## CHOOSING DEFAULT COLORS mapper = LinearColorMapper(palette=get_reversed_list(cividis(no_of_colors)), low=low_df_corr_min, high=high_df_corr_min) ## SETTING UP THE PLOT p = figure(title=title,x_range=common_axes_val, y_range=list((common_axes_val)),x_axis_location="below", plot_width=plot_width, plot_height=plot_height,tools=BOKEH_TOOLS, toolbar_location='above',tooltips=[('Parameters', '@level_0 - @parameters'), ('Correlation', '@correlation')]) p.toolbar.autohide = True ## SETTING UP PLOT PROPERTIES 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 = "12pt" p.xaxis.major_label_orientation = pi/2 ## SETTING UP HEATMAP RECTANGLES cir = p.rect(x="level_0", y="parameters", width=1, height=1,source=source,fill_color={'field': 'correlation', 'transform': mapper},line_color=None) ## SETTING UP COLOR BAR color_bar = ColorBar(color_mapper=mapper, major_label_text_font_size="5pt",ticker=BasicTicker(desired_num_ticks=10),formatter=PrintfTickFormatter(format="%.1f"),label_standoff=6, border_line_color=None, location=(0, 0)) p.add_layout(color_bar, 'right') ## AVAILABLE COLOR SCHEMES COLOR_SCHEME = { 'Cividis':get_reversed_list(cividis(no_of_colors)), 'Gray':get_reversed_list(gray(no_of_colors)), 'Inferno':get_reversed_list(inferno(no_of_colors)), 'Magma':get_reversed_list(magma(no_of_colors)), 'Viridis':get_reversed_list(viridis(no_of_colors)), 'Turbo':get_reversed_list(turbo(no_of_colors)), } ## JS CALLBACK callback = CustomJS(args=dict(col_sch=COLOR_SCHEME,low=low_df_corr_min,high=high_df_corr_min,cir=cir,color_bar=color_bar), code=""" // JavaScript code goes here var chosen_color = cb_obj.value; var color_mapper = new Bokeh.LinearColorMapper({palette:col_sch[chosen_color], low:low, high:high}); cir.glyph.fill_color = {field: 'correlation', transform: color_mapper}; color_bar.color_mapper.low = low; color_bar.color_mapper.high = high; color_bar.color_mapper.palette = col_sch[chosen_color]; """) ## SELECT OPTION FOR INTERACTIVITY GIVEN TO USER select = Select(title='Color Palette',value='cividis', options=list(COLOR_SCHEME.keys()), width=200, height=50) ## CALL BACK TO BE TRIGGERED WHENEVER USER SELECTS A COLOR PALETTE select.js_on_change('value', callback) ## GENERATION FINAL PLOT BY BINDING PLOT AND SELECT OPTION final_plot = layout([[select],[p]]) curdoc().add_root(final_plot) output_file(path_to_save) save(final_plot) carry_bokeh_correction(path_to_save)
active_mass = None else: st.write( "Calculating specific capacity using {} g active material".format( active_mass)) #cycnums = np.arange(cyc_range[0], cyc_range[1]) #st.write("Cycle numbers", cycnums) cmap = st.sidebar.selectbox("Color pallette", ('Default', 'viridis', 'cividis')) if cmap == 'Default': avail_colors = plt.rcParams['axes.prop_cycle'].by_key()['color'] colors = avail_colors * int(num_cycs / len(avail_colors) + 1) elif cmap == 'viridis': colors = bp.viridis(num_cycs) elif cmap == 'cividis': colors = bp.cividis(num_cycs) #else: # colors = plt.get_cmap(cmap)(np.linspace(0,1,num_cycs)) smooth = st.sidebar.slider("dQ/dV moving average window width", 0, 10) if smooth == 0: smooth = None # with plt.style.context('grapher'): # fig, ax = plt.subplots(figsize=(5,4)) #curdoc().theme = 'dark_minimal' # not working p = figure(plot_width=800, plot_height=400) if plot_opts == 'V-Q': caps, volts = voltage_curves(cycnums, active_mass=active_mass)
df1_cut_yr = df1_cut_year.groupby(['year', 'certif_EN'])['flat_AR'].mean() df1_cut_yr.drop(index=['nan', 'DUVL', 'pending', 'DGAC'], level=1, inplace=True) df1_cut_yr_df = df1_cut_yr.unstack() df1_minus = df1_cut_yr_manu_mean_df - df1_cut_yr_df df1_minus_avg = df1_minus.groupby('manufacturer').mean() df1_minus_avg_T = df1_minus_avg.transpose() df1_minus_avg_T.dropna(axis='columns', inplace=True) numlines_nb = len(df1_minus_avg_T.columns) mypalette = cividis(numlines_nb) p = figure(plot_width=plot_width_val, plot_height=plot_height_val, x_range=list(df1_minus_avg_T.index), tools=TOOLS) p.title.text = 'Means value of Aspect ration ( year ) , colored by manufactuer' for name, color in zip(df1_minus_avg_T.columns, mypalette): df = pd.DataFrame(df1_minus_avg_T[name]) p.line(df.index, df[name], line_width=2, color=color, alpha=0.8, legend=name)
import math from bokeh.io import output_file, show from bokeh.models import ColumnDataSource, FactorRange, LegendItem, Legend from bokeh.palettes import Spectral6, magma, cividis from bokeh.plotting import figure from bokeh.transform import factor_cmap from bokeh.transform import dodge import pandas as pd import numpy as np data = pd.read_csv("factbook.csv") length = len(data.index) p = figure(plot_width=800, plot_height=400) colors = cividis(25) data = data.rename(columns=lambda x: x.strip()) GDPperCapita = data['GDP per capita'].str.replace("$", "").str.replace( ",", "").str.strip().astype(float) Life = data['Life expectancy at birth'] population = data['Population'].str.replace(",", "").astype(int) birthrate = data['Birth rate'] population = population / 10000000 #print(birthrate) colorlist = [colors[int(i / 2)] for i in birthrate] print(colorlist) xdata = GDPperCapita.values ydata = Life.values #print(xdata) p.circle(xdata, ydata, size=population, color=colorlist) show(p)
def map_create(prot_pop_geo, prot_pop): geosource = GeoJSONDataSource( geojson=simplejson.dumps(simplejson.loads(prot_pop_geo.to_json()))) # ----- Raw count map ----- # initialize figure palette = cividis(256) hover_a = HoverTool(tooltips=[('Country', '@name'), ('Protests', '@protest')]) color_mapper_a = LogColorMapper(palette=palette, low=1, high=prot_pop['protest'].max() + 5, nan_color='#ffffff') color_bar_a = ColorBar( color_mapper=color_mapper_a, ticker=LogTicker(), orientation='horizontal', border_line_color=None, location=(0, 0), label_standoff=8, major_label_text_font='Raleway', major_label_text_font_size='12pt', title='Number of registered Fridays for Future protests', title_text_font_style='normal', title_text_font='Raleway', title_text_font_size='15pt') p_a = figure( title= 'The Fridays for Future movement grew to global scale within only 10 months', tools=[hover_a, ZoomInTool(), ZoomOutTool(), ResetTool(), PanTool()], plot_height=600, plot_width=980) # add patch renderer p_a.patches('xs', 'ys', source=geosource, fill_color={ 'field': 'protest', 'transform': color_mapper_a }, line_color='black', line_width=0.25, fill_alpha=1) # prettify p_a.xgrid.grid_line_color = None p_a.ygrid.grid_line_color = None p_a.outline_line_color = None p_a.axis.visible = False p_a.add_layout(color_bar_a, 'below') p_a.toolbar.logo = None p_a.title.text_font = "Raleway" p_a.title.text_font_style = "normal" p_a.title.align = 'center' p_a.title.text_color = 'black' p_a.title.text_font_size = '20pt' tab_a = Panel(child=p_a, title="Absolute number") # ----- Population adjusted count map ----- # initialize figure hover_b = HoverTool(tooltips=[( 'Country', '@name'), ('Protests per 10 million', '@prot_ptm'), ('Protests', '@protest'), ('Population', '@population')]) color_mapper_b = LogColorMapper( palette=palette, low=prot_pop['prot_ptm'].min(), high=float(prot_pop.loc[prot_pop['code'] == 'GRL']['prot_ptm']) + 10, nan_color='#ffffff') color_bar_b = ColorBar( color_mapper=color_mapper_b, ticker=LogTicker(), orientation='horizontal', border_line_color=None, location=(0, 0), label_standoff=8, major_label_text_font='Raleway', major_label_text_font_size='12pt', title='Number of registered Fridays for Future protests per 10 million', title_text_font_style='normal', title_text_font='Raleway', title_text_font_size='15pt') p_b = figure( title= 'The Fridays for Future movement grew to global scale within only 10 months', tools=[hover_b, ZoomInTool(), ZoomOutTool(), ResetTool(), PanTool()], plot_height=600, plot_width=980) # add patch renderer p_b.patches('xs', 'ys', source=geosource, fill_color={ 'field': 'prot_ptm', 'transform': color_mapper_b }, line_color='black', line_width=0.25, fill_alpha=1) # prettify p_b.xgrid.grid_line_color = None p_b.ygrid.grid_line_color = None p_b.outline_line_color = None p_b.axis.visible = False p_b.add_layout(color_bar_b, 'below') p_b.toolbar.logo = None p_b.title.text_font = "Raleway" p_b.title.text_font_style = "normal" p_b.title.align = 'center' p_b.title.text_color = 'black' p_b.title.text_font_size = '20pt' tab_b = Panel(child=p_b, title="Population normalized") tabs = Tabs(tabs=[tab_a, tab_b]) return tabs
def one_experiment(csv): # Get csv df = pd.read_csv(csv) # Get Data # Coordenates x = df['coordX'].unique() y = df['coordY'].unique() # csvList csvList = list(df['csvName'].unique()) # Get csv Count sumtime = df.loc[df['Time'] == 1] countList = list(sumtime['sumTime']) # Get time Count time = df.loc[df['id'] == 1] # Get cvs time values in x Time timeList = [] for i in range(len(time)): res = df.loc[df['Time'] == i + 1] lList = list(res['sumTime']) timeList.append(lList) # Scatter Graphic: # Colors N = len(sumtime['sumTime']) _x = random(size=N) * 100 color_count = np.asarray(countList) colors = [ "#%02x%02x%02x" % (int(r), int(g), 150) for r, g in zip(50 + 2 * _x, (30 + 2 * (color_count / 100))) ] # Data Source source = ColumnDataSource( dict(x=x, y=y, csv=csvList, count=countList, colors=colors)) # Hover hover = HoverTool(tooltips=[('csv', '@csv'), ('Total', '@count')]) scatter = figure(tools=[hover]) scatter.scatter(x='x', y='y', size=16, source=source, fill_color='colors', fill_alpha=0.7) # Bar Graph # Get Column List col_list = list(df) col_list.remove('id') col_list.remove('coordY') col_list.remove('coordX') col_list.remove('csvName') col_list.remove('Time') col_list.remove('sumTime') col_list.remove('Unnamed: 0') # Get df for first csv bar_initial = df.loc[df['id'] == 0] bar_initial = bar_initial[col_list] gene_list = [] # Get list of gene values count_csv = len(df['id'].unique()) for i in range(count_csv): # Get gene values for a single csv bar_initial = df.loc[df['id'] == i] bar_initial = bar_initial[col_list] aux_gene = [] for rows in bar_initial.itertuples(index=False): x = list(rows) aux_gene.append(x) gene_list.append(aux_gene) heatmap_data = np.array(gene_list).sum(axis=0).T heatmap_genes = ('WW', 'WH', 'WE', 'WR', 'WB', 'HH', 'HE', 'HR', 'HB', 'EE', 'ER', 'EB', 'RR', 'RB', 'BB') heatmap_df = pd.DataFrame(data=heatmap_data, index=heatmap_genes) heatmap_df.columns.name = 'time' heatmap_df.index.name = 'gene' heatmap_data = pd.DataFrame(heatmap_df.stack(), columns=['value']).reset_index() heatmap_source = ColumnDataSource(heatmap_data) # Heatmap hm_max = heatmap_data.value.max() # Using 0 as low prevents it from using LogTicker mapper = LogColorMapper(palette=Viridis256, low=1, high=hm_max) heatmap = figure(title="Heatmap", x_range=list(heatmap_df.columns.astype('str')), y_range=heatmap_genes, tools="save,pan,box_zoom,reset,wheel_zoom") heatmap.rect(x="time", y="gene", width=1, height=1, source=heatmap_source, line_color=None, fill_color=transform('value', mapper), dilate=True) color_bar = ColorBar(color_mapper=mapper, location=(0, 0), ticker=LogTicker(), formatter=PrintfTickFormatter(format="%d"), orientation='horizontal') heatmap.add_layout(color_bar, 'below') csv_selected = gene_list[0] # Get data for graph counts = csv_selected[0] # Get colors bar_colors = cividis(len(col_list)) bar_source = ColumnDataSource(data=dict(col_list=col_list, counts=counts, bar_colors=bar_colors, gene_list=gene_list)) status = ColumnDataSource(data={'time': [1], 'csv': [0]}) bar = mg_bar(col_list, bar_source) # Slider slider = mg_slider(source, timeList, time, bar_source, status) # Select select = mg_select(csvList, bar_source, status) return scatter, bar, heatmap, slider, select
temp = [str(m) for m in MPGs] print(temp) x = [ (ori,mpg) for mpg in temp for ori in origins] dictionary = {} dictionary['Origin'] = origins for i, t in enumerate(temp): dictionary[t] = matrix[i,:].tolist() print(dictionary) source = ColumnDataSource(data=dictionary) p = figure(x_range=FactorRange(*x), y_range=(0,50),plot_height=500, title="MPG", toolbar_location=None, tools="") colors = cividis(20) #r = p.vbar(x='x', top='counts', width=0.9, source=source, line_color="white",fill_color=factor_cmap('x', palette=magma(20), factors=temp, start=1, end=20)) for i in range(len(temp)): p.vbar(x=dodge('Origin', -0.6*10+0.6*i, range=p.x_range), top=temp[i], width=0.4, source=source, color=colors[i], legend_label=temp[i]) p.xgrid.grid_line_color = None p.y_range.start = 0 p.y_range.end = 45 p.xaxis.major_label_text_alpha = 0 p.xaxis.major_label_orientation = 1
def create_plot(df, title, tech_colors={}): """ :param df: :param title: string, plot title :param tech_colors: optional dict that maps technologies to colors. Technologies without a specified color will use a default palette :return: """ # TODO: handle empty dataframe (will give bokeh warning) technologies = df["technology"].unique() # Create a map between factor (technology) and color. techs_wo_colors = [t for t in technologies if t not in tech_colors.keys()] default_cmap = dict(zip(techs_wo_colors, cividis(len(techs_wo_colors)))) colormap = {} for t in technologies: if t in tech_colors: colormap[t] = tech_colors[t] else: colormap[t] = default_cmap[t] # Set up the figure plot = figure( plot_width=800, plot_height=500, tools=["pan", "reset", "zoom_in", "zoom_out", "save", "help"], title=title, # sizing_mode="scale_both" ) # Add scattered cap factors to plot. Do this one tech at a time so as to # allow interactivity such as hiding cap factors by tech by clicking # on legend renderers = [] for tech in technologies: sub_df = df[df["technology"] == tech] source = ColumnDataSource(data=sub_df) r = plot.circle( x="period", y="cap_factor", # legend=tech, source=source, line_color=colormap[tech], fill_color=colormap[tech], size=12, alpha=0.4) renderers.append(r) # Keep track of legend items legend_items = [(y, [renderers[i]]) for i, y in enumerate(technologies)] # Add Legend legend = Legend(items=legend_items) plot.add_layout(legend, "right") plot.legend.click_policy = "hide" # Add interactivity to the legend plot.legend.title = "Technology" # # Note: Doesn't rescale the graph down, simply hides the area # # Note2: There's currently no way to auto-size legend based on graph size(?) # # except for maybe changing font size automatically? show_hide_legend(plot=plot) # Hide legend on double click # Format Axes (labels, number formatting, range, etc.) plot.xaxis.axis_label = "Period" plot.yaxis.axis_label = "Capacity Factor (%)" plot.xaxis[0].ticker = df["period"].unique() plot.yaxis.formatter = NumeralTickFormatter(format="0%") for r in renderers: hover = HoverTool(tooltips=[("Project", "@project"), ("Technology", "@technology"), ("Period", "@period"), ("Capacity Factor", "@cap_factor{0%}")], renderers=[r], toggleable=False) plot.add_tools(hover) # Alternative, more succinct approach that uses factor_cmap and plots all # circles at once (but less legend interactivity and customizable) # # colors = factor_cmap( # field_name='technology', # palette=cividis, # factors=df["technology"].unique() # ) # # r = plot.circle( # x="period", # y="cap_factor", # legend="technology", # source=source, # line_color=colors, # fill_color=colors, # size=12, # alpha=0.4 # ) # # Add HoverTools # hover = HoverTool( # tooltips=[ # ("Project", "@project"), # ("Technology", "@technology"), # ("Period", "@period"), # ("Capacity Factor", "@cap_factor{0%}") # ], # renderers=[r], # toggleable=False) # plot.add_tools(hover) return plot
import math from bokeh.io import output_file, show from bokeh.models import ColumnDataSource, FactorRange, LegendItem, Legend from bokeh.palettes import Spectral6, magma, cividis from bokeh.plotting import figure from bokeh.transform import factor_cmap from bokeh.transform import dodge import pandas as pd import numpy as np data = pd.read_csv("old_cars.csv") years = [*range(70, 83)] p = figure(plot_width=400, plot_height=400) colors = cividis(13) for i, y in enumerate(years): subset = data.loc[data['Model'] == y] xdata = subset['Horsepower'].values ydata = subset['MPG'].values p.circle(xdata, ydata, color=colors[i], legend_label='19' + str(y)) show(p)