Skip to content

Commit bd910b4

Browse files
pmainsConengmo
andauthored
Jenks in Choropleth (#1634)
* Add Jenks Natural Breaks Optimization to Choropleth * Remove jenkspy from requirements. * Install jenkspy in example ipynb * Removing ipynb output. Making SSL ignore cert. * address comments * add docstring Co-authored-by: Frank <33519926+Conengmo@users.noreply.github.com>
1 parent 45c92b6 commit bd910b4

3 files changed

Lines changed: 572 additions & 1 deletion

File tree

examples/Choropleth with Jenks natural breaks optimization.ipynb

Lines changed: 555 additions & 0 deletions
Large diffs are not rendered by default.

folium/features.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1155,6 +1155,10 @@ class Choropleth(FeatureGroup):
11551155
representation. Leaflet defaults to 1.0.
11561156
highlight: boolean, default False
11571157
Enable highlight functionality when hovering over a GeoJSON area.
1158+
use_jenks: bool, default False
1159+
Use jenkspy to calculate bins using "natural breaks"
1160+
(Fisher-Jenks algorithm). This is useful when your data is unevenly
1161+
distributed.
11581162
name : string, optional
11591163
The name of the layer, as it will appear in LayerControls
11601164
overlay : bool, default True
@@ -1193,6 +1197,7 @@ def __init__(self, geo_data, data=None, columns=None, key_on=None, # noqa
11931197
line_weight=1, line_opacity=1, name=None, legend_name='',
11941198
overlay=True, control=True, show=True,
11951199
topojson=None, smooth_factor=None, highlight=None,
1200+
use_jenks=False,
11961201
**kwargs):
11971202
super(Choropleth, self).__init__(name=name, overlay=overlay,
11981203
control=control, show=show)
@@ -1231,7 +1236,17 @@ def __init__(self, geo_data, data=None, columns=None, key_on=None, # noqa
12311236
if color_data is not None and key_on is not None:
12321237
real_values = np.array(list(color_data.values()))
12331238
real_values = real_values[~np.isnan(real_values)]
1234-
_, bin_edges = np.histogram(real_values, bins=bins)
1239+
if use_jenks:
1240+
from jenkspy import jenks_breaks
1241+
1242+
if not isinstance(bins, int):
1243+
raise ValueError(
1244+
f'bins value must be an integer when using Jenks.'
1245+
f' Invalid value "{bins}" received.'
1246+
)
1247+
bin_edges = np.array(jenks_breaks(real_values, bins), dtype=float)
1248+
else:
1249+
_, bin_edges = np.histogram(real_values, bins=bins)
12351250

12361251
bins_min, bins_max = min(bin_edges), max(bin_edges)
12371252
if np.any((real_values < bins_min) | (real_values > bins_max)):

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ geographiclib
1212
geopandas
1313
gpxpy
1414
ipykernel
15+
jenkspy
1516
jupyter
1617
matplotlib
1718
nbconvert

0 commit comments

Comments
 (0)