Thematic Map

Make static maps with geopandas and matplotlib.

class ThematicMap(*gdfs, column=None, bounds=None, title=None, title_position=None, size=25, dark=False, cmap=None, scheme='naturalbreaks', k=5, bins=None, nan_label='Missing', legend_kwargs=None, title_kwargs=None, legend=True, **kwargs)[source]

Bases: Map

Class for making static maps.

Parameters:
  • *gdfs (GeoDataFrame) – One or more GeoDataFrames.

  • column (str | None) – The name of the column to plot.

  • bounds (tuple | None) – Optional bounding box for the map.

  • title (str | None) – Title of the plot.

  • title_position (tuple[float, float] | None) – Title position. Either “center” (default), “left” or “right”.

  • size (int) – Width and height of the plot in inches. Fontsize of title and legend is adjusted accordingly. Defaults to 25.

  • dark (bool) – If False (default), the background will be white and the text black. If True, the background will be black and the text white. When True, the default cmap is “viridis”, and when False, the default is red to purple (RdPu).

  • cmap (str | None) – Colormap of the plot. See: https://matplotlib.org/stable/tutorials/colors/colormaps.html

  • scheme (str) – How to devide numeric values into categories. Defaults to “naturalbreaks”.

  • k (int) – Number of color groups.

  • bins (tuple[float] | None) – For numeric columns. List of numbers that define the maximum value for the color groups.

  • nan_label (str) – Label for missing data.

  • legend_kwargs (dict | None) – dictionary with attributes for the legend. E.g.: title: Legend title. Defaults to the column name. rounding: If positive number, it will round floats to n decimals. If negative, eg. -2, the number 3429 is rounded to 3400. By default, the rounding depends on the column’s maximum value and standard deviation. position: The legend’s x and y position in the plot. By default, it’s decided dynamically by finding the space with most distance to the geometries. To be specified as a tuple of x and y position between 0 and 1. E.g. position=(0.8, 0.2) for a position in the bottom right corner, (0.2, 0.8) for the upper left corner. pretty_labels: Whether to capitalize words in text categories. label_suffix: For numeric columns. The text to put after each number in the legend labels. Defaults to None. label_sep: For numeric columns. Text to put in between the two numbers in each color group in the legend. Defaults to ‘-‘. thousand_sep: For numeric columns. Separator between each thousand for large numbers. Defaults to None, meaning no separator. decimal_mark: For numeric columns. Text to use as decimal point. Defaults to None, meaning ‘.’ (dot) unless ‘thousand_sep’ is ‘.’. In this case, ‘,’ (comma) will be used as decimal mark.

  • **kwargs – Additional attributes for the map. E.g.: title_color (str): Color of the title font. title_fontsize (int): Color of the title font. cmap_start (int): Start position for the color palette. cmap_stop (int): End position for the color palette. facecolor (str): Background color. labelcolor (str): Color for the labels. nan_color: Color for missing data.

  • title_kwargs (dict | None)

  • legend (bool)

Examples:

>>> import sgis as sg
>>> points = sg.random_points(100, loc=1000).pipe(sg.buff, np.random.rand(100) * 100)
>>> points2 = sg.random_points(100, loc=1000).pipe(sg.buff, np.random.rand(100) * 100)

Simple plot with legend and title.

>>> m = sg.ThematicMap(points, points2, column="area", title="Area of random circles")
>>> m.plot()

Plot with custom legend units (label_suffix) and thousand separator. And with rounding set to -2, meaning e.g. 3429 is rounded to 3400. If rounding was set to positive 2, 3429 would be rounded to 3429.00.

>>> m = sg.ThematicMap(
...     points,
...     points2,
...     column="area",
...     title = "Area of random circles",
...     legend_kwargs=dict(
...         rounding=-2,
...         thousand_sep=" ",
...         label_sep="to",
...     ),
... )
>>> m.plot()

With custom bins for the categories, and other customizations.

>>> m = sg.ThematicMap(
...     points,
...     points2,
...     column="area",
...     cmap="Greens",
...     cmap_start=50,
...     cmap_stop=255,
...     nan_label="Missing",
...     title = "Area of random circles",
...     bins = [5000, 10000, 15000, 20000],
...     title_kwargs=dict(
...         loc="left",
...         y=0.93,
...         x=0.025,
...     ),
...     legend_kwargs=dict(
...         thousand_sep=" ",
...         label_sep="to",
...         decimal_mark=".",
...         label_suffix="m2",
...     ),
... )
>>> m.plot()
add_background(gdf, color=None)[source]

Add a GeoDataFrame as a background layer.

Parameters:
  • gdf (GeoDataFrame) – a GeoDataFrame.

  • color (str | None) – Single color. Defaults to gray (shade depends on whether the map facecolor is black or white).

Return type:

ThematicMap

change_cmap(cmap, start=0, stop=256)[source]

Change the color palette of the plot.

Parameters:
Return type:

ThematicMap

property dark: bool

Whether to use dark background and light text colors.

plot(**kwargs)[source]

Creates the final plot.

This method should be run after customising the map, but before saving.

Return type:

None

save(path)[source]

Save figure as image file.

To be run after the plot method.

Parameters:

path (str) – File path.

Return type:

None

property size: int

Size of the image.

property title_fontsize: int

Title fontsize, not to be confused with legend.title_fontsize.

property valid_keywords: set[str]

List all valid keywords for the class initialiser.