vishelper.geo package

Submodules

vishelper.geo.geo module

vishelper.geo.geo.add_df_latlon(df, lat_col='lat', lng_col='lng', radius_col=None, radius=15000, color_col=None, raw_color_col='color', color_how=None, colors=None, outline_color=None, raw_outline_color_col=None, weight=1, fill=True, fill_opacity=1, color_continuous_kwargs=None, popup_col=None, max_popup_width=2650, fmap=None, **kwargs)[source]

Add points to a map from data in a dataframe. Can color and size points based on data columns.

Parameters
  • df (pandas.DataFrame()) – Dataframe containing at least lat_col and lng_col. If coloring according to data or adding a popup, dataframe should contain color_col, popup_col, and/or raw_color_col.

  • lat_col (str) – Name of column containing latitude

  • lng_col (str) – Name of column containing longitude

  • radius_col (str) – Name of column containing values for the radius of each point. Optional. If not provided, radius will be used for all points.

  • radius (int) – Radius of point if constant for all points. Default 15000.

  • color_col (str) – Name of column containing data to color the points according to. Must provide color_how to

  • raw_color_col (str) – If color_how=’raw’, name of column containing raw color name for each point. If color_how = ‘continuous’ or ‘categorical’, color assignments will be put in a column named raw_color_col. Default is ‘color’. It will not be used if color_how is not provided.

  • color_how

    How to color the points. Options include: * None: The color provided in colors will be used for all points. If colors contains a list, the first

    color in the list will be used.

    • Raw: The color in raw_color_col will be used as the color for each point.

    • Continuous: Points will be colored based on a colormap and the values of data in the `color_col

  • colors (str or list) – If color_col and raw_color_col are None, value or first value of list are used to color each point. If color_col is not None and color_how = ‘categorical’, list of colors is used.

  • outline_color (str) –

  • raw_outline_color_col (str) – name of column containing raw color name for the outline of each point.

  • fill (bool) – If True, circles will be filled in with color. Default True.

  • fill_opacity (float) – Opacity of area fill, range 0-1.

  • color_continuous_kwargs

  • popup_col (str) – Name of column containing information to display in a hover popup on map. Optional.

  • max_popup_width (int) – Maximum width of popup box. Default 2650.

  • fmap (folium.Map) – If a map object has already been created, it can be provided and points will be added to it.

  • **kwargs

Returns:

vishelper.geo.geo.add_latlons(latlons, color=None, fmap=None, fill=True, lines=False, line_color='black', **kwargs)[source]
vishelper.geo.geo.add_line(latlonA, latlonB, line_color='black', fmap=None, **kwargs)[source]
vishelper.geo.geo.basic_map(**kwargs)[source]
Parameters

**kwargs – location_start (tuple): Lat/lon to center map on to begin with. Default given by vh.geo_formatting. zoom_start (int): Level of zoom, 1-10. Default given by vh.geo_formatting tiles (str): Name of folium tiles to use. Default given by vh.geo_formatting.

Returns

folium map object

Return type

fmap (folium.Map)

vishelper.geo.geo.filter_geojson(geojson_dict, df, geo_col, geojson_key='ZCTA5CE10', filter_geos=True)[source]
vishelper.geo.geo.html_to_png(htmlpath=None, pngpath=None, delay=5, width=2560, ratio=0.5625, browser=None)[source]
vishelper.geo.geo.plot_map(df=None, color_column=None, geo_column=None, geo_type=None, geo_data=None, key_on=None, legend_name=None, fmap=None, reset=True, fill_color=None, filter_geos=True, threshold_scale=None, **kwargs)[source]

Creates a choropleth map based on a dataframe. Colors regions in a map based on provided data.

Must provide geo_type or geo_data and key_on.

geo_type options:
  • ‘us_states’: will join to a column containing state names. See

    to_state_name() to convert state abbreviations to full state names

  • ‘zip3’: will join to a column containing zip3s

  • ‘kma’: will join to a column containing key market area ids

Parameters
  • df – Dataframe containing color_column and geo_column. If None, all regions will be colored a single color.

  • color_column (str) – Name of column in dataframe containing data to color map according to. If None, df must be None as well. All regions will be colored a single color.

  • geo_column (str) – Name of column in dataframe containing geographic key that can join to key_on in geoJson. If None, df must be None as well. All regions will be colored a single color.

  • geo_type (str) – If provided, maps to a geo_data and key_on for a given geography

  • above. (type. Default None. Must provide this or geo_data and key_on. See geo_type options) –

  • geo_data (str) – URL, file path, or data (json, dict, geopandas, etc) to your GeoJSON geometries. Required if geo_type not provided.

  • key_on (str) – Variable in the geo_data GeoJSON file to bind the data to. Must start with ‘feature’ and be in JavaScript objection notation. Ex: ‘feature.id’ or ‘feature.properties.statename’. If not provided, must have geo_type specified.

  • legend_name (str) – Name of legend. If None, legend will be labeled with color_column name formatted witH underscores removed.

  • fmap (folium.Map) – map object. Default None - will create new map.

  • reset (bool) – Default True. Clears map data from fmap object.

  • fill_color (str) – Area fill color. Can pass a hex code, color name, or if you are binding data, one of the following color brewer palettes: ‘BuGn’, ‘BuPu’, ‘GnBu’, ‘OrRd’, ‘PuBu’, ‘PuBuGn’, ‘PuRd’, ‘RdPu’, ‘YlGn’, ‘YlGnBu’, ‘YlOrBr’, and ‘YlOrRd’. Default given by vh.geo_formatting.

  • filter_geos (bool) – If True, will filter out all geoJSON entries for regions not included in the dataframe, df. This is typically necessary as folium generally won’t plot anything if there isn’t a match for each region.

  • threshold_scale (list) – List of thresholds for coloring the data. The list must have more than three values, each item must be larger in value than the last. All data must have values between the first and last items of the list. Example: [100, 200, 300, 400] In this case, all data in df[color_column] must be between 100 and 400.

  • **kwargs – location_start (tuple): Lat/lon to center map on to begin with. Default given by vh.geo_formatting. zoom_start (int): Level of zoom, 1-10. Default given by vh.geo_formatting tiles (str): Name of folium tiles to use. Default given by vh.geo_formatting. fill_opacity (float): Opacity of area fill, range 0-1. Default given by vh.geo_formatting. line_opacity (float): Opacity of line, range 0-1. Default given by vh.geo_formatting.

Returns

Map with geographic regions colored according to provided data

Return type

fmap (folium.Map)

vishelper.geo.geo.plot_subset(plot_function, df, option, option_column, **kwargs)[source]
vishelper.geo.geo.read_geojson(geo_data)[source]
vishelper.geo.geo.save_map(fmap, htmlpath=None, pngpath=None, png=False, delay=5, width=2560, ratio=0.5625, browser=None)[source]
vishelper.geo.geo.slider_interact(plot_function, df, options, option_column, initial_option=None, option_label=None, disabled=False, button_style='', **kwargs)[source]
vishelper.geo.geo.to_geo_dir(x)
vishelper.geo.geo.to_state_name(df, state_column, new_column)[source]

Creates a column with the state names corresponding to the abbreviations in the state_column provided.

Parameters
  • df – Dataframe containing column name

  • state_column – Name of column containing state abbreviations

  • new_column – Name of column to create with state names

Returns

Dataframe now containing new_column

Return type

df

Module contents