def _init_viz_schema(self): self.state_counties = { code: county for code, county in counties.items() if county["state"] == "wa" } self.county_xs = [county["lons"] for county in self.state_counties.values()] self.county_ys = [county["lats"] for county in self.state_counties.values()] self.county_names = [county['name'] for county in self.state_counties.values()] self.county_rates = [] self.rate_categories = [] for county_name in self.county_names: cfg = self.county_data.get(county_name, {'Confirmed_Cases': 0}) val = cfg['Confirmed_Cases'] self.county_rates.append(val) if val is 0: self.rate_categories.append('OK') elif 0 < val <= 10: self.rate_categories.append('Arriving') elif 10 < val <= 50: self.rate_categories.append('Warn') else: self.rate_categories.append('Severe')
def plot(location, predictions,state_name): from bokeh.sampledata.us_counties import data as counties palette.reverse() counties = {code: county for code, county in counties.items() if county["state"] in [state_name]} 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()]#Make sure names match with data county_rates = [unemployment[county_id] for county_id in counties]#These would be the predictions #color_mapper = LogColorMapper(palette=palette) color_mapper = LinearColorMapper(palette=palette,low=25, high=75) data=dict(x=county_xs,y=county_ys,name=county_names,rate=predictions,) TOOLS = "pan,wheel_zoom,reset,hover,save" p = figure(title="Vote Preference Predictions", tools=TOOLS, plot_width=900, #x_axis_location=None, y_axis_location=None,tooltips=[("Name", "@name"), ("Unemployment rate)", "@rate%"), ("(Long, Lat)", "($x, $y)")]) x_axis_location=None, y_axis_location=None,tooltips=[("Name", "@name"), ("Yes vote (percent)", "@rate%")]) 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=1.0, line_color="white", line_width=0.5) script, div = components(p) return script, div
def bokeh_chloropleth(state, values): """Creates a Bokeh chloropleth for a given state's counties given values. Args: state (str): The state to analyze. values (list): A list of county values. """ # Define county shapes. counties = { code: county for code, county in counties.items() if county["state"] == state } 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()] # Define county values # county_rates = [unemployment[county_id] for county_id in counties] color_mapper = LogColorMapper(palette=palette) data = dict( x=county_xs, y=county_ys, name=county_names, value=values, ) TOOLS = "pan,wheel_zoom,reset,hover,save" p = figure(title="Washington State Cannabis Cultivators", tools=TOOLS, x_axis_location=None, y_axis_location=None, tooltips=[("Name", "@name"), ("Number", "@value%"), ("(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': 'value', 'transform': color_mapper }, fill_alpha=0.7, line_color="white", line_width=0.5, plot_width=800, plot_height=500) show(p) return p
def plot(location, predictions, state_name): from bokeh.sampledata.us_counties import data as counties #print(counties.keys()) counties = { code: county for code, county in counties.items() if county["state"] in [state_name] } print(len(counties)) #print(type(counties)) 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() ] #Make sure names match with data print(county_names) color_mapper = LinearColorMapper(palette=palette, low=25, high=75) data = dict( x=county_xs, y=county_ys, name=county_names, rate=predictions, ) TOOLS = "pan,wheel_zoom,reset,hover,save" p = figure(title="Vote Preference Predictions", tools=TOOLS, plot_width=900, x_axis_location=None, y_axis_location=None, tooltips=[("Name", "@name"), ("Yes vote (percent)", "@rate%")]) color_bar = ColorBar(color_mapper=color_mapper, ticker=LogTicker(), label_standoff=12, border_line_color=None, location=(0, 0)) p.add_layout(color_bar, 'right') 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=1.0, line_color="white", line_width=0.5) script, div = components(p) return script, div
def create_plot(ridb): or_counties = { code: county for code, county in counties.items() if county["state"] == "or" } county_xs = [county["lons"] for county in or_counties.values()] county_ys = [county["lats"] for county in or_counties.values()] county_names = [county['name'] for county in or_counties.values()] lat = ridb['FacilityLatitude'] lon = ridb['FacilityLongitude'] source = ColumnDataSource( data=dict(x=county_xs, y=county_ys, name=ridb['FacilityName'])) TOOLS = "pan,wheel_zoom,box_zoom,reset,save" ridb = ridb.assign(Secret="TRILLIUM") df_source = ColumnDataSource( data=ridb[['FacilityName', 'FacilityPhone', 'Secret']]) p = figure(title="Oregon Counties", tools=TOOLS) p.circle(lon, lat, size=8, color='navy', alpha=1, source=df_source, name='geo_points') p.patches('x', 'y', source=source, fill_color='green', fill_alpha=0.7, line_color="black", line_width=0.5) hover = HoverTool(names=['geo_points']) hover.tooltips = [ (c, '@' + c) for c in ridb[['FacilityName', 'FacilityPhone', 'Secret']].columns ] p.add_tools(hover) return p
def bokehPlot(p): from bokeh.io import show from bokeh.models import LogColorMapper from bokeh.palettes import Viridis6 as palette try: from bokeh.sampledata.unemployment import data as unemployment from bokeh.sampledata.us_counties import data as counties except: import bokeh bokeh.sampledata.download() from bokeh.sampledata.unemployment import data as unemployment from bokeh.sampledata.us_counties import data as counties palette = tuple(reversed(palette)) counties = { code: county for code, county in counties.items() if county["state"] == "tx" } 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()] county_rates = [unemployment[county_id] for county_id in counties] color_mapper = LogColorMapper(palette=palette) data = dict( x=county_xs, y=county_ys, name=county_names, rate=county_rates, ) 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)
def gen_texas(): from bokeh.io import show from bokeh.models import LogColorMapper from bokeh.palettes import Viridis6 as palette from bokeh.plotting import figure from bokeh.sampledata.us_counties import data as counties from bokeh.sampledata.unemployment import data as unemployment palette.reverse() counties = { code: county for code, county in counties.items() if county["state"] == "tx" } 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()] county_rates = [unemployment[county_id] for county_id in counties] color_mapper = LogColorMapper(palette=palette) data=dict( x=county_xs, y=county_ys, name=county_names, rate=county_rates, ) TOOLS = "pan,wheel_zoom,reset,hover,save" p = figure( title="Texas Unemployment, 2009", tools=TOOLS, x_axis_location=None, y_axis_location=None, tooltips=[ ("Name", "@name"), ("Unemployment rate)", "@rate%"), ("(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) show(p)
def add_location_to_dfm(dfm, state="ca"): """ Given dataframe with County field adds county boundaries in order to be used in the plotting routine """ # Build County boundary DataFrame from bokeh county data from bokeh.sampledata.us_counties import data as counties counties = { code: county for code, county in counties.items() if county["state"] == state } 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()] location_df = pd.DataFrame({ 'County': county_names, 'x': county_xs, 'y': county_ys }) return dfm.merge(location_df, on='County', how='left')
def create_plot(ridb) : or_counties = { code: county for code, county in counties.items() if county["state"] == "or" } county_xs = [county["lons"] for county in or_counties.values()] county_ys = [county["lats"] for county in or_counties.values()] county_names = [county['name'] for county in or_counties.values()] lat = ridb['FacilityLatitude'] lon = ridb['FacilityLongitude'] source = ColumnDataSource(data=dict( x=county_xs, y=county_ys, name=ridb['FacilityName'] )) TOOLS="pan,wheel_zoom,box_zoom,reset,save" ridb = ridb.assign(Secret = "TRILLIUM") df_source = ColumnDataSource(data=ridb[['FacilityName','FacilityPhone','Secret']]) p = figure(title="Oregon Counties", tools=TOOLS) p.circle(lon, lat, size=8, color='navy', alpha=1, source=df_source, name='geo_points') p.patches('x', 'y', source=source, fill_color='green', fill_alpha=0.7, line_color="black", line_width=0.5) hover = HoverTool(names=['geo_points']) hover.tooltips = [(c, '@' + c) for c in ridb[['FacilityName','FacilityPhone','Secret']].columns] p.add_tools(hover) return p
def create_plot(ridb, list_to_display) : ridb_show = ridb[list_to_display] or_counties = { code: county for code, county in counties.items() if county["state"] == "or" } county_xs = [county["lons"] for county in or_counties.values()] county_ys = [county["lats"] for county in or_counties.values()] county_names = [county['name'] for county in or_counties.values()] lat = ridb['FacilityLatitude'] lon = ridb['FacilityLongitude'] source = ColumnDataSource(data=dict( x=county_xs, y=county_ys, )) TOOLS="pan,wheel_zoom,box_zoom,reset,save" df_source = ColumnDataSource(data=ridb_show) p = figure(title="Camping in Oregon!", tools=TOOLS) p.circle(lon, lat, size=8, color='navy', alpha=1, source=df_source, name='geo_points') p.patches('x', 'y', source=source, fill_color='green', fill_alpha=0.7, line_color="black", line_width=0.5) hover = HoverTool(names=['geo_points']) for c in ridb_show.columns: print(c) hover.tooltips = [(c, '@' + c) for c in ridb_show.columns] p.add_tools(hover) return p
x=latLong_recovered[0], y=latLong_recovered[1], size=10 * np.log10(total.loc[total['status'] == 'recovered'][latestDateCol]))) latLong_deaths = merc_from_arrays( total.loc[total['status'] == 'deaths']['Lat'], total.loc[total['status'] == 'deaths']['Long']) map_source_deaths = ColumnDataSource( data=dict(x=latLong_deaths[0], y=latLong_deaths[1], size=10 * np.log10(total.loc[total['status'] == 'deaths'][latestDateCol]))) #################################################### Choropleth counties = {code: county for code, county in counties.items()} US_county_confirmed = US_total.loc[US_total['status'] == 'confirmed']['Province/State'] US_county_recovered = US_total.loc[US_total['status'] == 'recovered']['Province/State'] US_county_deaths = US_total.loc[US_total['status'] == 'deaths']['Province/State'] # Join county patches to case count data by joining on county key print(f'---------------US_county_confirmed, {US_county_confirmed}') county_df = pd.DataFrame.from_dict(counties, orient='index') county_df['state'] = county_df['state'].str.upper() county_df['Province/State'] = county_df['name'] + ', ' + county_df['state'] county_join_df = US_total.join(county_df.set_index('Province/State'), on='Province/State')
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 map(): from bokeh.sampledata.us_counties import data as counties from bokeh.sampledata.unemployment import data as unemployment palette.reverse() counties = { code: county for code, county in counties.items() if county["state"] == "tx" } 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()] county_rates = [unemployment[county_id] for county_id in counties] color_mapper = LogColorMapper(palette=palette) source = ColumnDataSource(data=dict( x=county_xs, y=county_ys, name=county_names, rate=county_rates, )) TOOLS = "pan,wheel_zoom,reset,hover,save" p = figure(title="Texas Unemployment, 2009", tools=TOOLS, x_axis_location=None, y_axis_location=None) p.grid.grid_line_color = None p.patches('x', 'y', source=source, fill_color={ 'field': 'rate', 'transform': color_mapper }, fill_alpha=0.7, line_color="white", line_width=0.5) hover = p.select_one(HoverTool) hover.point_policy = "follow_mouse" hover.tooltips = [ ("Name", "@name"), ("Unemployment rate)", "@rate%"), ("(Long, Lat)", "($x, $y)"), ] #show(p) # grab the static resources js_resources = INLINE.render_js() css_resources = INLINE.render_css() # render template script, div = components(p) html = render_template( 'index.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, ) return encode_utf8(html)
unemptitle = input_state + " Unemployment Rate, " + input_year # creating dataframes and filter to stats we'll use datafile = file_input df_pcts = pd.read_csv(datafile) df_stats_unfiltered = df_pcts[['totalpopE', 'NAME', 'pctpoverty', 'pctfoodstamps', 'unemploymentrate']] # listing the counties we want the map to show county_filter = ['Sullivan County, New York', 'Ulster County, New York', 'Dutchess County, New York', 'Orange County, New York', 'Putnam County, New York', 'Rockland County, New York', 'Westchester County, New York', 'Bronx County, New York', 'New York County, New York', 'Queens County, New York', 'Kings County, New York', 'Richmond County, New York', 'Nassau County, New York', 'Suffolk County, New York'] df_filter = df_stats_unfiltered['NAME'].isin(county_filter) df_stats = df_stats_unfiltered[df_filter] # filtering counties counties = { code: county for code, county in counties.items() if county["state"] == "ny" and (county["detailed name"] in county_filter) } # getting x/y coords from the lon/lat in dataframe county county_xs = [county["lons"] for county in counties.values()] county_ys = [county["lats"] for county in counties.values()] # getting the data county_pop = df_stats['totalpopE'] county_names = df_stats['NAME'] county_poverty = df_stats['pctpoverty'] county_fsrate = df_stats['pctfoodstamps'] county_unemprate = df_stats['unemploymentrate'] # x/y and label for stadium barclays_y = 40.6826 #long/lat for stadium
from bokeh.models.widgets import Panel, Tabs from bokeh.io import output_file, show from bokeh.models import LogColorMapper, LogTicker, ColorBar from bokeh.models import ColumnDataSource, HoverTool import bokeh.palettes as bp from bokeh.palettes import Viridis6 as myPalette # @UnresolvedImport EXCLUDED = ("ak", "hi", "pr", "gu", "vi", "mp", "as") uscountiesdf = pd.DataFrame(us_counties_data) print(uscountiesdf) us_states = us_states.data.copy() counties = { code: county for code, county in us_counties_data.items() if county["state"] not in EXCLUDED } unemployment = unemployment.data del us_states["HI"] del us_states["AK"] state_xs = [us_states[code]["lons"] for code in us_states] state_ys = [us_states[code]["lats"] for code in us_states] county_xs = [ us_counties_data[code]["lons"] for code in us_counties_data if us_counties_data[code]["state"] not in EXCLUDED ] county_ys = [
x=latLong_recovered[0], y=latLong_recovered[1], size=10 * np.log10(total.loc[total['status'] == 'recovered'][dates[ timeslider.value]].replace(to_replace=0, value=1))) map_source_deaths.data = dict( x=latLong_deaths[0], y=latLong_deaths[1], size=10 * np.log10(total.loc[total['status'] == 'deaths'][dates[ timeslider.value]].replace(to_replace=0, value=1))) print(f'Current date: {dates[timeslider.value]}') #################################################### MAP PLOT #################################################### # Choropleth counties = {code: county for code, county in counties.items()} county_lons = [county["lons"] for county in counties.values()] county_lats = [county["lats"] for county in counties.values()] county_names = [county['name'] for county in counties.values()] US_latLong_confirmed = merc_from_arrays( total.loc[total['status'] == 'confirmed']['Lat'], total.loc[total['status'] == 'confirmed']['Long']) US_map_source_confirmed = ColumnDataSource(data=dict( x=latLong_confirmed[0], y=latLong_confirmed[1], size=10 * np.log10(total.loc[total['status'] == 'confirmed'][latestDateCol]))) US_latLong_recovered = merc_from_arrays(
def show_analytics(): st.title("Land analytics :seedling:") st.text( "Welcome to the analytics page, here you can easily view all your data" ) crops = { 'Peppers': 150, 'Tomatoes': 300, 'Beet': 100, 'Rice': 63, } data = pd.Series(crops).reset_index(name='value').rename( columns={'index': 'crop'}) data['angle'] = data['value'] / data['value'].sum() * 2 * np.pi data['color'] = Category20c[len(crops)] p = figure(plot_height=350, title="Pie Chart", toolbar_location=None, tools="hover", tooltips="@crop: @value", x_range=(-0.5, 1.0)) p.wedge(x=0, y=1, radius=0.3, start_angle=cumsum('angle', include_zero=True), end_angle=cumsum('angle'), line_color="white", fill_color='color', legend_field='crop', source=data) p.axis.axis_label = None p.axis.visible = False p.grid.grid_line_color = None st.markdown("## Crop diversity") st.bokeh_chart(p, use_container_width=True) st.markdown("## Land Nitrogen status") n = 500 x = 2 + 2 * np.random.uniform(-5, 5, n) y = 2 + 2 * np.random.uniform(-5, 5, n) p = figure(title="Farm hexagons", match_aspect=True, tools="wheel_zoom,reset", background_fill_color='#440154') p.grid.visible = False r, bins = p.hexbin(x, y, size=0.5, hover_color="pink", hover_alpha=0.8) p.circle(x, y, color="white", size=1) p.add_tools( HoverTool(tooltips=[("amount", "@c"), ("(q,r)", "(@q, @r)")], mode="mouse", point_policy="follow_mouse", renderers=[r])) st.bokeh_chart(p) st.markdown("## Soil humidity") palette = tuple(reversed(Viridis6)) counties = { code: county for code, county in us_counties.items() if county["state"] == "wy" } county_xs = [county["lons"] for county in counties.values()] county_ys = [county["lats"] for county in counties.values()] crops = [np.random.choice(list(crops.keys())) for _ in counties.values()] county_rates = [unemployment[county_id] for county_id in counties] color_mapper = LogColorMapper(palette=palette) data = dict( x=county_xs, y=county_ys, name=crops, rate=county_rates, ) TOOLS = "pan,wheel_zoom,reset,hover,save" p = figure(title="Soil humidity in your field", tools=TOOLS, x_axis_location=None, y_axis_location=None, tooltips=[("Crop", "@name"), ("Soil humidity", "@rate%"), ("(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) st.bokeh_chart(p)
import pandas as pd import numpy as np import argparse parser = argparse.ArgumentParser(description='Process some integers.') parser.add_argument('st', metavar='N', type=str, help='an integer for the accumulator') args = parser.parse_args() palette.reverse() counties = { code: county for code, county in counties.items() if county["state"] == args.st.lower() } us_election1 = pd.read_csv('../../data/xaa.csv', error_bad_lines=False, sep=';') us_election2 = pd.read_csv('../../data/xab.csv', error_bad_lines=False, sep=';') us_election3 = pd.read_csv('../../data/xac.csv', error_bad_lines=False, sep=';') us_election4 = pd.read_csv('../../data/xad.csv', error_bad_lines=False, sep=';') frames = [us_election1, us_election2, us_election3, us_election4]
from bokeh.io import show from bokeh.models import LogColorMapper from bokeh.palettes import Viridis6 as palette from bokeh.plotting import figure from bokeh.sampledata.us_counties import data as counties from bokeh.sampledata.unemployment import data as unemployment palette = tuple(reversed(palette)) counties = { code: county for code, county in counties.items() if county["state"] == "tx" } 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()] county_rates = [unemployment[county_id] for county_id in counties] color_mapper = LogColorMapper(palette=palette) data=dict( x=county_xs, y=county_ys, name=county_names, rate=county_rates, ) TOOLS = "pan,wheel_zoom,reset,hover,save" p = figure(
def getCountyDict(counties): countydict = {} for code, county in counties.items(): if counties[code]["state"] == "ia": countydict[code] = county return countydict
def plot_counties(df, variable_to_distribute, variables_to_display, state=None, logcolor=False): """Plots the distribution of a given variable across the given sttate Params ------ df df is a data frame containing the county level data variable_to_distribute variable_to_distribute is the variable that you want to see across the state variables_to_display Variables to display on hovering over each county output: Bokeh plotting object """ from bokeh.sampledata.us_counties import data as counties counties = { code: county for code, county in counties.items() if county["state"] == state.lower() } county_xs = [county["lons"] for county in counties.values()] county_ys = [county["lats"] for county in counties.values()] if variable_to_distribute in variables_to_display: variables_to_display.remove(variable_to_distribute) colors = palettes.RdBu11 #(n_colors) min_value = df[variable_to_distribute].min() max_value = df[variable_to_distribute].max() gran = (max_value - min_value) / float(len(colors)) #print variable_to_distribute,state,min_value,max_value index_range = [min_value + x*gran for x in range(len(colors))] county_colors = [] variable_dictionary = {} variable_dictionary["county_names"] = [county['name'] for county in counties.values()] variable_dictionary["x"] = county_xs variable_dictionary["y"] = county_ys variable_dictionary[re.sub("[^\w]","",variable_to_distribute)] = [] for vd in variables_to_display: variable_dictionary[re.sub("[^\w]","",vd)] = [] for county_id in counties: StateCountyID = str(county_id[0]).zfill(2) + str(county_id[1]).zfill(3) if StateCountyID in list(df["Header-FIPSStandCtyCode"].values): temp_var = df[df["Header-FIPSStandCtyCode"] == StateCountyID][variable_to_distribute].values[0] # if temp_var > 0.0: variable_dictionary[re.sub("[^\w]","",variable_to_distribute)].append(temp_var) for vd in variables_to_display: variable_dictionary[re.sub("[^\w]","",vd)].append(round(float(df[df["Header-FIPSStandCtyCode"] == StateCountyID][vd].values),2)) color_idx = list(temp_var - np.array(index_range)).index(min(x for x in list(temp_var - np.array(index_range)) if x >= 0)) county_colors.append(colors[color_idx]) ''' else: variable_dictionary[re.sub("[^\w]","",variable_to_distribute)].append(0.0) county_colors.append("#A9A9A9") for vd in variables_to_display: variable_dictionary[re.sub("[^\w]","",vd)].append(0.0) ''' else: variable_dictionary[re.sub("[^\w]","",variable_to_distribute)].append(0.0) county_colors.append("#A9A9A9") for vd in variables_to_display: variable_dictionary[re.sub("[^\w]","",vd)].append(0.0) #print temp_var,counties[county_id]["name"] variable_dictionary["color"] = county_colors source = ColumnDataSource(data = variable_dictionary) TOOLS = "pan,wheel_zoom,box_zoom,reset,hover,save" if logcolor: mapper = LogColorMapper(palette=colors, low=min_value, high=max_value) else: mapper = LinearColorMapper(palette=colors, low=min_value, high=max_value) color_bar = ColorBar(color_mapper=mapper, location=(0, 0), orientation='horizontal', title = variable_to_distribute,ticker=FixedTicker(ticks=index_range)) p = figure(title=variable_to_distribute, toolbar_location="left",tools=TOOLS, plot_width=1100, plot_height=700,x_axis_location=None, y_axis_location=None) p.patches('x', 'y', source=source, fill_alpha=0.7,fill_color='color', line_color="#884444", line_width=2) hover = p.select_one(HoverTool) hover.point_policy = "follow_mouse" tool_tips = [("County ", "@county_names")] for key in variable_dictionary.keys(): if key not in ["x","y","color","county_names"]: tool_tips.append((key,"@"+re.sub("[^\w]","",key) + "{1.11}")) hover.tooltips = tool_tips p.add_layout(color_bar, 'below') return p
from os.path import abspath import webbrowser import holoviews as hv from holoviews import opts hv.extension('bokeh') from bokeh.sampledata.us_counties import data as counties from bokeh.sampledata.unemployment import data as unemployment counties = [dict(county, Unemployment=unemployment[cid]) for cid, county in counties.items() if county["state"] == "tx"] choropleth = hv.Polygons(counties, ['lons', 'lats'], [('detailed name', 'County'), 'Unemployment']) choropleth.opts(opts.Polygons(logz=True, tools=['hover'], xaxis=None, yaxis=None, show_grid=False, show_frame=False, width=500, height=500, color_index='Unemployment', colorbar=True, toolbar='above', line_color='white')) hv.save(choropleth, 'texas.html', backend='bokeh') url = abspath('texas.html') webbrowser.open(url)
def make_map_plot(counties=counties, data=unemployment, year=2018, additional_data=True): # examples directory: http://bokeh.pydata.org/en/latest/docs/gallery/ # example adapted from http://bokeh.pydata.org/en/latest/docs/gallery/texas.html # changes color pallete assignments #palette.reverse() # grab polygons for Colorado counties only counties = { code: county for code, county in counties.items() if county["state"] == "co" } if additional_data: df = build_simple_model_df(save=False) #df = pd.read_parquet('data/processed_data/simple_modeling_set.parquet', engine='fastparquet') counties_df = pd.DataFrame.from_records(counties).T additional_data_df = pd.merge(df, counties_df, how='left', left_on='county', right_on='name') # subset to 2018 additional_data_df = additional_data_df[additional_data_df['year'] == year] # need to convert to this format for Bokeh to render the plot 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()] # This is what we will need to change to adapt the map to use different values # county_rates = [data[county_id] for county_id in counties] county_rates = additional_data_df['unemprate'] expected_consumers = additional_data_df['total_expected_consumers'] / 1000 # maps colors to the color_mapper = LogColorMapper(palette=palette) # Bokeh expects the data in a dict as follows data = dict( x=county_xs, y=county_ys, name=county_names, rate=county_rates, customers=expected_consumers, ) # all tools here: https://bokeh.pydata.org/en/latest/docs/user_guide/tools.html TOOLS = "pan,wheel_zoom,reset,hover,save" p = figure( # title="Colorado Unemployment, 2009", tools=TOOLS, title=f"Colorado County Statistics {str(year)}", tools=TOOLS, # sizing_mode controls how the plot responds to browser resizing sizing_mode="stretch_both", # if you are having issues with the image fitting in the plot box, # you may want to adjust the width policy value width_policy="fit", # We don't need axes for this plot x_axis_location=None, y_axis_location=None, # This is the fun stuff! And so easy to implement, what do we want # the chart to show on hover? tooltips=[("Name", "@name"), ("Unemployment rate", "@rate%"), ("Expected Customers (k)", "@customers"), ("(Long, Lat)", "($x, $y)")]) # A grid isn't going to make this easier to read, turn off p.grid.grid_line_color = None p.hover.point_policy = "follow_mouse" # MUST set to maintain aspect ratio of state p.match_aspect = True # patches is how polygons are passed for visualization p.patches('x', 'y', source=data, fill_color={ 'field': 'customers', 'transform': color_mapper }, fill_alpha=0.7, line_color="white", line_width=0.5) return p
from bokeh.io import show, output_file from bokeh.models import LogColorMapper from bokeh.palettes import Viridis6 as palette from bokeh.plotting import figure from bokeh.sampledata.us_counties import data as counties from bokeh.sampledata.unemployment import data as unemployment palette.reverse() counties = { code: county for code, county in counties.items() if county["state"] in ["md"] } 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()] county_rates = [unemployment[county_id] for county_id in counties] color_mapper = LogColorMapper(palette=palette) data = dict( x=county_xs, y=county_ys, name=county_names, rate=county_rates, ) TOOLS = "pan,wheel_zoom,reset,hover,save"
def make_dataset(self): per_capita = self.per_capita.active == 1 data_type = self.data_getter.labels[self.data_getter.active].lower() date = self.date.value excluded = ("ak", "hi", "pr", "gu", "vi", "mp", "as") counties = { abbrv: county for abbrv, county in US_COUNTIES.items() if county["state"] not in excluded } data = np.zeros(len(counties), dtype=float) cases = np.zeros(len(counties), dtype=float) deaths = np.zeros(len(counties), dtype=float) cases_pc = np.zeros(len(counties), dtype=float) deaths_pc = np.zeros(len(counties), dtype=float) pop = np.zeros(len(counties), dtype=int) if not per_capita: dt_label = data_type label = f"Total New {data_type.title()}" else: dt_label = f"{data_type}_pc" label = f"New {data_type.title()} per 100,000" subset = GH_COUNTIES_DATA.loc[GH_COUNTIES_DATA["date"] == date, :] for i, (abbrv, county) in enumerate(counties.items()): state_name, county_name = parse_detailed_name( county["detailed name"]) value = subset.loc[(subset["county"] == county_name).values & (subset["state"] == state_name).values, :, ] if not value.empty: dataval = value[f"avg_{dt_label}"].values[0] if not np.isnan(dataval): data[i] = max(0, dataval) else: data[i] = 0 cases[i] = value["avg_cases"].values[0] deaths[i] = value["avg_deaths"].values[0] cases_pc[i] = value["avg_cases_pc"].values[0] deaths_pc[i] = value["avg_deaths_pc"].values[0] pop[i] = population(f"{state_name}, {county_name}") if per_capita and data_type != "deaths": maxval = 1000 else: maxval = GH_COUNTIES_DATA.loc[:, f"avg_{dt_label}"].max() interp = ( compute_log_palette # if logarithmic else compute_linear_palette ) color_data = { "color": [interp(PALETTE, maxval / 256, maxval, val) for val in data], "value": data, "cases": cases, "deaths": deaths, "cases_pc": cases_pc, "deaths_pc": deaths_pc, "population": pop, "name": [county["detailed name"] for county in counties.values()], "lons": [], "lats": [], } for county in counties.values(): color_data["lons"].append(county["lons"]) color_data["lats"].append(county["lats"]) return label, maxval, ColumnDataSource(color_data)
print("\nPopl data at row 500:") print(df.loc[500]) # Make dictionary of state_id, cid tuple vs popl density. state_ids = df.state_id.tolist() cids = df.cid.tolist() den = df.Density.tolist() tuple_list = tuple(zip(state_ids, cids)) popl_dens_dict = dict(zip(tuple_list, den)) # Exclude states & territories not part of conterminious US. EXCLUDED = ('ak', 'hi', 'pr', 'gu', 'vi', 'mp', 'as') counties = [ dict(county, Density=popl_dens_dict[cid]) for cid, county in counties.items() if county["state"] not in EXCLUDED ] choropleth = hv.Polygons(counties, ['lons', 'lats'], [('detailed name', 'County'), 'Density']) choropleth.opts( opts.Polygons( logz=True, tools=['hover'], xaxis=None, yaxis=None, show_grid=False, show_frame=False, width=1100, height=700,
from bokeh.sampledata.unemployment import data as unemployment from bokeh.sampledata.us_counties import data as counties except: raise Exception( 'could not import data sets; run bokeh.sampledata.download() then re-run script.' ) # palette = tuple(reversed(palette)) # lower 48 plus dc for now excludes = ['ak', 'hi', 'pr', 'mp', 'vi', 'as', 'gu'] counties = { code: county for code, county in counties.items() if county["state"] not in excludes } 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()] #county_rates = [unemployment[county_id] for county_id in counties] county_rates = [] for county_id in counties: county_rates.append(unemployment.get(county_id, np.nan)) color_mapper = LogColorMapper(palette=palette) data = dict( x=county_xs, y=county_ys,
import bokeh.plotting as bk from bokeh.resources import CDN from bokeh.embed import components, autoload_static, autoload_server ############################################################################ # # Data Prep # ############################################################################ # get built-in data bokeh.sampledata.download() #limit by state !!! order changes with re-run of this script !!! counties = { code: county for code, county in counties.items() if county["state"] == "va" } # end !!! county_x = [county["lons"] for county in counties.values()] county_y = [county["lats"] for county in counties.values()] county_names = [county['name'] for county in counties.values()] ### experimental locs = pd.DataFrame(county_names) locs['x'] = county_x locs['y'] = county_y locs['name'] = locs[0] del locs[0] #delete bedford city as county equivalent and make location data frame
import os today_date = basic.get_today_date() csv_file_name = 'data/csv_county_summary/csv_county_summary_' + today_date + '.csv' index_file_name = 'index.html' readme_file_name = 'README.md' if os.path.exists(csv_file_name): df = pd.read_csv(csv_file_name) else: print('csv file not exists: ' + csv_file_name) exit(1) counties = { code: county for code, county in us_counties.items() if county["state"] == "oh" } 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()] # county_rates = [unemployment[county_id] for county_id in counties] county_counts = [] for index, county_name in enumerate(county_names): county_count = df.loc[df['County'] == county_name]['Case Count'].values if len(county_count) == 0: county_counts.append(0) else: county_counts.append(county_count[0])
bokeh.sampledata.download() from bokeh.sampledata.us_counties import data as counties from bokeh.sampledata.us_states import data as states from bokeh.sampledata.unemployment import data as unemployment from pprint import pprint from bokeh.models import (ColumnDataSource, LogColorMapper, LinearColorMapper, HoverTool) from bokeh.palettes import RdGy8 as palette palette.reverse() ## Filter in Indiana only counties = { code: county for code, county in counties.items() if county["state"] == "in" } 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()] county_rates = [unemployment[county_id] for county_id in counties] color_mapper = LogColorMapper(palette=palette) source = ColumnDataSource(data=dict( x=county_xs, y=county_ys, name=county_names, rate=county_rates, ))
import pandas as pd from bokeh.plotting import figure, ColumnDataSource from bokeh.io import curdoc from bokeh.models import HoverTool, LinearColorMapper from bokeh.models.widgets import Slider from bokeh.layouts import column, widgetbox from bokeh.sampledata.us_counties import data as counties from bokeh.palettes import PuBuGn8 #reading in GA population density data df = pd.read_csv(".\data\GA County Population Density 1980-2017.csv") #extracting ga county information ga_counties = {key: info for key, info in counties.items() if info['state']=='ga'} county_names = [county_info['name'] for county_info in ga_counties.values()] county_lons = [county_info['lons'] for county_info in ga_counties.values()] county_lats = [county_info['lats'] for county_info in ga_counties.values()] #initializing ColumnDataSource to 1980 densities source = ColumnDataSource(data=dict( x = county_lons, y = county_lats, county = county_names, density = df['1980'] )) #initializing Slider object slider = Slider(title='Year', value=1980, start=1980, end=2017) #updating source data on slider change
from bokeh.io import show from bokeh.models import (ColumnDataSource, HoverTool, LogColorMapper) from bokeh.palettes import Viridis6 as palette from bokeh.plotting import figure from bokeh.sampledata.us_counties import data as counties from bokeh.sampledata.unemployment import data as unemployment if __name__ == "__main__": palette.reverse() counties = {code: county \ for code, county in counties.items() \ if county["state"] == "tx" } 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()] county_rates = [unemployment[county_id] for county_id in counties] color_mapper = LogColorMapper(palette=palette) source = ColumnDataSource(data=dict( x=county_xs, y=county_ys, name=county_names, rate=county_rates)) p = figure(title="Texas Unemployment, 2009", tools="pan,wheel_zoom,reset,hover,save", x_axis_location=None, y_axis_location=None) p.grid.grid_line_color = None
def build_state_plot(): from bokeh.sampledata.us_counties import data as counties print('building data sets...') alum_points = create_dictionary_data('./local/alum_lat_lon.csv') cm_points = create_dictionary_data('./local/cm_lat_lon.csv') school_points = create_dictionary_data('./local/school_lat_lon.csv') supporter_points = create_dictionary_data('./local/supporters_lat_lon.csv') print('school data...') cm_data, cm_counties = open_cm_data() for cm in cm_data: students, county = int(cm['num']), cm['county'] cm_counties[county]['num'] += students cm_counties[county]['cms'] += 1 print('creating county info...') counties = { code: county for code, county in counties.items() if (county['state'] == 'ms' or county['state'] == 'ar') } student_numbers = [] for code in counties.keys(): if counties[code]['name'].replace( ' County', '') in cm_counties.keys() and cm_counties.get( counties[code]['name'], { 'state': '' }).get('state', '') == counties[code]['state']: counties[code]['students'] = cm_counties[counties[code] ['name']].get('num', 0) student_numbers.append(cm_counties[counties[code]['name']].get( 'num', 0)) counties[code]['cms'] = cm_counties[counties[code]['name']].get( 'cms', 0) else: counties[code]['students'] = 0 counties[code]['cms'] = 0 max_students = max(student_numbers) print('building plot...') output_file('./map.html') county_xs = [county["lons"] for county in counties.values()] county_ys = [county["lats"] for county in counties.values()] county_names = [ '{} County'.format(county['name']) for county in counties.values() ] county_place = [bool(county['students']) for county in counties.values()] county_students = [ '{} students'.format(county['students']) for county in counties.values() ] county_cms = [ '{} Corps Members'.format(county['cms']) for county in counties.values() ] palette = [CUSTOM_COLORS['Dark Blue'], CUSTOM_COLORS['Teal']] color_mapper = LogColorMapper(palette=palette) source = ColumnDataSource(data=dict(x=county_xs, y=county_ys, county=county_names, has_cm=county_place, students=county_students)) TOOLS = "pan,wheel_zoom,reset,save" p = figure(tools=TOOLS, active_drag="pan", active_scroll=None, active_tap=None, active_inspect=None, x_axis_location=None, y_axis_location=None, toolbar_location="left") p.grid.grid_line_color = None p.sizing_mode = 'scale_width' # p.sizing_mode = 'scale_height' p.patches('x', 'y', source=source, fill_color={ 'field': 'has_cm', 'transform': color_mapper }, fill_alpha=1, line_color="white", line_width=2) # school data points source = ColumnDataSource( dict( x=school_points['lon'], y=school_points['lat'], city=school_points['city'], name=school_points['name'], type=school_points['type'], cms=school_points['number'], )) g = p.square(x="x", y="y", source=source, size=8, color=CUSTOM_COLORS['Purple'], alpha=1, legend='Schools') hover1 = HoverTool(renderers=[g], tooltips=[ ("School Name", "@name"), ("Location", "@city"), ("Type of Partner", "@type"), ("Number of CMs", "@cms"), ]) p.add_tools(hover1) # donor data points source = ColumnDataSource( dict( x=supporter_points['lon'], y=supporter_points['lat'], city=supporter_points['city'], name=supporter_points['name'], donate=supporter_points['last_donate'], )) g = p.circle(x="x", y="y", source=source, size=8, color=CUSTOM_COLORS['Blue'], alpha=1, legend='Supporters') hover2 = HoverTool(renderers=[g], tooltips=[ ("Donor Name", "@name"), ("Location", "@city"), ("Latest Donation", "@donate"), ]) p.add_tools(hover2) # alumni data points source = ColumnDataSource( dict( x=alum_points['lon'], y=alum_points['lat'], city=alum_points['city'], name=alum_points['name'], corps=alum_points['corps'], title=alum_points['title'], employer=alum_points['employer'], profession=alum_points['profession'], last_survey=alum_points['last_survey'], nps=alum_points['nps'], )) g = p.circle(x="x", y="y", source=source, size=8, color=CUSTOM_COLORS['Red'], alpha=1, legend='Alumni') hover3 = HoverTool(renderers=[g], tooltips=[ ("Alum Name", "@name"), ("Corps", "@corps"), ("Location", "@city"), ("Title", "@title"), ("Employer", "@employer"), ("profession", "@profession"), ("Last Alum Survey", "@last_survey"), ("Latest NPS", "@nps"), ]) p.add_tools(hover3) # cm data points source = ColumnDataSource( dict( x=cm_points['lon'], y=cm_points['lat'], city=cm_points['city'], name=cm_points['name'], school=cm_points['school'], coach=cm_points['coach'], content=cm_points['content'], num=cm_points['num'], yol=cm_points['yol'], )) g = p.circle(x="x", y="y", source=source, size=8, color=CUSTOM_COLORS['Orange'], alpha=1, legend='Corps Members') hover4 = HoverTool(renderers=[g], tooltips=[ ("CM Name", "@name"), ("Location", "@city"), ("School", "@school"), ("Coach", "@coach"), ("Content", "@content"), ("Number of Students", "@num"), ("Expected Growth", "@yol"), ]) p.add_tools(hover4) p.legend.location = "top_right" p.legend.click_policy = "hide" p.toolbar.active_inspect = [hover2, hover3, hover4] # p.toolbar.active_inspect = [] show(p)