def plot_ballot_heatmap(self, width=800): title = 'Ballot Data' ballots = self.data candidates = self.candidate_names cnum = len(candidates) voters = np.arange(len(ballots)) height = len(voters) * 5 + 200 width = len(candidates) * 20 + 150 vmg, cmg = np.meshgrid(voters, candidates, indexing='ij') vr = vmg.ravel() cr = cmg.ravel() br = ballots.ravel() if self.election.ballot_type == voting.ID_RANK: br[br == 0] = cnum mapper = LinearColorMapper(palette=inferno(10), low=cnum, high=1) else: mapper = LinearColorMapper(palette=inferno(10), low=br.min(), high=br.max()) col_data = dict(voter=vr, candidate=cr, ballot=br, y=vr + 0.5) col_data = ColumnDataSource(data=col_data) plot = figure(y_range=[0, len(ballots)], x_range=candidates, plot_height=height, plot_width=width, title=title, tools='save', tooltips=[ ('candidate', '@candidate'), ('voter', '@voter'), ('value', '@ballot'), ]) plot.rect( source=col_data, y='y', x='candidate', width=1, height=1, fill_color={ 'field': 'ballot', 'transform': mapper }, # line_color="#111111", ) plot.xaxis.axis_label_text_font_size = '12pt' plot.xaxis.major_label_text_font_size = '12pt' plot.xaxis.major_label_orientation = np.pi / 2 plot.xaxis.axis_label = 'Candidate' plot.yaxis.axis_label_text_font_size = '12pt' plot.yaxis.major_label_text_font_size = '12pt' plot.yaxis.axis_label = 'Voter' html = file_html(plot, CDN, title=title) return html
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 showViz(data, zip_code): """ data=pandas data frame for resturant zip_code=zip code where resturants are located """ data = data.fillna('') column_name='perZip'+'.'+str(zip_code) #print(column_name) cuisine=data['cuisine'].tolist() counts=list(data[column_name].values) # Needed for using bokeh source = ColumnDataSource(data=dict(cuisine=cuisine, count=counts)) p = figure(x_range=cuisine,plot_height=1000, plot_width=1000,toolbar_location=None, title="Resturant counts by cusine") renderers = p.vbar(x='cuisine', top='count', width=0.9, source=source, line_color='white', fill_color=factor_cmap('cuisine', palette=inferno(len(cuisine)), factors=cuisine)) p.xgrid.grid_line_color = None # p.xaxis.major_label_orientation = math.pi/4 p.xaxis.major_label_text_font_size = "8pt" p.xaxis.major_label_orientation = "vertical" # Implement interactivity my_hover = HoverTool() my_hover.tooltips = [('Cuisine','@cuisine'),('Number of Locations','@count')] p.add_tools(my_hover) return p
def test(): ticks = [0., 1., 3, 5, 10, 30, 100] #we have 7 ticks we need 8 colors from bokeh.palettes import inferno colors = inferno(8) scale = CustomColors(ticks, colors, side='left') inputs = (-2, 0., 0.5, 20, 90, 100, 101) expected = (0, 0, 1, 5, 6, 6, 7) for i, e in zip(inputs, expected): result = scale.color(i) expected = colors[e] print(f"{i} -> {result} == {expected} - {result == expected}") scale = CustomColors(ticks, colors, side='right') inputs = (-2, 0., 0.5, 20, 90, 100, 101) expected = (0, 1, 1, 5, 6, 7, 7) for i, e in zip(inputs, expected): result = scale.color(i) expected = colors[e] print(f"{i} -> {result} == {expected} - {result == expected}") advanced_ticks = [(0., 'left'), 0.5, (1., 'right')] colors = ['green', 'yellow', 'orange', 'red'] scale = CustomColors(advanced_ticks, colors, side='right') inputs = (-2, 0., 0.4, 0.5, 0.7, 1., 1.1) expected = (0, 0, 1, 2, 2, 3, 3) for i, e in zip(inputs, expected): result = scale.color(i) expected = colors[e] print(f"{i} -> {result} == {expected} - {result == expected}")
def plot_cluster(traj_lst, cluster_lst): #num_clusters = 10 left = -140 right = -80 bottom = 25 top = 45 p = figure(title="Clustered Flight Trajectories Arriving To LAX", x_range=(left, right), y_range=(bottom, top)) color_mapper = linear_cmap('index', inferno(num_clusters), 0, num_clusters) for traj, cluster in zip(traj_lst, cluster_lst): # Gather list of latitude and longitude points for each flight separately lat = traj[:, 0] lon = traj[:, 1] index_list = np.ones(len(lat)) * cluster source = ColumnDataSource( data=dict(lat=lat, lon=lon, index=index_list)) p.circle(x="lon", y="lat", size=2, fill_alpha=0.1, source=source, color=color_mapper) p.xaxis.axis_label = 'Longitude' p.yaxis.axis_label = 'Latitude' return p
def test_cmap_generator_function(): assert pal.viridis(256) == pal.Viridis256 assert pal.magma(256) == pal.Magma256 assert pal.plasma(256) == pal.Plasma256 assert pal.inferno(256) == pal.Inferno256 assert pal.gray(256) == pal.Greys256 assert pal.grey(256) == pal.Greys256
def bokeh_variation_with_factor(chem, meta, factor_name, fig_dir=None): descr = (chem.groupby(meta[factor_name]).describe().rename(columns={ '25%': 'q1', '50%': 'q2', '75%': 'q3' })) if len(descr) > 11: colors = inferno(n=len(descr)) else: colors = Category10[len(descr)] cmap = {f: colors[i] for (i, f) in enumerate(descr.index)} plots = [] for col in chem.columns: data = descr[col].dropna().reset_index() if data['std'].max() == 0 or data[['q1', 'q2', 'q3']].max().max() == 0: continue if len(data) > 1: scatter_data = meta.assign(y=chem[col]).dropna() p = boxplot_single(data, cmap, factor_name, col, scatter_data=scatter_data) plots.append(p) grid = gridplot(plots, ncols=4) output_file(f"{fig_dir}/chem_with_{factor_name}.html") save(grid)
def test_cmap_generator_function(): assert pal.viridis(256) == pal.Viridis256 assert pal.magma(256) == pal.Magma256 assert pal.plasma(256) == pal.Plasma256 assert pal.inferno(256) == pal.Inferno256 assert pal.gray(256) == pal.Greys256 assert pal.grey(256) == pal.Greys256 assert pal.turbo(256) == pal.Turbo256 assert pal.diverging_palette(pal.Reds9, pal.Greys9, n=18, midpoint=0.5) == pal.Reds9 + pal.Greys9[::-1]
def map_plot(data, target_col, geodf): """ Function enables creation of inspectable maps depicting statistical values using bokeh plotting library and geopandas to supply vector maps of polish regions data - data to use as a base for the plot target_col - column in supplied data with value of depicted statistic geodf - geopandas dataframe with vectors palet - color palette fro bokeh to use - color(<size of palette>) """ # merging dataframes, creating source geodf = gpd.GeoDataFrame(geodf.merge(data, on="region")).to_json() source = GeoJSONDataSource(geojson=geodf) # defining figure p = figure(plot_height=500, plot_width=500, toolbar_location="below", sizing_mode="scale_both") # defining color palette palette = inferno(8) palette = palette[::-1] # defining color mapper to turn values into colors color_mapper = LinearColorMapper(palette=palette, low=0, high=max(data[target_col[0]]) * 1.1) # creating color bar to display below of the plot color_bar = ColorBar(color_mapper=color_mapper, label_standoff=8, border_line_color=None, location=(0, 0), orientation="vertical") # Add patch renderer to figure. plska = p.patches("xs", "ys", source=source, fill_color={ "field": target_col[0], "transform": color_mapper }, line_color="grey", line_width=0.25, fill_alpha=1) # Create hover tool p.xgrid.grid_line_color = None p.ygrid.grid_line_color = None p.axis.visible = False p.add_tools( HoverTool(renderers=[plska], tooltips=[('Województwo', '@region'), (target_col[1], f'@{target_col[0]}')])) # add color bar to the layot p.add_layout(color_bar, "right") return (p)
def biplot(score, coeff, expl_var, decomp='PCA', meta=None, fig_dir=None): coeff /= (np.abs(coeff).max() - np.abs(coeff).min()) * 1.5 score /= (score.max() - score.min()) coeff['norm'] = coeff.x**2 + coeff.y**2 coeff = coeff.sort_values(by='norm', ascending=False).iloc[:8] labels = meta.unique() if len(labels) < 11: colors = Category10[len(labels)] else: colors = inferno(len(labels)) color_map = {label: colors[i] for i, label in enumerate(labels)} score[meta.name] = meta.values score['color'] = [color_map[label] for label in meta.values] p = figure(plot_width=800, plot_height=800, x_range=(-1, 1), y_range=(-1, 1), title='Covariate contribution to the first 2 components') p.circle(x=score.columns[0], y=score.columns[1], color='color', alpha=0.25, size=5, legend_group=meta.name, source=score) for (x, y, _) in coeff.values: p.add_layout( Arrow(end=NormalHead(fill_color="orange", size=12, line_width=1), x_start=0, y_start=0, x_end=x, y_end=y)) labels = LabelSet(x='x', y='y', text='index', level='glyph', x_offset=5, y_offset=5, render_mode='canvas', source=ColumnDataSource(coeff.reset_index())) p.add_layout(labels) p.xaxis.axis_label = 'Component 1 ({:.1%})'.format(expl_var[0]) p.yaxis.axis_label = 'Component 2 ({:.1%})'.format(expl_var[1]) output_file(f"{fig_dir}/biplot_{decomp}.html") save(p)
def genColors(n, ptype='magma'): """ """ from bokeh.palettes import magma, inferno, plasma, viridis if ptype == 'magma': return magma(n) elif ptype == 'inferno': return inferno(n) elif ptype == 'plasma': return plasma(n) else: return viridis(n)
def datashade(self, column=None, reduction=None): """Return datashader shaded with reduction of given column value Arguments: - `column`: - `reduction`: """ import datashader as ds from bokeh import palettes from holoviews.operation.datashader import aggregate, shade, datashade, dynspread if reduction is None: reduction = ds.mean #print "datashade", column, reduction shade_opts = {"cmap": palettes.inferno(64)} if column is not None: d = self._points.dframe()[column] d_min, d_max = d.min(), d.max() #print "Value Range:", d_min, d_max #shade_opts["clims"] = (d_min, d_max) if d_min * d_max < 0.0: print("Diverging palette") d_extreme = max(-d_min, d_max) shade_opts = { "cmap": palettes.RdYlBu11, #"clims": (-d_extreme, d_extreme) } def _linear_norm_debug(v, masked): import pandas as pd #print args #print kwargs print(v) print(pd.DataFrame(v).describe()) min_v, max_v = v[~masked].min(), v[~masked].max() print(min_v, max_v) o = (v - min_v) / (max_v - min_v) print(o) return o #del (shade_opts["clims"]) plot = dynspread( datashade( self._points, aggregator=ds.count() if column is None else reduction(column), normalization="linear" if "clims" in shade_opts else "eq_hist", **shade_opts)) return plot
def make_coord_plots(): """ This makes all of the embedding coordinate scatter plots. """ # Read in the data. data = pd.read_csv( "https://raw.githubusercontent.com/ericmjl/flu-sequence-predictor/master/data/metadata_with_embeddings.csv", # noqa index_col=0, parse_dates=["Collection Date"], ) data["year"] = data["Collection Date"].apply(lambda x: x.year) data["Strain Name"] = data["Strain Name"].str.split("(").str[0] # Filter out just vaccine strains. with open("data/vaccine_strains.yaml", "r+") as f: vaccine_strains = yaml.load(f) vacc_data = data[data["Strain Name"].isin(vaccine_strains.values())] vacc_data.drop_duplicates(subset=["Strain Name"], inplace=True) vacc_data["years_deployed"] = 0 vaccine_strains_by_name = defaultdict(list) for year, strain in vaccine_strains.items(): vaccine_strains_by_name[strain].append(year) vacc_data["years_deployed"] = vacc_data["Strain Name"].apply( lambda x: vaccine_strains_by_name[x]) vacc_src = ColumnDataSource(vacc_data) # Resample data to quarterly data. data = data.set_index("Collection Date").resample("Q").mean() palette = inferno(len(data)) data["palette"] = palette src = ColumnDataSource(data) predcoords = load_prediction_coordinates() # Make the coordinate scatter plots p1 = make_coordinate_scatterplot([0, 1], src, predcoords, vacc_src) p2 = make_coordinate_scatterplot([1, 2], src, predcoords, vacc_src) p2.x_range = p1.y_range p3 = make_coordinate_scatterplot([0, 2], src, predcoords, vacc_src) p3.x_range = p1.x_range p3.y_range = p2.y_range # Create the plot layout - using rows. r1 = row([p1, p2, p3]) evo_script, evo_div = components(r1) return evo_script, evo_div
def get_threshold_summary_plot(ds): resultsdir = ds.config('DYESCORE_RESULTS_DIR') inpath = os.path.join(resultsdir, f'recall_summary_plot_data.csv') ds.file_in_validation(inpath) if ds.s3: with ds.s3.open(inpath, 'r') as f: results_df = pd_read_csv(f) else: results_df = pd_read_csv(inpath) recall_thresholds = sorted(results_df.recall_threshold.unique()) grouped_results_df = results_df.groupby('recall_threshold').agg( lambda x: list(x)) palette = inferno(len(recall_thresholds) + 1) # The yellow is often a little light source = ColumnDataSource(grouped_results_df) p = figure( title= f'Scripts captured by distance threshold for {len(recall_thresholds)} recall thresholds (colored)', width=800, toolbar_location=None, tools='', y_range=Range1d(results_df.n_over_threshold.min(), results_df.n_over_threshold.max()), ) p.xaxis.axis_label = 'distance threshold' p.yaxis.axis_label = 'minimum n_scripts' p.yaxis.formatter = NumeralTickFormatter(format="0a") p.extra_y_ranges = { 'percent': Range1d(results_df.percent.min(), results_df.percent.max()) } p.add_layout( LinearAxis(y_range_name='percent', axis_label='minimum n_scripts (percent of total)', formatter=NumeralTickFormatter(format='0%')), 'right') for i, recall_threshold in enumerate(recall_thresholds): view = CDSView(source=source, filters=[IndexFilter([i])]) opts = dict(source=source, view=view, legend=str(recall_threshold), color=palette[i], line_width=5, line_alpha=0.6) p.multi_line(xs='distance_threshold', ys='n_over_threshold', **opts) p.multi_line(xs='distance_threshold', ys='percent', y_range_name='percent', **opts) p.legend.click_policy = 'hide' return p
def plot_a_two_parameter_landscape(multi_landscape: multiparameter_landscape, index: int, TOOLTIPS=None): if (index > multi_landscape.maximum_landscape_depth) or (index < 0): raise TypeError('Index out of range') if TOOLTIPS is None: TOOLTIPS = [("x", "$x"), ("y", "$y"), ("value", "@image")] source = ColumnDataSource( data=dict(image=[ landscape_matrix_to_img(multi_landscape.landscape_matrix[ index, :, :]) ], x=[multi_landscape.bounds.lower_left[0]], y=[multi_landscape.bounds.lower_left[1]], dw=[ multi_landscape.bounds.upper_right[0] - multi_landscape.bounds.lower_left[0] ], dh=[ multi_landscape.bounds.upper_right[1] - multi_landscape.bounds.lower_left[1] ])) plot = figure(x_range=Range1d(multi_landscape.bounds.lower_left[0], multi_landscape.bounds.upper_right[0], bounds='auto'), y_range=Range1d(multi_landscape.bounds.lower_left[1], multi_landscape.bounds.upper_right[1], bounds='auto'), title="Multiparameter Landscape k=" + str(index + 1), sizing_mode='scale_both', match_aspect=True, toolbar_location=None) img = plot.image(source=source.data, image='image', x='x', y='y', dw='dw', dh='dh', palette=inferno(256)) img_hover = HoverTool(renderers=[img], tooltips=TOOLTIPS) plot.add_tools(img_hover) return plot
def build_visualization(symbol, fpath='Data/', outpath='Visualizations/', bad_vars=['Time', 'Volume']): symbol = symbol.lower() datasets = dict() datasets['Daily'] = build_dataset(fpath + f'{symbol}/{symbol}_daily/') datasets['Intraday'] = build_dataset(fpath + f'{symbol}/{symbol}_intraday/') datasets['Sentiment'] = build_dataset(fpath + f'{symbol}/{symbol}_sentiment/') figs = list() for dataset in datasets.keys(): s = figure(plot_width=1600, plot_height=500, x_axis_type="datetime") s.title.text = f'Data from {dataset}' columns = [ c for c in list(datasets[dataset].columns) if c not in bad_vars ] colors = inferno(len(columns)) for col, color in zip(columns, colors): line = s.line(x='Time', y=col, source=datasets[dataset], line_width=2, color=color, alpha=0.8, legend_label=col) s.add_tools( HoverTool(renderers=[line], tooltips=[('date', '@Time{%F}'), (f'{col}', f'@{col}'), ('Volume', f'@Volume')], formatters={ 'Time': 'datetime', f'{col}': f'numeral', 'Volume': f'numeral' }, mode='mouse')) s.legend.location = "top_left" s.legend.click_policy = "hide" figs.append(s) now = datetime.now() output_file(outpath + f'{symbol}_viz_{now.year}_{now.month}_{now.day}.html', title=f'Plots for {symbol}') c = column(*figs) show(c) save(c)
def feature_sentiment_distribution(word, frequent_feat): x = list(frequent_feat.columns[-10:]) y = list(frequent_feat[frequent_feat.Word == word][x].values[0]) title = "Score Distribution for " + word p = figure(x_range=x, plot_height=300, plot_width=400, title=title, toolbar_location=None, tools="") p = style(p) p.vbar(x=x, top=y, width=0.3, color=inferno(10)) return p
def update_menu(attr, old, new): new_n_walks = int(menu1.value) new_walk_length = int(menu2.value) global x_walks global colors x_walks = get_1d_walks(new_n_walks, new_walk_length) x_min = min([min(w) for w in x_walks]) x_max = max([max(w) for w in x_walks]) colors = [np.random.choice(inferno(100)) for w in x_walks] new_data = {'xs': x_walks, 'colors': colors} source.data = new_data plot.x_range.start = x_min plot.x_range.end = x_max slider.end = new_walk_length
def __init__(self, datafile=None, datadir=None): # self.colortable = np.asarray( # ['#8dd3c7', '#ffffb3', '#bebada', '#fb8072', # '#80b1d3', '#fdb462', '#b3de69', '#fccde5', # '#d9d9d9', '#bc80bd', '#ccebc5', '#ffed6f']) self.colortable = np.asarray(palettes.inferno(128)) self.imagecol = 'URL' self.layout = None self.excelfile = None self.doc = curdoc() self.sheetname = 'example data' self.color_bar = None self.fakedata() self.datadir = datadir self.page_setup() if datadir is not None: self.datadir = datadir
def make_plot(org_option): TopJobs_plot = TopJobs[TopJobs['Org_Group'] == org_option] Tot_Sal = sum(TopJobs_plot['TotalSals']) Tot_Bens = sum(TopJobs_plot['TotalBens']) Tot_Comp = sum(TopJobs_plot['TotalComp']) counts = [Tot_Sal, Tot_Bens, Tot_Comp] option_list = ['Total Salary', 'Total Benefits', 'Total Compensation'] p = figure(x_range=option_list, plot_height=500, plot_width=900, title=org_option) p.vbar(x=option_list, top=counts, width=0.9, color=inferno(3)) p.xaxis.major_label_orientation = 360 - 45 p.xgrid.grid_line_color = None p.y_range.start = 0 show(p)
def get_plot_data_source(data): xs = [[int(float(datapoint[0])) * 1000 for datapoint in points] for (object_id, points) in data] ys = [[ int(datapoint[1]) + random.randint(-100, 100) for datapoint in points ] for (object_id, points) in data] source = ColumnDataSource({ 'x': xs, 'y': ys, 'object_id': [object_id for (object_id, points) in data], 'address': [points[-1][2] for (object_id, points) in data], #TODO: cannot set different price for different data points, maybe a bug, maybe using it wrong; includind the entire price history for each point 'price': [ " -> ".join([ str(price) + "€" for price in [x[0] for x in groupby([point[1] for point in points])] ]) for (object_id, points) in data ], 'link': [points[-1][3] for (object_id, points) in data], 'pic': [points[-1][4] for (object_id, points) in data], 'desctext': [points[-1][5] for (object_id, points) in data], 'desc': [points[-1][6] for (object_id, points) in data], 'lastdl': [ datetime.datetime.fromtimestamp( int(float(points[-1][0])) - offset).strftime('%Y-%m-%d %H:%M:%S') for (object_id, points) in data ], 'color': [ inferno(min(256, len(data)))[index % min(256, len(data))] for index in range(len(data)) ], # thicker line if prices have changed 'thicc': [ 5 if len(set([point[1] for point in points])) != 1 else 1 for (object_id, points) in data ] }) return source
def createBar(): temp_dict = visual.read_csv('natural_disasters/CSV/disasters.csv') df = pd.DataFrame(temp_dict) df_sumed = df.groupby(['year']).sum() result = list(zip(df_sumed.index, df_sumed.occurrence)) years = list([str(val[0]) for val in result]) occurrences = list([val[1] for val in result]) source = ColumnDataSource( data=dict(years=years, occurrences=occurrences, color=inferno(97))) p = figure(x_range=years, y_range=(0, 40), plot_height=500, plot_width=1000, title="Natural Disasters", toolbar_location=None, tools="") p.vbar(x='years', top='occurrences', width=0.5, color='color', source=source) p.xaxis.major_label_orientation = math.pi / 2 label = LabelSet(x='years', y='occurrences', text='occurrences', level='glyph', text_font_size='0.8em', text_align='center', source=source, render_mode='canvas') p.xgrid.grid_line_color = None p.add_layout(label) script, div = components(p) return (script, div)
def plot_coords(df, filename): lat = 'Latitude|"Degrees"|-180.0|180.0|10' long = 'Longitude|"Degrees"|-180.0|180.0|10' speed = 'Speed|"mph"|0.0|150.0|10' df.loc[df[lat] == 0, lat] = np.nan df.loc[df[long] == 0, long] = np.nan df[lat] = -df[lat] df[long] = -df[long] coord_source = ColumnDataSource(df) coord_source.add(df['Interval|"ms"|0|0|1'], name='Time') coord = figure(sizing_mode='scale_both', width=700, height=600, title='GPS Data_{}'.format(filename)) df = df.dropna() mapper = linear_cmap( field_name='Speed|"mph"|0.0|150.0|10', palette=inferno(max(df[speed]) - min(df[speed])), # low_color='#ffffff', high_color='#ffffff', low=min(df[speed] + 13), high=max(df[speed]) - 27) coord.circle(x=long, y=lat, source=coord_source, size=3, color=mapper) # TODO figure out how to make the points be connected # coord.line(x=long, y=lat, source=coord_source, line_width=2, color='red') # Tools hover = HoverTool() hover.tooltips = [('Lat', '$x{0.000000}'), ('Long', '$y{0.000000}'), ('Time', '@Time')] hover.point_policy = 'follow_mouse' coord.add_tools(hover) return coord
def update_plot(highlight=False): global new_id, source, id_interests, topic_names, search_ids, custom_text #print(model_W[new_id]) sims = find_most_similar(model_W, [model_W[new_id]]) #print(sims) tops = assign_to_top_topic(model_W) col = inferno(no_topics) # random.shuffle(col) steps = 360. / no_topics x_ = [] y_ = [] t_ = [] o_ = [] topic_ = [] # print ("id_interests", id_interests, "Search_ids", search_ids) case = 0 if id_interests == [-1,-1]: case = 1
def chart(df): """ Builds a Bokeh plot for given stock betas :param df: Pandas DataFrame containing stock betas :return: Bokeh script, div components for rendering plot """ tooltips = [("Beta", "$y")] p = figure(width=800, height=400, x_axis_type='datetime', tools='wheel_zoom,pan,box_zoom,reset', tooltips=tooltips) col_len = len(df.columns) # Spectral palette is really nice, but only has 11 colors if col_len <= 11: palette = linear_palette(Spectral11, col_len) else: palette = inferno(col_len) for i in range(col_len): p.line(df.index, df[df.columns[i]], color=palette[i], line_width=2, legend=df.columns[i], alpha=0.8, muted_alpha=0.2, muted_color=palette[i]) p.legend.location = "top_left" p.legend.click_policy = "mute" return components(p)
def plot_region_GDP(data): nums = len(GDP_region_preprocess(data)) processed = GDP_region_preprocess(data) countries = [processed[i].columns[1] for i in range(nums)] date_column = processed[0]['year'] GDP = [processed[i][processed[i].columns[1]] for i in range(nums)] # initialize the figure numlines = len(GDP) mypalette = inferno(numlines) p = figure(plot_width=1200, plot_height=600, x_axis_type="datetime", title="GDP") p.grid.grid_line_alpha = 0.3 p.xaxis.axis_label = 'Date' p.yaxis.axis_label = 'Region GDP' for i in range(len(countries)): p.line(date_column, GDP[i], color=mypalette[i], legend=countries[i]) p.legend.location = "top_left" return p
y_label = 'Latitude (degrees north)' aod_tabs = [] for index, ds in enumerate(Datasets): # Pull the relevant data from the dictionary current_set = Datasets[names[index]] aod = current_set['AOD'][:][:] lat = current_set['Latitude'] time = current_set['Time'] name = current_set['name'] data = {'AOD': aod, 'Latitude': lat, 'Time': time} # Create the plot, with both lines and points n_divs = 10 color = palettes.inferno(n_divs) color = color[::-1] # reverse the color palette # Override the max value for files with outlier points # high = round(0.75 * np.max(aod), 1) # default colorbar high value if index < 3: # Set time range and colour bar more milennial data high = 0.5 if index == 0: # Link axes to the millennial plot so all zoom and pan together x_range = [-500, 2020] else: x_range = plots[0].x_range else: # Set time range and colour bar for historical data high = 0.2 if index == 3: # Link axes to the first historical plot x_range = [1850, 2020]
def plot_powspec_binned_bokeh(data: Dict, lmax: Dict, title_string: str, truthfile=None, truth_label: str = None) -> None: """Plotting Args: data (Dict): powerspectra with spectrum and frequency-combinations in the columns plotsubtitle (str, optional): Add characters to the title. Defaults to 'default'. plotfilename (str, optional): Add characters to the filename. Defaults to 'default' """ # koi = next(iter(data.keys())) # specs = list(data[koi].keys()) bins = np.logspace(np.log10(1), np.log10(lmax + 1), 250) bl = bins[:-1] br = bins[1:] from bokeh.plotting import figure as bfigure plt = bfigure(title=title_string, y_axis_type="log", x_axis_type="log", x_range=(10, 4000), y_range=(1e-3, 1e6), background_fill_color="#fafafa") # plt.xlabel("Multipole l") # plt.ylabel("Powerspectrum") idx = 0 idx_max = 8 from bokeh.palettes import inferno for freqc, val in data.items(): col = inferno(idx_max) freqs = freqc.split("-") if freqs[0] == freqs[1]: binmean, binerr, _ = _std_dev_binned(data[freqc]) binerr_low = np.array([ binmean[n] * 0.01 if binerr[n] > binmean[n] else binerr[n] for n in range(len(binerr)) ]) plt.line( 0.5 * bl + 0.5 * br, np.nan_to_num(binmean), legend_label=freqc + " Channel", # yerr=(binerr_low, binerr), # # 0.5 * bl + 0.5 * br, # # binmean, # # yerr=binerr, # label=freqc, # capsize=2, # elinewidth=1, # fmt='x', # # ls='-', # ms=4, # alpha=(2*idx_max-idx)/(2*idx_max) color=col[idx], line_width=3, muted_alpha=0.2) plt.multi_line( #[(bm, bm) for bm in np.nan_to_num(binmean)] [(bx, bx) for bx in 0.5 * bl + 0.5 * br], [(bm - br, bm + br) for bm, br in zip(np.nan_to_num(binmean), np.nan_to_num(binerr))], legend_label=freqc + " Channel", # yerr=(binerr_low, binerr), # # 0.5 * bl + 0.5 * br, # # binmean, # # yerr=binerr, # label=freqc, # capsize=2, # elinewidth=1, # fmt='x', # # ls='-', # ms=4, # alpha=(2*idx_max-idx)/(2*idx_max) line_color=col[idx], line_width=2, muted_alpha=0.2) idx += 1 plt.xaxis.axis_label = "Multipole l" plt.yaxis.axis_label = "Powerspectrum" plt.line(np.arange(0, 4000, 1), truthfile, color='red', line_width=4, legend_label="Best Planck EE", muted_alpha=0.2) # label = truth_label, # ls='-', marker='.', # ms=0, # lw=3) plt.legend.location = "top_left" plt.legend.click_policy = "mute" return plt
def make_plot(df, corp, color_palette): palette_list = [ palettes.viridis(100), palettes.inferno(100), palettes.magma(100), palettes.plasma(100), ] colors = list(reversed(palette_list[color_palette])) mapper = LinearColorMapper(palette=colors, low=0, high=100) TOOLS = "hover,save,pan,box_zoom,reset,wheel_zoom" TOOLTIPS = """ <div> <div> <span style="font-size: 20px; font-weight: bold;">score: @c%</span> </div> <div> <span style="font-size: 16px; font-style: italic;">@a & @b</span> </div> </div> """ # tooltips=[('score','@c%'), ('doc_1', '@a'), ('doc_2', '@b')]) hm = figure( x_range=corp, y_range=list(reversed(corp)), x_axis_location="above", plot_width=900, plot_height=900, tools=TOOLS, toolbar_location="below", tooltips=TOOLTIPS, ) hm.grid.grid_line_color = None hm.axis.axis_line_color = None hm.axis.major_tick_line_color = None hm.axis.major_label_text_font_size = "8pt" hm.axis.major_label_standoff = 0 hm.xaxis.major_label_orientation = pi / 3 hm.rect( x="a", y="b", source=df, width=1, height=1, line_color="#ffffff", fill_color={ "field": "c", "transform": mapper }, ) color_bar = ColorBar( color_mapper=mapper, formatter=PrintfTickFormatter(format="%d%%"), major_label_text_font_size="10pt", label_standoff=10, border_line_color=None, location=(0, 0), ) hm.add_layout(color_bar, "right") js_resources = INLINE.render_js() css_resources = INLINE.render_css() script, div = components(hm) return js_resources, css_resources, script, div
def compute_opacity(atmosphere, opacityclass, delta_eddington=True,test_mode=False,raman=0, plot_opacity=False, full_output=False): """ Returns total optical depth per slab layer including molecular opacity, continuum opacity. It should automatically select the molecules needed Parameters ---------- atmosphere : class ATMSETUP This inherets the class from atmsetup.py opacityclass : class opacity This inherets the class from optics.py. It is done this way so that the opacity db doesnt have to be reloaded in a retrieval delta_eddington : bool (Optional) Default=True, With Delta-eddington on, it incorporates the forward peak contribution by adjusting optical properties such that the fraction of scattered energy in the forward direction is removed from the scattering parameters raman : int (Optional) Default =0 which corresponds to oklopcic+2018 raman scattering cross sections. Other options include 1 for original pollack approximation for a 6000K blackbody. And 2 for nothing. test_mode : bool (Optional) Default = False to run as normal. This will overwrite the opacities and fix the delta tau at 0.5. full_output : bool (Optional) Default = False. If true, This will add taugas, taucld, tauray to the atmosphere class. This is done so that the users can debug, plot, etc. plot_opacity : bool (Optional) Default = False. If true, Will create a pop up plot of the weighted of each absorber at the middle layer. Returns ------- DTAU : ndarray This is a matrix with # layer by # wavelength. It is the opacity contained within a layer including the continuum, scattering, cloud (if specified), and molecular opacity **If requested, this is corrected for with Delta-Eddington.** TAU : ndarray This is a matrix with # level by # wavelength. It is the cumsum of opacity contained including the continuum, scattering, cloud (if specified), and molecular opacity **If requested, this is corrected for with Delta-Eddington.** WBAR : ndarray This is the single scattering albedo that includes rayleigh, raman and user input scattering sources. It has dimensions: # layer by # wavelength **If requested, this is corrected for with Delta-Eddington.** COSB : ndarray This is the asymettry factor which accounts for rayleigh and user specified values It has dimensions: # layer by # wavelength **If requested, this is corrected for with Delta-Eddington.** ftau_cld : ndarray This is the fraction of cloud opacity relative to the total TAUCLD/(TAUCLD + TAURAY) ftau_ray : ndarray This is the fraction of rayleigh opacity relative to the total TAURAY/(TAUCLD + TAURAY) GCOS2 : ndarray This is used for Cahoy+2010 methodology for accounting for rayleigh scattering. It replaces the use of the actual rayleigh phase function by just multiplying ftau_ray by 2 DTAU : ndarray This is a matrix with # layer by # wavelength. It is the opacity contained within a layer including the continuum, scattering, cloud (if specified), and molecular opacity **If requested, this is corrected for with Delta-Eddington.** TAU : ndarray This is a matrix with # level by # wavelength. It is the cumsum of opacity contained including the continuum, scattering, cloud (if specified), and molecular opacity **Original, never corrected for with Delta-Eddington.** WBAR : ndarray This is the single scattering albedo that includes rayleigh, raman and user input scattering sources. It has dimensions: # layer by # wavelength **Original, never corrected for with Delta-Eddington.** COSB : ndarray This is the asymettry factor which accounts for rayleigh and user specified values It has dimensions: # layer by # wavelength **Original, never corrected for with Delta-Eddington.** Notes ----- This was baselined against jupiter with the old fortran code. It matches 100% for all cases except for hotter cases where Na & K are present. This differences is not a product of the code but a product of the different opacities (1060 grid versus old 736 grid) Todo ----- Add a better approximation than delta-scale (e.g. M.Marley suggests a paper by Cuzzi that has a better methodology) """ atm = atmosphere tlevel = atm.level['temperature'] plevel = atm.level['pressure']/atm.c.pconv #think of a better solution for this later when mark responds tlayer = atm.layer['temperature'] player = atm.layer['pressure']/atm.c.pconv #think of a better solution for this later when mark responds gravity = atm.planet.gravity / 100.0 #this too... need to have consistent units. nlayer = atm.c.nlayer nwno = opacityclass.nwno if plot_opacity: plot_layer=int(nlayer/2)#np.size(tlayer)-1 opt_figure = figure(x_axis_label = 'Wavelength', y_axis_label='TAUGAS in optics.py', title = 'Opacity at T='+str(tlayer[plot_layer])+' Layer='+str(plot_layer) ,y_axis_type='log',height=600, width=800,x_range=[0.3,1]) #====================== INITIALIZE TAUGAS#====================== TAUGAS = 0 TAURAY = 0 c=1 #set color scheme.. adding 3 for raman, rayleigh, and total if plot_opacity: colors = inferno(3+len(atm.continuum_molecules) + len(atm.molecules)) #====================== ADD CONTIMUUM OPACITY====================== #Set up coefficients needed to convert amagat to a normal human unit #these COEF's are only used for the continuum opacity. ACOEF = (tlayer/(tlevel[:-1]*tlevel[1:]))*( tlevel[1:]*plevel[1:] - tlevel[:-1]*plevel[:-1])/(plevel[1:]-plevel[:-1]) #UNITLESS BCOEF = (tlayer/(tlevel[:-1]*tlevel[1:]))*( tlevel[:-1] - tlevel[1:])/(plevel[1:]-plevel[:-1]) #INVERSE PRESSURE COEF1 = atm.c.rgas*273.15**2*.5E5* ( ACOEF* (plevel[1:]**2 - plevel[:-1]**2) + BCOEF*( 2./3.)*(plevel[1:]**3 - plevel[:-1]**3) ) / ( 1.01325**2 *gravity*tlayer*atm.layer['mmw']) #go through every molecule in the continuum first for m in atm.continuum_molecules: #H- Bound-Free if (m[0] == "H-") and (m[1] == "bf"): ADDTAU = (opacityclass.continuum_opa['H-bf']*( #[(nwno x nlayer) *( atm.layer['mixingratios'][m[0]].values* #nlayer atm.layer['colden']/ #nlayer (atm.layer['mmw']*atm.c.amu)) ).T #nlayer)].T TAUGAS += ADDTAU if plot_opacity: opt_figure.line(1e4/opacityclass.wno, ADDTAU[plot_layer,:], alpha=0.7,legend_label=m[0]+m[1], line_width=3, color=colors[c], muted_color=colors[c], muted_alpha=0.2) #H- Free-Free elif (m[0] == "H-") and (m[1] == "ff"): ADDTAU = (opacityclass.continuum_opa['H-ff']*( #[(nwno x nlayer) *( atm.layer['pressure']* #nlayer atm.layer['mixingratios']['H'].values*atm.layer['electrons']*#nlayer atm.layer['colden']/ #nlayer (tlayer*atm.layer['mmw']*atm.c.amu*atm.c.k_b)) ).T #nlayer)].T #testing['H-ff'] = ADDTAU TAUGAS += ADDTAU if plot_opacity: opt_figure.line(1e4/opacityclass.wno, ADDTAU[plot_layer,:], alpha=0.7,legend_label=m[0]+m[1], line_width=3, color=colors[c], muted_color=colors[c], muted_alpha=0.2) #H2- elif (m[0] == "H2-") and (m[1] == ""): #calculate opacity #this is a hefty matrix multiplication to make sure that we are #multiplying each column of the opacities by the same 1D vector (as opposed to traditional #matrix multiplication). This is the reason for the transposes. ADDTAU = (opacityclass.continuum_opa['H2-']*( #[(nwno x nlayer) *( atm.layer['pressure']* #nlayer atm.layer['mixingratios']['H2'].values*atm.layer['electrons']* #nlayer atm.layer['colden']/ #nlayer (atm.layer['mmw']*atm.c.amu)) ).T #nlayer)].T TAUGAS += ADDTAU if plot_opacity: opt_figure.line(1e4/opacityclass.wno, ADDTAU[plot_layer,:], alpha=0.7,legend_label=m[0]+m[1], line_width=3, color=colors[c], muted_color=colors[c], muted_alpha=0.2) #everything else.. e.g. H2-H2, H2-CH4. Automatically determined by which molecules were requested else: #calculate opacity ADDTAU = (opacityclass.continuum_opa[m[0]+m[1]] * ( #[(nwno x nlayer) *( COEF1* #nlayer atm.layer['mixingratios'][m[0]].values * #nlayer atm.layer['mixingratios'][m[1]].values ) ).T #nlayer)].T TAUGAS += ADDTAU if plot_opacity: opt_figure.line(1e4/opacityclass.wno, ADDTAU[plot_layer,:], alpha=0.7,legend_label=m[0]+m[1], line_width=3, color=colors[c], muted_color=colors[c], muted_alpha=0.2) c+=1 #====================== ADD MOLECULAR OPACITY====================== for m in atm.molecules: #if m =='O3': # df = pd.read_csv('~/Desktop/LIFE/O3_visible.txt',delim_whitespace=True,names=['nm','cx']) # wno_old1 = (df['nm']*1e-3).values # wno1 = 1e4/opacityclass.wno[::-1] # o31 = np.zeros((len(wno1),1)) # o31[:,0] = np.interp(wno1, wno_old1 ,df['cx'], left=1e-33, right=1e-33)[::-1]*6.02214086e+23 # opacityclass.molecular_opa[m] += o31 ADDTAU = (opacityclass.molecular_opa[m] * ( #[(nwno x nlayer) *( atm.layer['colden']* atm.layer['mixingratios'][m].values/ #removing this bc of opa unit change *atm.weights[m].values[0]/ atm.layer['mmw']) ).T TAUGAS += ADDTAU #testing[m] = ADDTAU if plot_opacity: opt_figure.line(1e4/opacityclass.wno, ADDTAU[plot_layer,:], alpha=0.7,legend_label=m, line_width=3, color=colors[c], muted_color=colors[c], muted_alpha=0.2) c+=1 #====================== ADD RAYLEIGH OPACITY====================== #old 1 """ ray_mixingratios = np.zeros((nlayer,3))#hardwired because we only have h2,he and ch4 scattering for i,j in zip(['H2','He','CH4'],range(3)): if i in atm.rayleigh_molecules: print(i) ray_mixingratios[:,j] = atm.layer['mixingratios'][i].values TAURAY = rayleigh_old(atm.layer['colden'],ray_mixingratios, opacityclass.wave, atm.layer['mmw'],atm.c.amu ) """ for m in atm.rayleigh_molecules: ray_matrix = np.array([opacityclass.rayleigh_opa[m]]*nlayer).T ADDTAU = (ray_matrix * ( #[(nwno x nlayer) *( atm.layer['colden']* atm.layer['mixingratios'][m].values/ #removing this bc of opa unit change *atm.weights[m].values[0]/ atm.layer['mmw'])).T TAURAY += ADDTAU """ #old 3 lam = np.zeros((len(opacityclass.wno),1)) lam[:,0] = 1e4/opacityclass.wno nam1=1E-8*(8342.13+2406030./(130.-lam**(-2))+15997./(38.9-lam**(-2))) ff=1.05 Na0=2.55E19*100.**3 ray_matrix=np.zeros((len(opacityclass.wno), nlayer))+32.*np.pi**3./(3.*Na0**2)*(nam1**2)*ff/((lam*1E-6)**4.) TAURAY = (6.02214086e+23 * 1e4 * ray_matrix * ( #[(nwno x nlayer) *( atm.layer['colden']* atm.layer['mixingratios']['N2'].values/ #removing this bc of opa unit change *atm.weights[m].values[0]/ atm.layer['mmw'])).T """ if plot_opacity: opt_figure.line(1e4/opacityclass.wno, TAURAY[plot_layer,:], alpha=0.7,legend_label='Rayleigh', line_width=3, color=colors[c], muted_color=colors[c], muted_alpha=0.2) #====================== ADD RAMAN OPACITY====================== #OKLOPCIC OPACITY if raman == 0 : raman_db = opacityclass.raman_db raman_factor = compute_raman(nwno, nlayer,opacityclass.wno, opacityclass.raman_stellar_shifts, tlayer, raman_db['c'].values, raman_db['ji'].values, raman_db['deltanu'].values) if plot_opacity: opt_figure.line(1e4/opacityclass.wno, raman_factor[plot_layer,:]*TAURAY[plot_layer,:], alpha=0.7,legend_label='Shifted Raman', line_width=3, color=colors[c], muted_color=colors[c], muted_alpha=0.2) raman_factor = np.minimum(raman_factor, raman_factor*0+0.99999) #POLLACK OPACITY elif raman ==1: raman_factor = raman_pollack(nlayer) raman_factor = np.minimum(raman_factor, raman_factor*0+0.99999) if plot_opacity: opt_figure.line(1e4/opacityclass.wno, raman_factor[plot_layer,:]*TAURAY[plot_layer,:], alpha=0.7,legend_label='Shifted Raman', line_width=3, color=colors[c], muted_color=colors[c], muted_alpha=0.2) #NOTHING else: raman_factor = 0.99999 #====================== ADD CLOUD OPACITY====================== TAUCLD = atm.layer['cloud']['opd'] #TAUCLD is the total extinction from cloud = (abs + scattering) asym_factor_cld = atm.layer['cloud']['g0'] single_scattering_cld = atm.layer['cloud']['w0'] #====================== If user requests full output, add Tau's to atmosphere class===== if full_output: atmosphere.taugas = TAUGAS atmosphere.tauray = TAURAY atmosphere.taucld = TAUCLD #====================== ADD EVERYTHING TOGETHER PER LAYER====================== #formerly DTAU DTAU = TAUGAS + TAURAY + TAUCLD # This is the fractional of the total scattering that will be due to the cloud #VERY important note. You must weight the taucld by the single scattering #this is because we only care about the fractional opacity from the cloud that is #scattering. ftau_cld = (single_scattering_cld * TAUCLD)/(single_scattering_cld * TAUCLD + TAURAY) COSB = ftau_cld*asym_factor_cld #formerly GCOSB2 ftau_ray = TAURAY/(TAURAY + TAUCLD) GCOS2 = 0.5*ftau_ray #Hansen & Travis 1974 for Rayleigh scattering #Raman correction is usually used for reflected light calculations #although users to have option turn it off in reflected light as well W0 = (TAURAY*raman_factor + TAUCLD*single_scattering_cld) / (TAUGAS + TAURAY + TAUCLD) #TOTAL single scattering #if a user wants both reflected and thermal, this computes SSA without raman correction, but with #scattering from clouds still W0_no_raman = (TAURAY*0.99999 + TAUCLD*single_scattering_cld) / (TAUGAS + TAURAY + TAUCLD) #TOTAL single scattering #sum up taus starting at the top, going to depth shape = DTAU.shape TAU = np.zeros((shape[0]+1, shape[1])) TAU[1:,:]=numba_cumsum(DTAU) if plot_opacity: opt_figure.line(1e4/opacityclass.wno, DTAU[int(np.size(tlayer)/2),:], legend_label='TOTAL', line_width=4, color=colors[0], muted_color=colors[c], muted_alpha=0.2) opt_figure.legend.click_policy="mute" show(opt_figure) if test_mode != None: #this is to check against Dlugach & Yanovitskij #https://www.sciencedirect.com/science/article/pii/0019103574901675?via%3Dihub if test_mode=='rayleigh': DTAU = TAURAY GCOS2 = 0.5 ftau_ray = 1.0 ftau_cld = 1e-6 else: DTAU = TAURAY*0+0.5 GCOS2 = 0.0 ftau_ray = 1e-6 ftau_cld = 1 COSB = atm.layer['scattering']['g0'] W0 = atm.layer['scattering']['w0'] TAU = np.zeros((shape[0]+1, shape[1])) TAU[1:,:]=numba_cumsum(DTAU) #====================== D-Eddington Approximation====================== if delta_eddington: #First thing to do is to use the delta function to icorporate the forward #peak contribution of scattering by adjusting optical properties such that #the fraction of scattered energy in the forward direction is removed from #the scattering parameters #Joseph, J.H., W. J. Wiscombe, and J. A. Weinman, #The Delta-Eddington approximation for radiative flux transfer, J. Atmos. Sci. 33, 2452-2459, 1976. #also see these lecture notes are pretty good #http://irina.eas.gatech.edu/EAS8803_SPRING2012/Lec20.pdf w0_dedd=W0*(1.-COSB**2)/(1.0-W0*COSB**2) cosb_dedd=COSB/(1.+COSB) dtau_dedd=DTAU*(1.-W0*COSB**2) #sum up taus starting at the top, going to depth tau_dedd = np.zeros((shape[0]+1, shape[1])) tau_dedd[1:,:]=numba_cumsum(dtau_dedd) #returning the terms used in return (dtau_dedd, tau_dedd, w0_dedd, cosb_dedd ,ftau_cld, ftau_ray, GCOS2, DTAU, TAU, W0, COSB, #these are returned twice because we need the uncorrected W0_no_raman) #values for single scattering terms where we use the TTHG phase function # w0_no_raman is used in thermal calculations only else: return (DTAU, TAU, W0, COSB, ftau_cld, ftau_ray, GCOS2, DTAU, TAU, W0, COSB, #these are returned twice for consistency with the delta-eddington option W0_no_raman) #W0_no_raman is used for thermal calculations only