def plot_dataframe( df, column=None, cmap=None, color=None, ax=None, cax=None, categorical=False, legend=False, scheme=None, k=5, vmin=None, vmax=None, markersize=None, figsize=None, legend_kwds=None, categories=None, classification_kwds=None, missing_kwds=None, aspect="auto", **style_kwds, ): """ Plot a GeoDataFrame. Generate a plot of a GeoDataFrame with matplotlib. If a column is specified, the plot coloring will be based on values in that column. Parameters ---------- column : str, np.array, pd.Series (default None) The name of the dataframe column, np.array, or pd.Series to be plotted. If np.array or pd.Series are used then it must have same length as dataframe. Values are used to color the plot. Ignored if `color` is also set. kind: str The kind of plots to produce: - 'geo': Map (default) Pandas Kinds - 'line' : line plot - 'bar' : vertical bar plot - 'barh' : horizontal bar plot - 'hist' : histogram - 'box' : BoxPlot - 'kde' : Kernel Density Estimation plot - 'density' : same as 'kde' - 'area' : area plot - 'pie' : pie plot - 'scatter' : scatter plot - 'hexbin' : hexbin plot. cmap : str (default None) The name of a colormap recognized by matplotlib. color : str (default None) If specified, all objects will be colored uniformly. ax : matplotlib.pyplot.Artist (default None) axes on which to draw the plot cax : matplotlib.pyplot Artist (default None) axes on which to draw the legend in case of color map. categorical : bool (default False) If False, cmap will reflect numerical values of the column being plotted. For non-numerical columns, this will be set to True. legend : bool (default False) Plot a legend. Ignored if no `column` is given, or if `color` is given. scheme : str (default None) Name of a choropleth classification scheme (requires mapclassify). A mapclassify.MapClassifier object will be used under the hood. Supported are all schemes provided by mapclassify (e.g. 'BoxPlot', 'EqualInterval', 'FisherJenks', 'FisherJenksSampled', 'HeadTailBreaks', 'JenksCaspall', 'JenksCaspallForced', 'JenksCaspallSampled', 'MaxP', 'MaximumBreaks', 'NaturalBreaks', 'Quantiles', 'Percentiles', 'StdMean', 'UserDefined'). Arguments can be passed in classification_kwds. k : int (default 5) Number of classes (ignored if scheme is None) vmin : None or float (default None) Minimum value of cmap. If None, the minimum data value in the column to be plotted is used. vmax : None or float (default None) Maximum value of cmap. If None, the maximum data value in the column to be plotted is used. markersize : str or float or sequence (default None) Only applies to point geometries within a frame. If a str, will use the values in the column of the frame specified by markersize to set the size of markers. Otherwise can be a value to apply to all points, or a sequence of the same length as the number of points. figsize : tuple of integers (default None) Size of the resulting matplotlib.figure.Figure. If the argument axes is given explicitly, figsize is ignored. legend_kwds : dict (default None) Keyword arguments to pass to matplotlib.pyplot.legend() or matplotlib.pyplot.colorbar(). Additional accepted keywords when `scheme` is specified: fmt : string A formatting specification for the bin edges of the classes in the legend. For example, to have no decimals: ``{"fmt": "{:.0f}"}``. labels : list-like A list of legend labels to override the auto-generated labels. Needs to have the same number of elements as the number of classes (`k`). interval : boolean (default False) An option to control brackets from mapclassify legend. If True, open/closed interval brackets are shown in the legend. categories : list-like Ordered list-like object of categories to be used for categorical plot. classification_kwds : dict (default None) Keyword arguments to pass to mapclassify missing_kwds : dict (default None) Keyword arguments specifying color options (as style_kwds) to be passed on to geometries with missing values in addition to or overwriting other style kwds. If None, geometries with missing values are not plotted. aspect : 'auto', 'equal', None or float (default 'auto') Set aspect of axis. If 'auto', the default aspect for map plots is 'equal'; if however data are not projected (coordinates are long/lat), the aspect is by default set to 1/cos(df_y * pi/180) with df_y the y coordinate of the middle of the GeoDataFrame (the mean of the y range of bounding box) so that a long/lat square appears square in the middle of the plot. This implies an Equirectangular projection. If None, the aspect of `ax` won't be changed. It can also be set manually (float) as the ratio of y-unit to x-unit. **style_kwds : dict Style options to be passed on to the actual plot function, such as ``edgecolor``, ``facecolor``, ``linewidth``, ``markersize``, ``alpha``. Returns ------- ax : matplotlib axes instance Examples -------- >>> df = geopandas.read_file(geopandas.datasets.get_path("naturalearth_lowres")) >>> df.head() # doctest: +SKIP pop_est continent name iso_a3 \ gdp_md_est geometry 0 920938 Oceania Fiji FJI 8374.0 MULTIPOLY\ GON (((180.00000 -16.06713, 180.00000... 1 53950935 Africa Tanzania TZA 150600.0 POLYGON (\ (33.90371 -0.95000, 34.07262 -1.05982... 2 603253 Africa W. Sahara ESH 906.5 POLYGON (\ (-8.66559 27.65643, -8.66512 27.58948... 3 35623680 North America Canada CAN 1674000.0 MULTIPOLY\ GON (((-122.84000 49.00000, -122.9742... 4 326625791 North America United States of America USA 18560000.0 MULTIPOLY\ GON (((-122.84000 49.00000, -120.0000... >>> df.plot("pop_est", cmap="Blues") # doctest: +SKIP See the User Guide page :doc:`../../user_guide/mapping` for details. """ if "colormap" in style_kwds: warnings.warn( "'colormap' is deprecated, please use 'cmap' instead " "(for consistency with matplotlib)", FutureWarning, ) cmap = style_kwds.pop("colormap") if "axes" in style_kwds: warnings.warn( "'axes' is deprecated, please use 'ax' instead " "(for consistency with pandas)", FutureWarning, ) ax = style_kwds.pop("axes") if column is not None and color is not None: warnings.warn( "Only specify one of 'column' or 'color'. Using 'color'.", UserWarning) column = None try: import matplotlib.pyplot as plt except ImportError: raise ImportError( "The matplotlib package is required for plotting in geopandas. " "You can install it using 'conda install -c conda-forge matplotlib' or " "'pip install matplotlib'.") if ax is None: if cax is not None: raise ValueError("'ax' can not be None if 'cax' is not.") fig, ax = plt.subplots(figsize=figsize) if aspect == "auto": if df.crs and df.crs.is_geographic: bounds = df.total_bounds y_coord = np.mean([bounds[1], bounds[3]]) ax.set_aspect(1 / np.cos(y_coord * np.pi / 180)) # formula ported from R package sp # https://github.com/edzer/sp/blob/master/R/mapasp.R else: ax.set_aspect("equal") elif aspect is not None: ax.set_aspect(aspect) # GH 1555 # if legend_kwds set, copy so we don't update it in place if legend_kwds is not None: legend_kwds = legend_kwds.copy() if df.empty: warnings.warn( "The GeoDataFrame you are attempting to plot is " "empty. Nothing has been displayed.", UserWarning, ) return ax if isinstance(markersize, str): markersize = df[markersize].values if column is None: return plot_series( df.geometry, cmap=cmap, color=color, ax=ax, figsize=figsize, markersize=markersize, aspect=aspect, **style_kwds, ) # To accept pd.Series and np.arrays as column if isinstance(column, (np.ndarray, pd.Series)): if column.shape[0] != df.shape[0]: raise ValueError( "The dataframe and given column have different number of rows." ) else: values = column # Make sure index of a Series matches index of df if isinstance(values, pd.Series): values = values.reindex(df.index) else: values = df[column] if pd.api.types.is_categorical_dtype(values.dtype): if categories is not None: raise ValueError( "Cannot specify 'categories' when column has categorical dtype" ) categorical = True elif values.dtype is np.dtype("O") or categories: categorical = True nan_idx = np.asarray(pd.isna(values), dtype="bool") if scheme is not None: mc_err = ("The 'mapclassify' package (>= 2.4.0) is " "required to use the 'scheme' keyword.") try: import mapclassify except ImportError: raise ImportError(mc_err) if mapclassify.__version__ < LooseVersion("2.4.0"): raise ImportError(mc_err) if classification_kwds is None: classification_kwds = {} if "k" not in classification_kwds: classification_kwds["k"] = k binning = mapclassify.classify(np.asarray(values[~nan_idx]), scheme, **classification_kwds) # set categorical to True for creating the legend categorical = True if legend_kwds is not None and "labels" in legend_kwds: if len(legend_kwds["labels"]) != binning.k: raise ValueError("Number of labels must match number of bins, " "received {} labels for {} bins".format( len(legend_kwds["labels"]), binning.k)) else: labels = list(legend_kwds.pop("labels")) else: fmt = "{:.2f}" if legend_kwds is not None and "fmt" in legend_kwds: fmt = legend_kwds.pop("fmt") labels = binning.get_legend_classes(fmt) if legend_kwds is not None: show_interval = legend_kwds.pop("interval", False) else: show_interval = False if not show_interval: labels = [c[1:-1] for c in labels] values = pd.Categorical([np.nan] * len(values), categories=binning.bins, ordered=True) values[~nan_idx] = pd.Categorical.from_codes(binning.yb, categories=binning.bins, ordered=True) if cmap is None: cmap = "viridis" # Define `values` as a Series if categorical: if cmap is None: cmap = "tab10" cat = pd.Categorical(values, categories=categories) categories = list(cat.categories) # values missing in the Categorical but not in original values missing = list(np.unique(values[~nan_idx & cat.isna()])) if missing: raise ValueError( "Column contains values not listed in categories. " "Missing categories: {}.".format(missing)) values = cat.codes[~nan_idx] vmin = 0 if vmin is None else vmin vmax = len(categories) - 1 if vmax is None else vmax # fill values with placeholder where were NaNs originally to map them properly # (after removing them in categorical or scheme) if categorical: for n in np.where(nan_idx)[0]: values = np.insert(values, n, values[0]) mn = values[~np.isnan(values)].min() if vmin is None else vmin mx = values[~np.isnan(values)].max() if vmax is None else vmax # decompose GeometryCollections geoms, multiindex = _flatten_multi_geoms(df.geometry, prefix="Geom") values = np.take(values, multiindex, axis=0) nan_idx = np.take(nan_idx, multiindex, axis=0) expl_series = geopandas.GeoSeries(geoms) geom_types = expl_series.type poly_idx = np.asarray((geom_types == "Polygon") | (geom_types == "MultiPolygon")) line_idx = np.asarray((geom_types == "LineString") | (geom_types == "MultiLineString") | (geom_types == "LinearRing")) point_idx = np.asarray((geom_types == "Point") | (geom_types == "MultiPoint")) # plot all Polygons and all MultiPolygon components in the same collection polys = expl_series[poly_idx & np.invert(nan_idx)] subset = values[poly_idx & np.invert(nan_idx)] if not polys.empty: _plot_polygon_collection(ax, polys, subset, vmin=mn, vmax=mx, cmap=cmap, **style_kwds) # plot all LineStrings and MultiLineString components in same collection lines = expl_series[line_idx & np.invert(nan_idx)] subset = values[line_idx & np.invert(nan_idx)] if not lines.empty: _plot_linestring_collection(ax, lines, subset, vmin=mn, vmax=mx, cmap=cmap, **style_kwds) # plot all Points in the same collection points = expl_series[point_idx & np.invert(nan_idx)] subset = values[point_idx & np.invert(nan_idx)] if not points.empty: if isinstance(markersize, np.ndarray): markersize = np.take(markersize, multiindex, axis=0) markersize = markersize[point_idx & np.invert(nan_idx)] _plot_point_collection( ax, points, subset, vmin=mn, vmax=mx, markersize=markersize, cmap=cmap, **style_kwds, ) if missing_kwds is not None and not expl_series[nan_idx].empty: if color: if "color" not in missing_kwds: missing_kwds["color"] = color merged_kwds = style_kwds.copy() merged_kwds.update(missing_kwds) plot_series(expl_series[nan_idx], ax=ax, **merged_kwds) if legend and not color: if legend_kwds is None: legend_kwds = {} if "fmt" in legend_kwds: legend_kwds.pop("fmt") from matplotlib.lines import Line2D from matplotlib.colors import Normalize from matplotlib import cm norm = style_kwds.get("norm", None) if not norm: norm = Normalize(vmin=mn, vmax=mx) n_cmap = cm.ScalarMappable(norm=norm, cmap=cmap) if categorical: if scheme is not None: categories = labels patches = [] for value, cat in enumerate(categories): patches.append( Line2D( [0], [0], linestyle="none", marker="o", alpha=style_kwds.get("alpha", 1), markersize=10, markerfacecolor=n_cmap.to_rgba(value), markeredgewidth=0, )) if missing_kwds is not None: if "color" in merged_kwds: merged_kwds["facecolor"] = merged_kwds["color"] patches.append( Line2D( [0], [0], linestyle="none", marker="o", alpha=merged_kwds.get("alpha", 1), markersize=10, markerfacecolor=merged_kwds.get("facecolor", None), markeredgecolor=merged_kwds.get("edgecolor", None), markeredgewidth=merged_kwds.get( "linewidth", 1 if merged_kwds.get("edgecolor", False) else 0), )) categories.append(merged_kwds.get("label", "NaN")) legend_kwds.setdefault("numpoints", 1) legend_kwds.setdefault("loc", "best") ax.legend(patches, categories, **legend_kwds) else: if cax is not None: legend_kwds.setdefault("cax", cax) else: legend_kwds.setdefault("ax", ax) n_cmap.set_array(np.array([])) ax.get_figure().colorbar(n_cmap, **legend_kwds) plt.draw() return ax
def _explore( df, column=None, cmap=None, color=None, m=None, tiles="OpenStreetMap", attr=None, tooltip=True, popup=False, highlight=True, categorical=False, legend=True, scheme=None, k=5, vmin=None, vmax=None, width="100%", height="100%", categories=None, classification_kwds=None, control_scale=True, marker_type=None, marker_kwds={}, style_kwds={}, highlight_kwds={}, missing_kwds={}, tooltip_kwds={}, popup_kwds={}, legend_kwds={}, **kwargs, ): """Interactive map based on GeoPandas and folium/leaflet.js Generate an interactive leaflet map based on :class:`~geopandas.GeoDataFrame` Parameters ---------- column : str, np.array, pd.Series (default None) The name of the dataframe column, :class:`numpy.array`, or :class:`pandas.Series` to be plotted. If :class:`numpy.array` or :class:`pandas.Series` are used then it must have same length as dataframe. cmap : str, matplotlib.Colormap, branca.colormap or function (default None) The name of a colormap recognized by ``matplotlib``, a list-like of colors, :class:`matplotlib.colors.Colormap`, a :class:`branca.colormap.ColorMap` or function that returns a named color or hex based on the column value, e.g.:: def my_colormap(value): # scalar value defined in 'column' if value > 1: return "green" return "red" color : str, array-like (default None) Named color or a list-like of colors (named or hex). m : folium.Map (default None) Existing map instance on which to draw the plot. tiles : str, xyzservices.TileProvider (default 'OpenStreetMap Mapnik') Map tileset to use. Can choose from the list supported by folium, query a :class:`xyzservices.TileProvider` by a name from ``xyzservices.providers``, pass :class:`xyzservices.TileProvider` object or pass custom XYZ URL. The current list of built-in providers (when ``xyzservices`` is not available): ``["OpenStreetMap", "Stamen Terrain", “Stamen Toner", “Stamen Watercolor" "CartoDB positron", “CartoDB dark_matter"]`` You can pass a custom tileset to Folium by passing a Leaflet-style URL to the tiles parameter: ``http://{s}.yourtiles.com/{z}/{x}/{y}.png``. Be sure to check their terms and conditions and to provide attribution with the ``attr`` keyword. attr : str (default None) Map tile attribution; only required if passing custom tile URL. tooltip : bool, str, int, list (default True) Display GeoDataFrame attributes when hovering over the object. ``True`` includes all columns. ``False`` removes tooltip. Pass string or list of strings to specify a column(s). Integer specifies first n columns to be included. Defaults to ``True``. popup : bool, str, int, list (default False) Input GeoDataFrame attributes for object displayed when clicking. ``True`` includes all columns. ``False`` removes popup. Pass string or list of strings to specify a column(s). Integer specifies first n columns to be included. Defaults to ``False``. highlight : bool (default True) Enable highlight functionality when hovering over a geometry. categorical : bool (default False) If ``False``, ``cmap`` will reflect numerical values of the column being plotted. For non-numerical columns, this will be set to True. legend : bool (default True) Plot a legend in choropleth plots. Ignored if no ``column`` is given. scheme : str (default None) Name of a choropleth classification scheme (requires ``mapclassify`` >= 2.4.0). A :func:`mapclassify.classify` will be used under the hood. Supported are all schemes provided by ``mapclassify`` (e.g. ``'BoxPlot'``, ``'EqualInterval'``, ``'FisherJenks'``, ``'FisherJenksSampled'``, ``'HeadTailBreaks'``, ``'JenksCaspall'``, ``'JenksCaspallForced'``, ``'JenksCaspallSampled'``, ``'MaxP'``, ``'MaximumBreaks'``, ``'NaturalBreaks'``, ``'Quantiles'``, ``'Percentiles'``, ``'StdMean'``, ``'UserDefined'``). Arguments can be passed in ``classification_kwds``. k : int (default 5) Number of classes vmin : None or float (default None) Minimum value of ``cmap``. If ``None``, the minimum data value in the column to be plotted is used. vmax : None or float (default None) Maximum value of ``cmap``. If ``None``, the maximum data value in the column to be plotted is used. width : pixel int or percentage string (default: '100%') Width of the folium :class:`~folium.folium.Map`. If the argument m is given explicitly, width is ignored. height : pixel int or percentage string (default: '100%') Height of the folium :class:`~folium.folium.Map`. If the argument m is given explicitly, height is ignored. categories : list-like Ordered list-like object of categories to be used for categorical plot. classification_kwds : dict (default None) Keyword arguments to pass to mapclassify control_scale : bool, (default True) Whether to add a control scale on the map. marker_type : str, folium.Circle, folium.CircleMarker, folium.Marker (default None) Allowed string options are ('marker', 'circle', 'circle_marker'). Defaults to folium.CircleMarker. marker_kwds: dict (default {}) Additional keywords to be passed to the selected ``marker_type``, e.g.: radius : float (default 2 for ``circle_marker`` and 50 for ``circle``)) Radius of the circle, in meters (for ``circle``) or pixels (for ``circle_marker``). fill : bool (default True) Whether to fill the ``circle`` or ``circle_marker`` with color. icon : folium.map.Icon the :class:`folium.map.Icon` object to use to render the marker. draggable : bool (default False) Set to True to be able to drag the marker around the map. style_kwds : dict (default {}) Additional style to be passed to folium ``style_function``: stroke : bool (default True) Whether to draw stroke along the path. Set it to ``False`` to disable borders on polygons or circles. color : str Stroke color weight : int Stroke width in pixels opacity : float (default 1.0) Stroke opacity fill : boolean (default True) Whether to fill the path with color. Set it to ``False`` to disable filling on polygons or circles. fillColor : str Fill color. Defaults to the value of the color option fillOpacity : float (default 0.5) Fill opacity. Plus all supported by :func:`folium.vector_layers.path_options`. See the documentation of :class:`folium.features.GeoJson` for details. highlight_kwds : dict (default {}) Style to be passed to folium highlight_function. Uses the same keywords as ``style_kwds``. When empty, defaults to ``{"fillOpacity": 0.75}``. tooltip_kwds : dict (default {}) Additional keywords to be passed to :class:`folium.features.GeoJsonTooltip`, e.g. ``aliases``, ``labels``, or ``sticky``. popup_kwds : dict (default {}) Additional keywords to be passed to :class:`folium.features.GeoJsonPopup`, e.g. ``aliases`` or ``labels``. legend_kwds : dict (default {}) Additional keywords to be passed to the legend. Currently supported customisation: caption : string Custom caption of the legend. Defaults to the column name. Additional accepted keywords when ``scheme`` is specified: colorbar : bool (default True) An option to control the style of the legend. If True, continuous colorbar will be used. If False, categorical legend will be used for bins. scale : bool (default True) Scale bins along the colorbar axis according to the bin edges (True) or use the equal length for each bin (False) fmt : string (default "{:.2f}") A formatting specification for the bin edges of the classes in the legend. For example, to have no decimals: ``{"fmt": "{:.0f}"}``. Applies if ``colorbar=False``. labels : list-like A list of legend labels to override the auto-generated labels. Needs to have the same number of elements as the number of classes (`k`). Applies if ``colorbar=False``. interval : boolean (default False) An option to control brackets from mapclassify legend. If True, open/closed interval brackets are shown in the legend. Applies if ``colorbar=False``. max_labels : int, default 10 Maximum number of colorbar tick labels (requires branca>=0.5.0) **kwargs : dict Additional options to be passed on to the folium object. Returns ------- m : folium.folium.Map folium :class:`~folium.folium.Map` instance Examples -------- >>> df = geopandas.read_file(geopandas.datasets.get_path("naturalearth_lowres")) >>> df.head(2) # doctest: +SKIP pop_est continent name iso_a3 \ gdp_md_est geometry 0 920938 Oceania Fiji FJI 8374.0 MULTIPOLY\ GON (((180.00000 -16.06713, 180.00000... 1 53950935 Africa Tanzania TZA 150600.0 POLYGON (\ (33.90371 -0.95000, 34.07262 -1.05982... >>> df.explore("pop_est", cmap="Blues") # doctest: +SKIP """ try: import branca as bc import folium import matplotlib.cm as cm import matplotlib.colors as colors import matplotlib.pyplot as plt from mapclassify import classify except (ImportError, ModuleNotFoundError): raise ImportError( "The 'folium', 'matplotlib' and 'mapclassify' packages are required for " "'explore()'. You can install them using " "'conda install -c conda-forge folium matplotlib mapclassify' " "or 'pip install folium matplotlib mapclassify'.") # xyservices is an optional dependency try: import xyzservices HAS_XYZSERVICES = True except (ImportError, ModuleNotFoundError): HAS_XYZSERVICES = False gdf = df.copy() # convert LinearRing to LineString rings_mask = df.geom_type == "LinearRing" if rings_mask.any(): gdf.geometry[rings_mask] = gdf.geometry[rings_mask].apply( lambda g: LineString(g)) if gdf.crs is None: kwargs["crs"] = "Simple" tiles = None elif not gdf.crs.equals(4326): gdf = gdf.to_crs(4326) # create folium.Map object if m is None: # Get bounds to specify location and map extent bounds = gdf.total_bounds location = kwargs.pop("location", None) if location is None: x = mean([bounds[0], bounds[2]]) y = mean([bounds[1], bounds[3]]) location = (y, x) if "zoom_start" in kwargs.keys(): fit = False else: fit = True else: fit = False # get a subset of kwargs to be passed to folium.Map map_kwds = {i: kwargs[i] for i in kwargs.keys() if i in _MAP_KWARGS} if HAS_XYZSERVICES: # match provider name string to xyzservices.TileProvider if isinstance(tiles, str): try: tiles = xyzservices.providers.query_name(tiles) except ValueError: pass if isinstance(tiles, xyzservices.TileProvider): attr = attr if attr else tiles.html_attribution map_kwds["min_zoom"] = tiles.get("min_zoom", 0) map_kwds["max_zoom"] = tiles.get("max_zoom", 18) tiles = tiles.build_url(scale_factor="{r}") m = folium.Map( location=location, control_scale=control_scale, tiles=tiles, attr=attr, width=width, height=height, **map_kwds, ) # fit bounds to get a proper zoom level if fit: m.fit_bounds([[bounds[1], bounds[0]], [bounds[3], bounds[2]]]) for map_kwd in _MAP_KWARGS: kwargs.pop(map_kwd, None) nan_idx = None if column is not None: if pd.api.types.is_list_like(column): if len(column) != gdf.shape[0]: raise ValueError( "The GeoDataFrame and given column have different number of rows." ) else: column_name = "__plottable_column" gdf[column_name] = column column = column_name elif pd.api.types.is_categorical_dtype(gdf[column]): if categories is not None: raise ValueError( "Cannot specify 'categories' when column has categorical dtype" ) categorical = True elif gdf[column].dtype is np.dtype("O") or categories: categorical = True nan_idx = pd.isna(gdf[column]) if categorical: cat = pd.Categorical(gdf[column][~nan_idx], categories=categories) N = len(cat.categories) cmap = cmap if cmap else "tab20" # colormap exists in matplotlib if cmap in plt.colormaps(): color = np.apply_along_axis(colors.to_hex, 1, cm.get_cmap(cmap, N)(cat.codes)) legend_colors = np.apply_along_axis( colors.to_hex, 1, cm.get_cmap(cmap, N)(range(N))) # colormap is matplotlib.Colormap elif isinstance(cmap, colors.Colormap): color = np.apply_along_axis(colors.to_hex, 1, cmap(cat.codes)) legend_colors = np.apply_along_axis(colors.to_hex, 1, cmap(range(N))) # custom list of colors elif pd.api.types.is_list_like(cmap): if N > len(cmap): cmap = cmap * (N // len(cmap) + 1) color = np.take(cmap, cat.codes) legend_colors = np.take(cmap, range(N)) else: raise ValueError( "'cmap' is invalid. For categorical plots, pass either valid " "named matplotlib colormap or a list-like of colors.") elif callable(cmap): # List of colors based on Branca colormaps or self-defined functions color = list(map(lambda x: cmap(x), df[column])) else: vmin = gdf[column].min() if not vmin else vmin vmax = gdf[column].max() if not vmax else vmax # get bins if scheme is not None: if classification_kwds is None: classification_kwds = {} if "k" not in classification_kwds: classification_kwds["k"] = k binning = classify(np.asarray(gdf[column][~nan_idx]), scheme, **classification_kwds) color = np.apply_along_axis(colors.to_hex, 1, cm.get_cmap(cmap, k)(binning.yb)) else: bins = np.linspace(vmin, vmax, 257)[1:] binning = classify(np.asarray(gdf[column][~nan_idx]), "UserDefined", bins=bins) color = np.apply_along_axis(colors.to_hex, 1, cm.get_cmap(cmap, 256)(binning.yb)) # set default style if "fillOpacity" not in style_kwds: style_kwds["fillOpacity"] = 0.5 if "weight" not in style_kwds: style_kwds["weight"] = 2 # specify color if color is not None: if (isinstance(color, str) and isinstance(gdf, geopandas.GeoDataFrame) and color in gdf.columns): # use existing column def _style_color(x): return { "fillColor": x["properties"][color], **style_kwds, } style_function = _style_color else: # assign new column if isinstance(gdf, geopandas.GeoSeries): gdf = geopandas.GeoDataFrame(geometry=gdf) if nan_idx is not None and nan_idx.any(): nan_color = missing_kwds.pop("color", None) gdf["__folium_color"] = nan_color gdf.loc[~nan_idx, "__folium_color"] = color else: gdf["__folium_color"] = color stroke_color = style_kwds.pop("color", None) if not stroke_color: def _style_column(x): return { "fillColor": x["properties"]["__folium_color"], "color": x["properties"]["__folium_color"], **style_kwds, } style_function = _style_column else: def _style_stroke(x): return { "fillColor": x["properties"]["__folium_color"], "color": stroke_color, **style_kwds, } style_function = _style_stroke else: # use folium default def _style_default(x): return {**style_kwds} style_function = _style_default if highlight: if "fillOpacity" not in highlight_kwds: highlight_kwds["fillOpacity"] = 0.75 def _style_highlight(x): return {**highlight_kwds} highlight_function = _style_highlight else: highlight_function = None # define default for points if marker_type is None: marker_type = "circle_marker" marker = marker_type if isinstance(marker_type, str): if marker_type == "marker": marker = folium.Marker(**marker_kwds) elif marker_type == "circle": marker = folium.Circle(**marker_kwds) elif marker_type == "circle_marker": marker_kwds["radius"] = marker_kwds.get("radius", 2) marker_kwds["fill"] = marker_kwds.get("fill", True) marker = folium.CircleMarker(**marker_kwds) else: raise ValueError( "Only 'marker', 'circle', and 'circle_marker' are " "supported as marker values") # remove additional geometries if isinstance(gdf, geopandas.GeoDataFrame): non_active_geoms = [ name for name, val in (gdf.dtypes == "geometry").items() if val and name != gdf.geometry.name ] gdf = gdf.drop(columns=non_active_geoms) # preprare tooltip and popup if isinstance(gdf, geopandas.GeoDataFrame): # add named index to the tooltip if gdf.index.name is not None: gdf = gdf.reset_index() # specify fields to show in the tooltip tooltip = _tooltip_popup("tooltip", tooltip, gdf, **tooltip_kwds) popup = _tooltip_popup("popup", popup, gdf, **popup_kwds) else: tooltip = None popup = None # add dataframe to map folium.GeoJson( gdf.__geo_interface__, tooltip=tooltip, popup=popup, marker=marker, style_function=style_function, highlight_function=highlight_function, **kwargs, ).add_to(m) if legend: # NOTE: overlaps will be resolved in branca #88 caption = column if not column == "__plottable_column" else "" caption = legend_kwds.pop("caption", caption) if categorical: categories = cat.categories.to_list() legend_colors = legend_colors.tolist() if nan_idx.any() and nan_color: categories.append(missing_kwds.pop("label", "NaN")) legend_colors.append(nan_color) _categorical_legend(m, caption, categories, legend_colors) elif column is not None: cbar = legend_kwds.pop("colorbar", True) colormap_kwds = {} if "max_labels" in legend_kwds: colormap_kwds["max_labels"] = legend_kwds.pop("max_labels") if scheme: cb_colors = np.apply_along_axis( colors.to_hex, 1, cm.get_cmap(cmap, binning.k)(range(binning.k))) if cbar: if legend_kwds.pop("scale", True): index = [vmin] + binning.bins.tolist() else: index = None colorbar = bc.colormap.StepColormap( cb_colors, vmin=vmin, vmax=vmax, caption=caption, index=index, **colormap_kwds, ) else: fmt = legend_kwds.pop("fmt", "{:.2f}") if "labels" in legend_kwds: categories = legend_kwds["labels"] else: categories = binning.get_legend_classes(fmt) show_interval = legend_kwds.pop("interval", False) if not show_interval: categories = [c[1:-1] for c in categories] if nan_idx.any() and nan_color: categories.append(missing_kwds.pop("label", "NaN")) cb_colors = np.append(cb_colors, nan_color) _categorical_legend(m, caption, categories, cb_colors) else: if isinstance(cmap, bc.colormap.ColorMap): colorbar = cmap else: mp_cmap = cm.get_cmap(cmap) cb_colors = np.apply_along_axis(colors.to_hex, 1, mp_cmap(range(mp_cmap.N))) # linear legend if mp_cmap.N > 20: colorbar = bc.colormap.LinearColormap( cb_colors, vmin=vmin, vmax=vmax, caption=caption, **colormap_kwds, ) # steps else: colorbar = bc.colormap.StepColormap( cb_colors, vmin=vmin, vmax=vmax, caption=caption, **colormap_kwds, ) if cbar: if nan_idx.any() and nan_color: _categorical_legend(m, "", [missing_kwds.pop("label", "NaN")], [nan_color]) m.add_child(colorbar) return m
def __init__( self, gdf, values, spatial_weights, unique_id, binning="HeadTailBreaks", categorical=False, categories=None, verbose=True, **classification_kwds, ): if not categorical: try: from mapclassify import classify except ImportError: raise ImportError( "The 'mapclassify >= 2.4.2` package is required.") self.gdf = gdf self.sw = spatial_weights self.id = gdf[unique_id] self.binning = binning self.categorical = categorical self.categories = categories self.classification_kwds = classification_kwds data = gdf.copy() if values is not None: if not isinstance(values, str): data["mm_v"] = values values = "mm_v" self.values = data[values] data = data.set_index(unique_id)[values] if not categories: categories = data.unique() if not categorical: self.bins = classify(data, scheme=binning, **classification_kwds).bins else: self.bins = categories results_list = [] for index in tqdm(data.index, total=data.shape[0], disable=not verbose): if index in spatial_weights.neighbors.keys(): neighbours = [index] neighbours += spatial_weights.neighbors[index] values_list = data.loc[neighbours] results_list.append( shannon_diversity( values_list, self.bins, categorical=categorical, categories=categories, )) else: results_list.append(np.nan) self.series = pd.Series(results_list, index=gdf.index)
def view( df, column=None, cmap=None, color=None, m=None, tiles="OpenStreetMap", attr=None, tooltip=False, popup=False, categorical=False, legend=None, scheme=None, k=5, vmin=None, vmax=None, width="100%", height="100%", categories=None, classification_kwds=None, control_scale=True, crs="EPSG3857", marker_type=None, marker_kwds={}, style_kwds={}, missing_kwds={}, tooltip_kwds={}, popup_kwds={}, legend_kwds={}, **kwargs, ): """Interactive map based on GeoPandas and folium/leaflet.js Generate an interactive leaflet map based on GeoDataFrame or GeoSeries Parameters ---------- df : GeoDataFrame The GeoDataFrame to be plotted. column : str, np.array, pd.Series (default None) The name of the dataframe column, np.array, or pd.Series to be plotted. If np.array or pd.Series are used then it must have same length as dataframe. cmap : str (default None) For non-categorical maps, the name of a colormap recognized by colorbrewer. Available are: ``["BuGn", "BuPu", "GnBu", "OrRd", "PuBu", "PuBuGn", "PuRd", "RdPu", "YlGn", "YlGnBu", "YlOrBr", "YlOrRd"]`` For categorical maps, the name of a matplotlib colormap or a list-like of colors. color : str, array-like (default None) Named color or array-like of colors (named or hex) m : folium.Map (default None) Existing map instance on which to draw the plot tiles : str (default 'OpenStreetMap') Map tileset to use. Can choose from this list of built-in tiles: ``["OpenStreetMap", "Stamen Terrain", “Stamen Toner", “Stamen Watercolor" "CartoDB positron", “CartoDB dark_matter"]`` You can pass a custom tileset to Folium by passing a Leaflet-style URL to the tiles parameter: http://{s}.yourtiles.com/{z}/{x}/{y}.png. You can find a list of free tile providers here: http://leaflet-extras.github.io/leaflet-providers/preview/. Be sure to check their terms and conditions and to provide attribution with the attr keyword. attr : str (default None) Map tile attribution; only required if passing custom tile URL. tooltip : bool, str, int, list (default False) Display GeoDataFrame attributes when hovering over the object. Integer specifies first n columns to be included, ``True`` includes all columns. ``False`` removes tooltip. Pass string or list of strings to specify a column(s). Defaults to ``False``. popup : bool, str, int, list (default False) Input GeoDataFrame attributes for object displayed when clicking. Integer specifies first n columns to be included, ``True`` includes all columns. ``False`` removes tooltip. Pass string or list of strings to specify a column(s). Defaults to ``False``. categorical : bool (default False) If False, cmap will reflect numerical values of the column being plotted. For non-numerical columns, this will be set to True. legend : bool (default None) Plot a categorical legend in categorical plots. Ignored if no `column` is given, or if `color` is given. scheme : str (default None) Name of a choropleth classification scheme (requires mapclassify). A mapclassify.MapClassifier object will be used under the hood. Supported are all schemes provided by mapclassify (e.g. 'BoxPlot', 'EqualInterval', 'FisherJenks', 'FisherJenksSampled', 'HeadTailBreaks', 'JenksCaspall', 'JenksCaspallForced', 'JenksCaspallSampled', 'MaxP', 'MaximumBreaks', 'NaturalBreaks', 'Quantiles', 'Percentiles', 'StdMean', 'UserDefined'). Arguments can be passed in classification_kwds. k : int (default 5) Number of classes vmin : None or float (default None) Minimum value of cmap. If None, the minimum data value in the column to be plotted is used. Cannot be higher than minimum data value. vmax : None or float (default None) Maximum value of cmap. If None, the maximum data value in the column to be plotted is used. Cannot be lower than maximum data value. width : pixel int or percentage string (default: '100%') Width of the folium.Map. If the argument m is given explicitly, width is ignored. height : pixel int or percentage string (default: '100%') Height of the folium.Map. If the argument m is given explicitly, height is ignored. categories : list-like Ordered list-like object of categories to be used for categorical plot. classification_kwds : dict (default None) Keyword arguments to pass to mapclassify control_scale : bool, (default True) Whether to add a control scale on the map. crs : str (default "EPSG3857") Defines coordinate reference systems for projecting geographical points into pixel (screen) coordinates and back. You can use Leaflet’s values : * ``'EPSG3857'`` : The most common CRS for online maps, used by almost all free and commercial tile providers. Uses Spherical Mercator projection. Set in by default in Map’s crs option. * ``'EPSG4326'`` : A common CRS among GIS enthusiasts. Uses simple Equirectangular projection. * ``'EPSG3395'`` : arely used by some commercial tile providers. Uses Elliptical Mercator projection. * ``'Simple'`` : A simple CRS that maps longitude and latitude into x and y directly. May be used for maps of flat surfaces (e.g. game maps). Note that the CRS of tiles needs to match ``crs``. marker_type : str, folium.Circle, folium.CircleMarker, folium.Marker (default None) Allowed strings are ('marker', 'circle', 'circle_marker') marker_kwds: dict (default {}) Additional keywords to be passed to the selected marker_type style_kwds : dict (default {}) Additional style to be passed to folium style_function tooltip_kwds : dict (default {}) Additional keywords to be passed to folium.features.GeoJsonTooltip, e.g. ``aliases``, ``labels``, or ``sticky``. See the folium documentation for details: https://python-visualization.github.io/folium/modules.html#folium.features.GeoJsonTooltip popup_kwds : dict (default {}) Additional keywords to be passed to folium.features.GeoJsonPopup, e.g. ``aliases`` or ``labels``. See the folium documentation for details: https://python-visualization.github.io/folium/modules.html#folium.features.GeoJsonPopup **kwargs : dict Additional options to be passed on to the folium.Map, folium.GeoJson or folium.Choropleth. Returns ------- m : folium.Map Folium map instance """ gdf = df.copy() if gdf.crs is None: crs = "Simple" tiles = None elif not gdf.crs.equals(4326): gdf = gdf.to_crs(4326) # create folium.Map object if m is None: # Get bounds to specify location and map extent bounds = gdf.total_bounds location = kwargs.pop("location", None) if location is None: x = mean([bounds[0], bounds[2]]) y = mean([bounds[1], bounds[3]]) location = (y, x) # get a subset of kwargs to be passed to folium.Map map_kwds = {i: kwargs[i] for i in kwargs.keys() if i in _MAP_KWARGS} m = folium.Map( location=location, control_scale=control_scale, tiles=tiles, attr=attr, width=width, height=height, crs=crs, **map_kwds, ) for map_kwd in _MAP_KWARGS: kwargs.pop(map_kwd, None) nan_idx = None if column is not None: if pd.api.types.is_list_like(column): if len(column) != gdf.shape[0]: raise ValueError( "The GeoDataframe and given column have different number of rows." ) else: column_name = "__plottable_column" gdf[column_name] = column column = column_name elif pd.api.types.is_categorical_dtype(gdf[column]): if categories is not None: raise ValueError( "Cannot specify 'categories' when column has categorical dtype" ) categorical = True elif gdf[column].dtype is np.dtype("O") or categories: categorical = True nan_idx = pd.isna(gdf[column]) if categorical: cat = pd.Categorical(gdf[column][~nan_idx], categories=categories) N = len(cat.categories) cmap = cmap if cmap else "tab20" # colormap exists in matplotlib if cmap in plt.colormaps(): color = np.apply_along_axis( colors.to_hex, 1, cm.get_cmap(cmap, N)(cat.codes) ) legend_colors = np.apply_along_axis( colors.to_hex, 1, cm.get_cmap(cmap, N)(range(N)) ) # custom list of colors elif pd.api.types.is_list_like(cmap): if N > len(cmap): cmap = cmap * (N // len(cmap) + 1) color = np.take(cmap, cat.codes) legend_colors = np.take(cmap, range(N)) else: raise ValueError( "'cmap' is invalid. For categorical plots, pass either valid " "named matplotlib colormap or a list-like of colors." ) else: vmin = gdf[column].min() if not vmin else vmin vmax = gdf[column].max() if not vmax else vmax if vmin > gdf[column].min(): warn( "'vmin' cannot be higher than minimum value. Setting vmin to minimum.", UserWarning, stacklevel=3, ) vmin = gdf[column].min() if vmax < gdf[column].max(): warn( "'vmax' cannot be lower than maximum value. Setting vmax to maximum.", UserWarning, stacklevel=3, ) vmax = gdf[column].max() # get bins if scheme is not None: if classification_kwds is None: classification_kwds = {} if "k" not in classification_kwds: classification_kwds["k"] = k binning = mapclassify.classify( np.asarray(gdf[column][~nan_idx]), scheme, **classification_kwds ) color = np.apply_along_axis( colors.to_hex, 1, cm.get_cmap(cmap, k)(binning.yb) ) else: bins = np.linspace(vmin, vmax, 257)[1:] binning = mapclassify.classify( np.asarray(gdf[column][~nan_idx]), "UserDefined", bins=bins ) color = np.apply_along_axis( colors.to_hex, 1, cm.get_cmap(cmap, 256)(binning.yb) ) # we cannot color default 'marker' if marker_type is None: marker_type = "circle" # set default style if "fillOpacity" not in style_kwds: style_kwds["fillOpacity"] = 0.5 if "weight" not in style_kwds: style_kwds["weight"] = 1 # specify color if color is not None: if ( isinstance(color, str) and isinstance(gdf, gpd.GeoDataFrame) and color in gdf.columns ): # use existing column style_function = lambda x: {"fillColor": x["properties"][color], **style_kwds} else: # assign new column if isinstance(gdf, gpd.GeoSeries): gdf = gpd.GeoDataFrame(geometry=gdf) if nan_idx is not None and nan_idx.any(): nan_color = missing_kwds.pop("color", None) gdf["__folium_color"] = nan_color gdf.loc[~nan_idx, "__folium_color"] = color else: gdf["__folium_color"] = color stroke_color = style_kwds.pop('color', None) if not stroke_color: style_function = lambda x: { "fillColor": x["properties"]["__folium_color"], "color": x["properties"]["__folium_color"], **style_kwds, } else: style_function = lambda x: { "fillColor": x["properties"]["__folium_color"], "color": stroke_color, **style_kwds, } else: # use folium default style_function = lambda x: {**style_kwds} marker = marker_type if marker_type is not None and isinstance(marker_type, str): if marker_type == "marker": marker = folium.Marker(**marker_kwds) elif marker_type == "circle": marker = folium.Circle(**marker_kwds) elif marker_type == "circle_marker": marker = folium.CircleMarker(**marker_kwds) else: raise ValueError( "Only 'marker', 'circle', and 'circle_marker' are supported as marker values" ) # preprare tooltip and popup if isinstance(gdf, gpd.GeoDataFrame): # specify fields to show in the tooltip tooltip = _tooltip_popup("tooltip", tooltip, gdf, **tooltip_kwds) popup = _tooltip_popup("popup", popup, gdf, **popup_kwds) else: tooltip = None popup = None # add dataframe to map folium.GeoJson( gdf.__geo_interface__, tooltip=tooltip, popup=popup, marker=marker, style_function=style_function, **kwargs, ).add_to(m) # fit bounds to get a proper zoom level m.fit_bounds([[bounds[1], bounds[0]], [bounds[3], bounds[2]]]) if legend: # NOTE: overlaps should be resolved in branca https://github.com/python-visualization/branca/issues/88 caption = column if not column == "__plottable_column" else "" caption = legend_kwds.pop("caption", caption) if categorical: categories = cat.categories.to_list() legend_colors = legend_colors.tolist() if nan_idx.any() and nan_color: categories.append(missing_kwds.pop("label", "NaN")) legend_colors.append(nan_color) _categorical_legend(m, caption, categories, legend_colors) elif column is not None: if scheme: cb_colors = np.apply_along_axis( colors.to_hex, 1, cm.get_cmap(cmap, binning.k)(range(binning.k)) ) if legend_kwds.pop("scale", True): index = [vmin] + binning.bins.tolist() else: index = None colorbar = bc.colormap.StepColormap( cb_colors, vmin=vmin, vmax=vmax, caption=caption, index=index ) else: mp_cmap = cm.get_cmap(cmap) cb_colors = np.apply_along_axis( colors.to_hex, 1, mp_cmap(range(mp_cmap.N)) ) # linear legend if mp_cmap.N > 20: colorbar = bc.colormap.LinearColormap( cb_colors, vmin=vmin, vmax=vmax, caption=caption ) # steps else: colorbar = bc.colormap.StepColormap( cb_colors, vmin=vmin, vmax=vmax, caption=caption ) if nan_idx.any() and nan_color: _categorical_legend( m, "", [missing_kwds.pop("label", "NaN")], [nan_color] ) m.add_child(colorbar) return m
def view( df, column=None, cmap=None, color=None, m=None, tiles="OpenStreetMap", attr=None, tooltip=True, popup=False, highlight=True, categorical=False, legend=True, scheme=None, k=5, vmin=None, vmax=None, width="100%", height="100%", categories=None, classification_kwds=None, control_scale=True, marker_type=None, marker_kwds={}, style_kwds={}, highlight_kwds={}, missing_kwds={}, tooltip_kwds={}, popup_kwds={}, legend_kwds={}, **kwargs, ): """Interactive map based on GeoPandas and folium/leaflet.js Generate an interactive leaflet map based on GeoDataFrame or GeoSeries Parameters ---------- df : GeoDataFrame The GeoDataFrame to be plotted. column : str, np.array, pd.Series (default None) The name of the dataframe column, np.array, or pd.Series to be plotted. If np.array or pd.Series are used then it must have same length as dataframe. cmap : str, matplotlib.Colormap, branca.colormap, self-defined function fun(column)->str (default None) The name of a colormap recognized by matplotlib, a list-like of colors, matplotlib.Colormap, a branca colormap or function that returns a named color or hex based on the column value, e.g.: def my_colormap(value): # scalar value defined in 'column' if value > 1: return "green" return "red" color : str, array-like (default None) Named color or a list-like of colors (named or hex). m : folium.Map (default None) Existing map instance on which to draw the plot. tiles : str, contextily.providers.TileProvider (default 'OpenStreetMap') Map tileset to use. Can choose from this list of built-in tiles or pass ``contextily.providers.TileProvider``: ``["OpenStreetMap", "Stamen Terrain", “Stamen Toner", “Stamen Watercolor" "CartoDB positron", “CartoDB dark_matter"]`` You can pass a custom tileset to Folium by passing a Leaflet-style URL to the tiles parameter: http://{s}.yourtiles.com/{z}/{x}/{y}.png. You can find a list of free tile providers here: http://leaflet-extras.github.io/leaflet-providers/preview/. Be sure to check their terms and conditions and to provide attribution with the attr keyword. attr : str (default None) Map tile attribution; only required if passing custom tile URL. tooltip : bool, str, int, list (default True) Display GeoDataFrame attributes when hovering over the object. Integer specifies first n columns to be included, ``True`` includes all columns. ``False`` removes tooltip. Pass string or list of strings to specify a column(s). Defaults to ``True``. popup : bool, str, int, list (default False) Input GeoDataFrame attributes for object displayed when clicking. Integer specifies first n columns to be included, ``True`` includes all columns. ``False`` removes tooltip. Pass string or list of strings to specify a column(s). Defaults to ``False``. highlight : bool (default True) Enable highlight functionality when hovering over a geometry. categorical : bool (default False) If False, cmap will reflect numerical values of the column being plotted. For non-numerical columns, this will be set to True. legend : bool (default True) Plot a legend in choropleth plots. Ignored if no `column` is given. scheme : str (default None) Name of a choropleth classification scheme (requires mapclassify). A mapclassify.MapClassifier object will be used under the hood. Supported are all schemes provided by mapclassify (e.g. 'BoxPlot', 'EqualInterval', 'FisherJenks', 'FisherJenksSampled', 'HeadTailBreaks', 'JenksCaspall', 'JenksCaspallForced', 'JenksCaspallSampled', 'MaxP', 'MaximumBreaks', 'NaturalBreaks', 'Quantiles', 'Percentiles', 'StdMean', 'UserDefined'). Arguments can be passed in classification_kwds. k : int (default 5) Number of classes vmin : None or float (default None) Minimum value of cmap. If None, the minimum data value in the column to be plotted is used. Cannot be higher than minimum data value. vmax : None or float (default None) Maximum value of cmap. If None, the maximum data value in the column to be plotted is used. Cannot be lower than maximum data value. width : pixel int or percentage string (default: '100%') Width of the folium.Map. If the argument m is given explicitly, width is ignored. height : pixel int or percentage string (default: '100%') Height of the folium.Map. If the argument m is given explicitly, height is ignored. categories : list-like Ordered list-like object of categories to be used for categorical plot. classification_kwds : dict (default None) Keyword arguments to pass to mapclassify control_scale : bool, (default True) Whether to add a control scale on the map. marker_type : str, folium.Circle, folium.CircleMarker, folium.Marker (default None) Allowed string options are ('marker', 'circle', 'circle_marker') marker_kwds: dict (default {}) Additional keywords to be passed to the selected ``marker_type``, e.g.: radius : float Radius of the circle, in meters (for ``'circle'``) or pixels (for ``circle_marker``). icon : folium.map.Icon the Icon object to use to render the marker. See https://python-visualization.github.io/folium/modules.html#folium.map.Icon. draggable : bool (default False) Set to True to be able to drag the marker around the map. style_kwds : dict (default {}) Additional style to be passed to folium style_function: stroke : bool (default True) Whether to draw stroke along the path. Set it to False to disable borders on polygons or circles. color : str Stroke color weight : int Stroke width in pixels opacity : float (default 1.0) Stroke opacity fill : boolean (default True) Whether to fill the path with color. Set it to False to disable filling on polygons or circles. fillColor : str Fill color. Defaults to the value of the color option fillOpacity : float (default 0.5) Fill opacity. Plus all supported by folium.Path object. See ``folium.vector_layers.path_options()`` for the Path options. highlight_kwds : dict (default {}) Style to be passed to folium highlight_function. Uses the same keywords as ``style_kwds``. When empty, defaults to ``{"fillOpacity": 0.75}``. tooltip_kwds : dict (default {}) Additional keywords to be passed to folium.features.GeoJsonTooltip, e.g. ``aliases``, ``labels``, or ``sticky``. See the folium documentation for details: https://python-visualization.github.io/folium/modules.html#folium.features.GeoJsonTooltip popup_kwds : dict (default {}) Additional keywords to be passed to folium.features.GeoJsonPopup, e.g. ``aliases`` or ``labels``. See the folium documentation for details: https://python-visualization.github.io/folium/modules.html#folium.features.GeoJsonPopup legend_kwds : dict (default {}) Additional keywords to be passed to the legend. Currently supported customisation: caption : string Custom caption of the legend. Defaults to the column name. Additional accepted keywords when `scheme` is specified: colorbar : bool (default True) An option to control the style of the legend. If True, continuous colorbar will be used. If False, categorical legend will be used for bins. scale : bool (default True) Scale bins along the colorbar axis according to the bin edges (True) or use the equal length for each bin (False) fmt : string (default "{:.2f}") A formatting specification for the bin edges of the classes in the legend. For example, to have no decimals: ``{"fmt": "{:.0f}"}``. Applies if ``colorbar=False``. labels : list-like A list of legend labels to override the auto-generated labels. Needs to have the same number of elements as the number of classes (`k`). Applies if ``colorbar=False``. interval : boolean (default False) An option to control brackets from mapclassify legend. If True, open/closed interval brackets are shown in the legend. Applies if ``colorbar=False``. **kwargs : dict Additional options to be passed on to the folium.Map or folium.GeoJson. Returns ------- m : folium.Map Folium map instance """ gdf = df.copy() # convert LinearRing to LineString rings_mask = df.geom_type == "LinearRing" if rings_mask.any(): gdf.geometry[rings_mask] = gdf.geometry[rings_mask].apply( lambda g: LineString(g) ) if gdf.crs is None: crs = "Simple" tiles = None elif not gdf.crs.equals(4326): gdf = gdf.to_crs(4326) # create folium.Map object if m is None: # Get bounds to specify location and map extent bounds = gdf.total_bounds location = kwargs.pop("location", None) if location is None: x = mean([bounds[0], bounds[2]]) y = mean([bounds[1], bounds[3]]) location = (y, x) if "zoom_start" in kwargs.keys(): fit = False else: fit = True else: fit = False # get a subset of kwargs to be passed to folium.Map map_kwds = {i: kwargs[i] for i in kwargs.keys() if i in _MAP_KWARGS} # contextily.providers object if hasattr(tiles, "url") and hasattr(tiles, "attribution"): attr = attr if attr else tiles["attribution"] map_kwds["min_zoom"] = tiles.get("min_zoom", 0) map_kwds["max_zoom"] = tiles.get("max_zoom", 18) tiles = tiles["url"].format( x="{x}", y="{y}", z="{z}", s="{s}", r=tiles.get("r", ""), **tiles ) m = folium.Map( location=location, control_scale=control_scale, tiles=tiles, attr=attr, width=width, height=height, **map_kwds, ) # fit bounds to get a proper zoom level if fit: m.fit_bounds([[bounds[1], bounds[0]], [bounds[3], bounds[2]]]) for map_kwd in _MAP_KWARGS: kwargs.pop(map_kwd, None) nan_idx = None if column is not None: if pd.api.types.is_list_like(column): if len(column) != gdf.shape[0]: raise ValueError( "The GeoDataframe and given column have different number of rows." ) else: column_name = "__plottable_column" gdf[column_name] = column column = column_name elif pd.api.types.is_categorical_dtype(gdf[column]): if categories is not None: raise ValueError( "Cannot specify 'categories' when column has categorical dtype" ) categorical = True elif gdf[column].dtype is np.dtype("O") or categories: categorical = True nan_idx = pd.isna(gdf[column]) if categorical: cat = pd.Categorical(gdf[column][~nan_idx], categories=categories) N = len(cat.categories) cmap = cmap if cmap else "tab20" # colormap exists in matplotlib if cmap in plt.colormaps(): color = np.apply_along_axis( colors.to_hex, 1, cm.get_cmap(cmap, N)(cat.codes) ) legend_colors = np.apply_along_axis( colors.to_hex, 1, cm.get_cmap(cmap, N)(range(N)) ) # colormap is matplotlib.Colormap elif isinstance(cmap, colors.Colormap): color = np.apply_along_axis(colors.to_hex, 1, cmap(cat.codes)) legend_colors = np.apply_along_axis(colors.to_hex, 1, cmap(range(N))) # custom list of colors elif pd.api.types.is_list_like(cmap): if N > len(cmap): cmap = cmap * (N // len(cmap) + 1) color = np.take(cmap, cat.codes) legend_colors = np.take(cmap, range(N)) else: raise ValueError( "'cmap' is invalid. For categorical plots, pass either valid " "named matplotlib colormap or a list-like of colors." ) elif callable(cmap): # List of colors based on Branca colormaps or self-defined functions color = list(map(lambda x: cmap(x), df[column])) else: vmin = gdf[column].min() if not vmin else vmin vmax = gdf[column].max() if not vmax else vmax if vmin > gdf[column].min(): warn( "'vmin' cannot be higher than minimum value. Setting vmin to minimum.", UserWarning, stacklevel=3, ) vmin = gdf[column].min() if vmax < gdf[column].max(): warn( "'vmax' cannot be lower than maximum value. Setting vmax to maximum.", UserWarning, stacklevel=3, ) vmax = gdf[column].max() # get bins if scheme is not None: if classification_kwds is None: classification_kwds = {} if "k" not in classification_kwds: classification_kwds["k"] = k binning = mapclassify.classify( np.asarray(gdf[column][~nan_idx]), scheme, **classification_kwds ) color = np.apply_along_axis( colors.to_hex, 1, cm.get_cmap(cmap, k)(binning.yb) ) else: bins = np.linspace(vmin, vmax, 257)[1:] binning = mapclassify.classify( np.asarray(gdf[column][~nan_idx]), "UserDefined", bins=bins ) color = np.apply_along_axis( colors.to_hex, 1, cm.get_cmap(cmap, 256)(binning.yb) ) # we cannot color default 'marker' if marker_type is None: marker_type = "circle" # set default style if "fillOpacity" not in style_kwds: style_kwds["fillOpacity"] = 0.5 if "weight" not in style_kwds: style_kwds["weight"] = 2 # specify color if color is not None: if ( isinstance(color, str) and isinstance(gdf, gpd.GeoDataFrame) and color in gdf.columns ): # use existing column style_function = lambda x: { "fillColor": x["properties"][color], **style_kwds, } else: # assign new column if isinstance(gdf, gpd.GeoSeries): gdf = gpd.GeoDataFrame(geometry=gdf) if nan_idx is not None and nan_idx.any(): nan_color = missing_kwds.pop("color", None) gdf["__folium_color"] = nan_color gdf.loc[~nan_idx, "__folium_color"] = color else: gdf["__folium_color"] = color stroke_color = style_kwds.pop("color", None) if not stroke_color: style_function = lambda x: { "fillColor": x["properties"]["__folium_color"], "color": x["properties"]["__folium_color"], **style_kwds, } else: style_function = lambda x: { "fillColor": x["properties"]["__folium_color"], "color": stroke_color, **style_kwds, } else: # use folium default style_function = lambda x: {**style_kwds} if highlight: if not "fillOpacity" in highlight_kwds: highlight_kwds["fillOpacity"] = 0.75 highlight_function = lambda x: {**highlight_kwds} else: highlight_function = None marker = marker_type if marker_type is not None and isinstance(marker_type, str): if marker_type == "marker": marker = folium.Marker(**marker_kwds) elif marker_type == "circle": marker = folium.Circle(**marker_kwds) elif marker_type == "circle_marker": marker = folium.CircleMarker(**marker_kwds) else: raise ValueError( "Only 'marker', 'circle', and 'circle_marker' are supported as marker values" ) # preprare tooltip and popup if isinstance(gdf, gpd.GeoDataFrame): # specify fields to show in the tooltip tooltip = _tooltip_popup("tooltip", tooltip, gdf, **tooltip_kwds) popup = _tooltip_popup("popup", popup, gdf, **popup_kwds) else: tooltip = None popup = None # add dataframe to map folium.GeoJson( gdf.__geo_interface__, tooltip=tooltip, popup=popup, marker=marker, style_function=style_function, highlight_function=highlight_function, **kwargs, ).add_to(m) if legend: # NOTE: overlaps should be resolved in branca https://github.com/python-visualization/branca/issues/88 caption = column if not column == "__plottable_column" else "" caption = legend_kwds.pop("caption", caption) if categorical: categories = cat.categories.to_list() legend_colors = legend_colors.tolist() if nan_idx.any() and nan_color: categories.append(missing_kwds.pop("label", "NaN")) legend_colors.append(nan_color) _categorical_legend(m, caption, categories, legend_colors) elif column is not None: cbar = legend_kwds.pop("colorbar", True) if scheme: cb_colors = np.apply_along_axis( colors.to_hex, 1, cm.get_cmap(cmap, binning.k)(range(binning.k)) ) if cbar: if legend_kwds.pop("scale", True): index = [vmin] + binning.bins.tolist() else: index = None colorbar = bc.colormap.StepColormap( cb_colors, vmin=vmin, vmax=vmax, caption=caption, index=index ) else: fmt = legend_kwds.pop("fmt", "{:.2f}") if "labels" in legend_kwds: categories = legend_kwds["labels"] else: categories = binning.get_legend_classes(fmt) show_interval = legend_kwds.pop("interval", False) if not show_interval: categories = [c[1:-1] for c in categories] if nan_idx.any() and nan_color: categories.append(missing_kwds.pop("label", "NaN")) cb_colors = np.append(cb_colors, nan_color) _categorical_legend(m, caption, categories, cb_colors) else: if isinstance(cmap, bc.colormap.ColorMap): colorbar = cmap else: mp_cmap = cm.get_cmap(cmap) cb_colors = np.apply_along_axis( colors.to_hex, 1, mp_cmap(range(mp_cmap.N)) ) # linear legend if mp_cmap.N > 20: colorbar = bc.colormap.LinearColormap( cb_colors, vmin=vmin, vmax=vmax, caption=caption ) # steps else: colorbar = bc.colormap.StepColormap( cb_colors, vmin=vmin, vmax=vmax, caption=caption ) if cbar: if nan_idx.any() and nan_color: _categorical_legend( m, "", [missing_kwds.pop("label", "NaN")], [nan_color] ) m.add_child(colorbar) return m
def test_classify(): # data link_to_data = examples.get_path('columbus.shp') gdf = gpd.read_file(link_to_data) x = gdf['HOVAL'].values # box_plot a = classify(x, 'box_plot') b = mapclassify.BoxPlot(x) _assertions(a, b) # EqualInterval a = classify(x, "EqualInterval", k=3) b = mapclassify.EqualInterval(x, k=3) _assertions(a, b) # FisherJenks a = classify(x, "FisherJenks", k=3) b = mapclassify.FisherJenks(x, k=3) _assertions(a, b) a= classify(x, "FisherJenksSampled", k=3, pct_sampled=0.5, truncate=False) b = mapclassify.FisherJenksSampled(x, k=3, pct=0.5,truncate=False) _assertions(a, b) # headtail_breaks a = classify(x, 'headtail_breaks') b = mapclassify.HeadTailBreaks(x) _assertions(a, b) # quantiles a = classify(x, 'quantiles',k=3) b = mapclassify.Quantiles(x, k=3) _assertions(a, b) # percentiles a = classify(x, 'percentiles', pct=[25,50,75,100]) b = mapclassify.Percentiles(x, pct=[25,50,75,100]) _assertions(a, b) #JenksCaspall a = classify(x, 'JenksCaspall', k=3) b = mapclassify.JenksCaspall(x, k=3) _assertions(a, b) a = classify(x, 'JenksCaspallForced', k=3) b = mapclassify.JenksCaspallForced(x, k=3) _assertions(a, b) a = classify(x, 'JenksCaspallSampled', pct_sampled=0.5) b = mapclassify.JenksCaspallSampled(x, pct=0.5) _assertions(a, b) # natural_breaks, max_p_classifier a = classify(x, 'natural_breaks') b = mapclassify.NaturalBreaks(x) _assertions(a, b) a = classify(x, 'max_p', k=3, initial=50) b = mapclassify.MaxP(x, k=3, initial=50) _assertions(a, b) # std_mean a = classify(x, 'std_mean', multiples=[-1,-0.5,0.5,1]) b = mapclassify.StdMean(x, multiples=[-1,-0.5,0.5,1]) _assertions(a, b) # user_defined a = classify(x, 'user_defined', bins=[20, max(x)]) b = mapclassify.UserDefined(x, bins=[20, max(x)]) _assertions(a, b)