API reference¶
Bivario.
Python library for plotting bivariate choropleth maps in Matplotlib and Folium.
AccentsBivariateColourmap
¶
Bases: BivariateColourmap
BivariateColourmap defined by 2 colour accents.
Source code in bivario/cmap.py
__init__
¶
__init__(accent_a: ColourType, accent_b: ColourType, dark_mode: bool = False, light: ColourType | None = None, dark: ColourType | None = None) -> None
Initialise AccentsBivariateColourmap.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
accent_a
|
ColourType
|
First colour accent. |
required |
accent_b
|
ColourType
|
Second colour accent. |
required |
dark_mode
|
bool
|
Whether to use palette in dark mode. Swaps light and dark corners (low and high) for better readability on dark background. Can also be set during call. Defaults to False. |
False
|
light
|
ColourType | None
|
light corner colour. If None, will be (1, 1, 1). Defaults to None. |
None
|
dark
|
ColourType | None
|
Dark corner colour. If None will be (0.15, 0.15, 0.15). Defaults to None. |
None
|
Source code in bivario/cmap.py
CornersBivariateColourmap
¶
Bases: BivariateColourmap
BivariateColourmap defined by 4 corners.
Source code in bivario/cmap.py
__init__
¶
__init__(accent_a: ColourType, accent_b: ColourType, low: ColourType, high: ColourType) -> None
Initialise CornersBivariateColourmap.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
accent_a
|
ColourType
|
First colour accent. |
required |
accent_b
|
ColourType
|
Second colour accent. |
required |
low
|
ColourType
|
Low values colour. |
required |
high
|
ColourType
|
High values colour. |
required |
Source code in bivario/cmap.py
MplCmapBivariateColourmap
¶
Bases: BivariateColourmap
BivariateColourmap defined by 2 Matplotlib colourmaps.
Source code in bivario/cmap.py
NamedBivariateColourmap
¶
Bases: BivariateColourmap
BivariateColourmap loaded from predefined palettes.
Source code in bivario/cmap.py
__init__
¶
Initialise NamedBivariateColourmap.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the predefined palette. |
required |
invert_accents
|
bool
|
Whether to swap two colour accents. Can also be set during call. Defaults to False. |
False
|
dark_mode
|
bool
|
Whether to use palette in dark mode. Swaps light and dark corners (low and high) for better readability on dark background. Can also be set during call. Defaults to False. |
False
|
Source code in bivario/cmap.py
explore_bivariate_data
¶
explore_bivariate_data(gdf: GeoDataFrame, column_a: str | ValueInput, column_b: str | ValueInput, column_a_label: str | None = None, column_b_label: str | None = None, scheme: SCHEME_TYPE | tuple[SCHEME_TYPE, SCHEME_TYPE] = True, k: int | tuple[int, int] = 5, tiles: str | TileLayer | TileProvider | None = None, cmap: BivariateColourmap | str | None = None, dark_mode: bool | None = None, alpha: bool = True, alpha_norm_quantile: float = 0.9, legend: bool = True, legend_size_px: int = 200, legend_max_grid_size: int | None = 100, legend_loc: Literal['bl', 'br', 'tl', 'tr'] | None = None, legend_offset_px: float | tuple[float, float] | None = None, legend_background: bool = True, legend_border: bool = True, legend_kwargs: dict[str, Any] | None = None, **kwargs: Any) -> Map
Explore geospatial data with a bivariate colormap on a folium map.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gdf
|
GeoDataFrame
|
Geospatial data to plot. |
required |
column_a
|
str | ValueInput
|
Column name for the first variable or list/array of values. |
required |
column_b
|
str | ValueInput
|
Column name for the second variable or list/array of values. |
required |
column_a_label
|
str | None
|
Label for column a. If None, will use column name. Defaults to None. |
None
|
column_b_label
|
str | None
|
Label for column b. If None, will use column name. Defaults to None. |
None
|
scheme
|
str | None | bool | tuple
|
Mapclassify binning scheme for the data. If True, uses "NaturalBreaks". If False, no binning is applied. If str, uses the specified scheme. If None, no binning is applied. Can also define two different values for columns a and b. Defaults to True. |
True
|
k
|
int | tuple[int, int]
|
Number of classes for binning. Can also define two different values for columns a and b. Defaults to 5. |
5
|
tiles
|
str | TileLayer | TileProvider | None
|
Tile layer for the map. If None, will set based on dark mode - OpenFreeMap's "Dark" for the dark mode, and "Positron" for the light mode. Defaults to None. |
None
|
cmap
|
BivariateColourmap | str | None
|
Bivariate colourmap to use. If None, will load a default one. Defaults to None. |
None
|
dark_mode
|
bool | None
|
Whether to use dark mode for the map tiles. If None, will infer from the tiles if provided, otherwise defaults to False. Defaults to None |
None
|
alpha
|
bool
|
Whether to apply alpha transparency based on the data values. Will set higher values to be more opaque. It is calculated based on the normalized values of both columns. Defaults to True. |
True
|
alpha_norm_quantile
|
float
|
Quantile for normalizing alpha transparency. Will be used to calculate the maximum value for alpha scaling. It is recommended to use value below 1 to avoid outliers affecting the transparency too much. Defaults to 0.9. |
0.9
|
legend
|
bool
|
Whether to add a bivariate legend to the map. Defaults to True. |
True
|
legend_size_px
|
int
|
Size of the legend in pixels. Defaults to 200. |
200
|
legend_max_grid_size
|
int | None
|
Max size of the legend grid used for plotting the legend. Used with scheme=False. Defaults to 100. |
100
|
legend_loc
|
Literal['bl', 'br', 'tl', 'tr'] | None
|
Location of the legend on the map. Can be "bl" (bottom-left), "br" (bottom-right), "tl" (top-left), or "tr" (top-right). Defaults to "bl". |
None
|
legend_offset_px
|
float | tuple[float, float] | None
|
Offset of the legend from the specified location in pixels. If None, uses default offsets based on location. Defaults to None. |
None
|
legend_background
|
bool
|
Whether to add a background to the legend. Defaults to True. |
True
|
legend_border
|
bool
|
Whether to add a border to the legend. Defaults to True. |
True
|
legend_kwargs
|
dict[str, Any] | None
|
Additional keyword arguments for the legend plotting function. Defaults to None. |
None
|
**kwargs
|
Any
|
Additional keyword arguments for the folium map. |
{}
|
Returns:
| Type | Description |
|---|---|
Map
|
folium.Map: Folium map with the bivariate colormap applied. |
Examples:
Plot NYC bike trips with morning starts and ends:
>>> from bivario.example_data import nyc_bike_trips
>>> from bivario import explore_bivariate_data
>>> gdf = nyc_bike_trips()
>>> explore_bivariate_data(
... gdf,
... column_a="morning_starts",
... column_b="morning_ends",
... )
<folium.folium.Map object at 0x...>
Plot in dark mode with different colormap and different binning:
>>> from bivario.example_data import nyc_bike_trips
>>> from bivario import explore_bivariate_data
>>> gdf = nyc_bike_trips()
>>> explore_bivariate_data(
... gdf,
... column_a="morning_starts",
... column_b="morning_ends",
... column_a_label="Morning Starts",
... column_b_label="Morning Ends",
... dark_mode=True,
... cmap="bubblegum",
... scheme="Quantiles",
... k=10,
... )
<folium.folium.Map object at 0x...>
Plot without binning (numerical values) and different legend position:
>>> from bivario.example_data import nyc_bike_trips
>>> from bivario import explore_bivariate_data
>>> gdf = nyc_bike_trips()
>>> explore_bivariate_data(
... gdf,
... column_a="morning_starts",
... column_b="afternoon_starts",
... scheme=False,
... legend_loc="tr",
... legend_size_px=300,
... )
<folium.folium.Map object at 0x...>
Source code in bivario/folium/__init__.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | |
get_bivariate_cmap
¶
get_bivariate_cmap(cmap: str | BivariateColourmap | None = None, **kwargs: Any) -> BivariateColourmap
Return a BivariateColourmap object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cmap
|
str | BivariateColourmap | None
|
Colourmap name or object. If None, will load defalt named palette - rosewood_pine. Defaults to None. |
None
|
**kwargs
|
Any
|
Additional keyword arguments for the NamedBivariateColourmap. |
{}
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If provided cmap object is of unknown type. |
Returns:
| Name | Type | Description |
|---|---|---|
BivariateColourmap |
BivariateColourmap
|
Parsed BivariateColourmap object. |
Source code in bivario/cmap.py
plot_bivariate_legend
¶
plot_bivariate_legend(values_a: ValueInput, values_b: ValueInput, ax: Axes | None = None, cmap: BivariateColourmap | str | None = None, grid_size: int | tuple[int, int] | None = None, label_a: str | None = None, label_b: str | None = None, tick_labels_a: list[Any] | None = None, tick_labels_b: list[Any] | None = None, dark_mode: bool = False, font_colour: str | None = None, tick_fontsize_px: int = 10) -> Axes
Plot bivariate 2D legend using Matplotlib.
Generates a 2D matrix of values and uses imshow function to display it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values_a
|
ValueInput
|
List or array of values for first variable. Will be assigned to the Y axis. |
required |
values_b
|
ValueInput
|
List or array of values for second variable. Will be assigned to the X axis. |
required |
ax
|
Axes | None
|
Matplotlib axis to plot legend on. If None, will be created. Defaults to None. |
None
|
cmap
|
BivariateColourmap | str | None
|
Bivariate colourmap to use. If None, will load a default one. Defaults to None. |
None
|
grid_size
|
int | tuple[int, int] | None
|
Number of pixels in the legend grid. Can define two different values for X and Y axis (in this order). If None, will default to 100. Defaults to None. |
None
|
label_a
|
str | None
|
Label to use for the first variable (Y axis). If None, will try to read series name, or defaults to "Value A". Defaults to None. |
None
|
label_b
|
str | None
|
Label to use for the first variable (X axis). If None, will try to read series name, or defaults to "Value B". Defaults to None. |
None
|
tick_labels_a
|
list[Any] | None
|
List of predefined ticks to use for the first variable (Y axis). Useful if binning has been applied. Defaults to None. |
None
|
tick_labels_b
|
list[Any] | None
|
List of predefined ticks to use for the first variable (X axis). Useful if binning has been applied. Defaults to None. |
None
|
dark_mode
|
bool
|
Whether to use dark mode to select a proper order of colours in the colourmap. Defaults to False. |
False
|
font_colour
|
str | None
|
Font colour for the labels and ticks. If None, will be selected based on dark_mode value - white or black. Defaults to None. |
None
|
tick_fontsize_px
|
int
|
Size of the ticksize and labels font in pixels. Defaults to 10. |
10
|
Returns:
| Name | Type | Description |
|---|---|---|
Axes |
Axes
|
Matplotlib axes with plotted legend. |
Source code in bivario/legend.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | |
viz_bivariate_data
¶
viz_bivariate_data(data: Any | list[Any] | tuple[Any, ...], column_a: str | ValueInput, column_b: str | ValueInput, column_a_label: str | None = None, column_b_label: str | None = None, scheme: SCHEME_TYPE | tuple[SCHEME_TYPE, SCHEME_TYPE] = True, k: int | tuple[int, int] = 5, tiles: str | None = None, cmap: BivariateColourmap | str | None = None, dark_mode: bool | None = None, alpha: bool = True, alpha_norm_quantile: float = 0.9, legend: Literal[True] = True, legend_size_px: int = 400, legend_max_grid_size: int | None = 100, legend_tick_fontsize_px: int = 16, legend_kwargs: dict[str, Any] | None = None, scatterplot_kwargs: ScatterplotLayerKwargs | None = None, path_kwargs: PathLayerKwargs | None = None, polygon_kwargs: PolygonLayerKwargs | None = None, map_kwargs: MapKwargs | None = None) -> LonboardMapWithLegend
viz_bivariate_data(data: Any | list[Any] | tuple[Any, ...], column_a: str | ValueInput, column_b: str | ValueInput, column_a_label: str | None = None, column_b_label: str | None = None, scheme: SCHEME_TYPE | tuple[SCHEME_TYPE, SCHEME_TYPE] = True, k: int | tuple[int, int] = 5, tiles: str | None = None, cmap: BivariateColourmap | str | None = None, dark_mode: bool | None = None, alpha: bool = True, alpha_norm_quantile: float = 0.9, legend: Literal[False] = False, legend_size_px: int = 400, legend_max_grid_size: int | None = 100, legend_tick_fontsize_px: int = 16, legend_kwargs: dict[str, Any] | None = None, scatterplot_kwargs: ScatterplotLayerKwargs | None = None, path_kwargs: PathLayerKwargs | None = None, polygon_kwargs: PolygonLayerKwargs | None = None, map_kwargs: MapKwargs | None = None) -> Map
viz_bivariate_data(data: Any | list[Any] | tuple[Any, ...], column_a: str | ValueInput, column_b: str | ValueInput, column_a_label: str | None = None, column_b_label: str | None = None, scheme: SCHEME_TYPE | tuple[SCHEME_TYPE, SCHEME_TYPE] = True, k: int | tuple[int, int] = 5, tiles: str | None = None, cmap: BivariateColourmap | str | None = None, dark_mode: bool | None = None, alpha: bool = True, alpha_norm_quantile: float = 0.9, legend: bool = True, legend_size_px: int = 400, legend_max_grid_size: int | None = 100, legend_tick_fontsize_px: int = 16, legend_kwargs: dict[str, Any] | None = None, scatterplot_kwargs: ScatterplotLayerKwargs | None = None, path_kwargs: PathLayerKwargs | None = None, polygon_kwargs: PolygonLayerKwargs | None = None, map_kwargs: MapKwargs | None = None) -> Map | LonboardMapWithLegend
Visualize geospatial data with a bivariate colormap on a lonboard map.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any | list[Any] | tuple[Any, ...]
|
Geospatial data to plot. Any compatible with lonboard viz function. |
required |
column_a
|
str | ValueInput
|
Column name for the first variable or list/array of values. |
required |
column_b
|
str | ValueInput
|
Column name for the second variable or list/array of values. |
required |
column_a_label
|
str | None
|
Label for column a. If None, will use column name. Defaults to None. |
None
|
column_b_label
|
str | None
|
Label for column b. If None, will use column name. Defaults to None. |
None
|
scheme
|
str | None | bool | tuple
|
Mapclassify binning scheme for the data. If True, uses "NaturalBreaks". If False, no binning is applied. If str, uses the specified scheme. If None, no binning is applied. Can also define two different values for columns a and b. Defaults to True. |
True
|
k
|
int | tuple[int, int]
|
Number of classes for binning. Can also define two different values for columns a and b. Defaults to 5. |
5
|
tiles
|
str | None
|
Tile layer for the map. If None, will set based on dark mode - OpenFreeMap's "Dark" for the dark mode, and "Positron" for the light mode. Defaults to None. |
None
|
cmap
|
BivariateColourmap | str | None
|
Bivariate colourmap to use. If None, will load a default one. Defaults to None. |
None
|
dark_mode
|
bool | None
|
Whether to use dark mode for the map tiles. If None, will infer from the tiles if provided, otherwise defaults to False. Defaults to None |
None
|
alpha
|
bool
|
Whether to apply alpha transparency based on the data values. Will set higher values to be more opaque. It is calculated based on the normalized values of both columns. Defaults to True. |
True
|
alpha_norm_quantile
|
float
|
Quantile for normalizing alpha transparency. Will be used to calculate the maximum value for alpha scaling. It is recommended to use value below 1 to avoid outliers affecting the transparency too much. Defaults to 0.9. |
0.9
|
legend
|
bool
|
Whether to return a lonboard map with a legend plotting function. If True, will return LonboardMapWithLegend object with lonboard map and legend function. If False, will return lonboard.Map. Defaults to True. |
True
|
legend_size_px
|
int
|
Size of the legend in pixels. Defaults to 400. |
400
|
legend_max_grid_size
|
int | None
|
Max size of the legend grid used for plotting the legend. Used with scheme=False. Defaults to 100. |
100
|
legend_tick_fontsize_px
|
int
|
Size of the ticks and labels for the legend in pixels. Defaults to 16. |
16
|
legend_kwargs
|
dict[str, Any] | None
|
Additional keyword arguments for the legend plotting function. Defaults to None. |
None
|
scatterplot_kwargs
|
ScatterplotLayerKwargs | None
|
a |
None
|
path_kwargs
|
PathLayerKwargs | None
|
a |
None
|
polygon_kwargs
|
PolygonLayerKwargs | None
|
a |
None
|
map_kwargs
|
MapKwargs | None
|
a |
None
|
Returns:
| Type | Description |
|---|---|
Map | LonboardMapWithLegend
|
lonboard.Map | LonboardMapWithLegend: Lonboard map with the bivariate colormap applied or object with lonboard map and legend plotting function. |
Examples:
Plot NYC bike trips with morning starts and ends:
>>> from bivario.example_data import nyc_bike_trips
>>> from bivario import viz_bivariate_data
>>> gdf = nyc_bike_trips()
>>> x = viz_bivariate_data(
... gdf,
... column_a="morning_starts",
... column_b="morning_ends",
... )
>>> x
LonboardMapWithLegend(...)
Display a legend in another cell:
Get lonboard.Map object from the result:
Plot in dark mode with different colormap and different binning:
>>> from bivario.example_data import nyc_bike_trips
>>> from bivario import viz_bivariate_data
>>> gdf = nyc_bike_trips()
>>> viz_bivariate_data(
... gdf,
... column_a="morning_starts",
... column_b="morning_ends",
... column_a_label="Morning Starts",
... column_b_label="Morning Ends",
... dark_mode=True,
... cmap="bubblegum",
... scheme="Quantiles",
... k=10,
... )
LonboardMapWithLegend(...)
Plot without binning (numerical values) and add tooltip to map:
>>> from bivario.example_data import nyc_bike_trips
>>> from bivario import viz_bivariate_data
>>> gdf = nyc_bike_trips()
>>> viz_bivariate_data(
... gdf,
... column_a="morning_starts",
... column_b="afternoon_starts",
... scheme=False,
... map_kwargs={"show_tooltip": True},
... )
LonboardMapWithLegend(...)
Plot without legend and get lonboard Map directly:
>>> from bivario.example_data import nyc_bike_trips
>>> from bivario import viz_bivariate_data
>>> gdf = nyc_bike_trips()
>>> viz_bivariate_data(
... gdf,
... column_a="morning_starts",
... column_b="morning_ends",
... legend=False,
... )
<lonboard._map.Map object at 0x...>
Source code in bivario/lonboard/__init__.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 | |
Example data¶
Example datasets for bivario package.
nyc_bike_trips
¶
Load example NYC bike trips data as a GeoDataFrame.
Contains H3-indexed bike trip start and end locations in New York City. Starts and split into morning and afternoon trips.
Available columns
- h3: H3 index of the location
- morning_starts: Number of bike trips starting in the morning
- morning_ends: Number of bike trips ending in the morning
- afternoon_starts: Number of bike trips starting in the afternoon
- afternoon_ends: Number of bike trips ending in the afternoon
- geometry: Geometry of the H3 cell
Returns:
| Type | Description |
|---|---|
GeoDataFrame
|
gpd.GeoDataFrame: A GeoDataFrame containing NYC bike trips data. |
Examples:
Plot example folium map with this data:
>>> from bivario.example_data import nyc_bike_trips
>>> from bivario import explore_bivariate_data
>>> explore_bivariate_data(
... nyc_bike_trips(),
... column_a="morning_starts",
... column_b="morning_ends",
... )
<folium.folium.Map object at 0x...>