Shared geographic plot controls

GeoPlot and DotDensityPlot share controls for outlines, focus, labels, colorbars, and output. Each example below changes one behavior.

from pathlib import Path

import geopandas as gpd
import pandas as pd

from gerrytools.plotting import GeoPlot, LabelOptions

precincts = gpd.read_file(Path("data/ga_2016_precincts.gpkg"))
precincts[["BVAP", "VAP"]] = precincts[["BVAP", "VAP"]].apply(pd.to_numeric)
precincts["BVAP_SHARE"] = precincts["BVAP"].div(precincts["VAP"])

Outlines

add_outline_layer() accepts the base GeoDataFrame, a separate geographic source, or a boolean mask. Supplying dissolve_column= converts finer source units into the boundaries of a larger geography before drawing.

outlines = GeoPlot(precincts)
outlines.add_outline_layer(dissolve_column="CTYNAME")
outlines.show()
../../../../_images/dd40c2b91402f1e34ab522b5d439df8da95dc7f531a562a34f9fc275c2bd3b19.png

Focus

focus_axes() changes the axes limits without removing any layer data. Its default padding is two percent of the selected geometry’s width and height; pad= accepts one value, separate x and y values, or four side-specific values.

metro_mask = precincts["CTYNAME"].isin(["Clayton", "Cobb", "DeKalb", "Fulton", "Gwinnett"])
focused = GeoPlot(precincts)
focused.add_districting_plan_layer("CD")
focused.focus_axes(geometry_mask=metro_mask)
focused.show()
../../../../_images/25180d7a1caed848d05165a0d5258334afd36276bf832a7a9b25c1d620c1a4d1.png

Labels

District and outline layers can calculate one label position per dissolved geography. Named styles cover the common treatments: "halo", "badge", "ink", "plain", and "tag". Start with a named label style, then use LabelOptions only for adjustments that the style does not cover.

labeled = GeoPlot(precincts)
labeled.add_districting_plan_layer(
    "CD",
    dissolve=True,
    show_labels=True,
    label_options=LabelOptions(label_style="badge"),
)
labeled.show()
../../../../_images/61fd13ec93df731703f9ce07d266ebdd42d08829e178b717d537b434fd56fdee.png

Colorbars

A choropleth returns a layer handle. show_colorbar=True attaches a colorbar immediately; add_colorbar(layer) can attach one later to the same scale. This matters when a map contains several continuous layers and only selected layers need keys.

shares = GeoPlot(precincts)
shares.add_choropleth_layer("BVAP_SHARE", show_colorbar=True)
shares.show()
../../../../_images/5603240633d3afdb7d86dd4767b340ea46d7ac58ce849fdabc550d777ff6f21b.png

Output

show() displays the current map and save() writes the same rendered state. Pass title= when constructing the plot or assign .title later; set_title_style() provides the same styling controls as the statistical builders. SVG and PDF preserve vector boundaries for reports; PNG is useful when the final destination requires a raster image. The builder’s .ax remains available for Matplotlib-level composition.

shares.save("bvap-share.svg")

Advanced label placement

LabelOptions can adjust individual labels without replacing the named label style. The adjustments mapping uses the displayed label as its key and an (x, y) offset as its value. Offsets are measured in the map’s coordinate units, so their useful magnitude depends on the active projected coordinate reference system.

adjusted = GeoPlot(precincts)
adjusted.add_districting_plan_layer(
    "CD",
    dissolve=True,
    show_labels=True,
    label_options=LabelOptions(
        label_style="badge",
        adjustments={5: (-60_000, -25_000), 6: (0, 25_000)},
    ),
)
adjusted.show()
../../../../_images/ca7b887075b4c0f4e71d27dade932da3934dc05cec5f265f697668a495d3ca7d.png