python-visualization / folium

Python Data. Leaflet.js Maps.
https://python-visualization.github.io/folium/
MIT License
6.92k stars 2.23k forks source link

folium choropleth rendering the legend of map bug #1686

Closed dongrixinyu closed 1 year ago

dongrixinyu commented 1 year ago

Describe the bug A clear and concise description of what the bug is. Here is an example of how I use choropleth.Map to rendering all provinces and cities population data of china.

image

and the population of each city ranges from 2300 ~12042384. the number of people for most cities ranges from 2300 ~ 1000000. So the distribution of population for all cities is uneven.

this caused the legend of the population is like above. most part of the legend is dark purple.

So, I recommend that a log(x) function is nessesary for the legend to smoothing the data distribution.

To Reproduce

state_data = pd.read_csv(r'D:\BaiduNetdiskDownload\US_folium\china-population.csv')

max_value = state_data['population'].max()
state_data['population'] /= max_value
bins = list(state_data['population'].quantile([i/10 for i in range(0, 11)]))

m = folium.Map(location=[37, 108], zoom_start=4)

folium.Choropleth(
    geo_data=r'D:\BaiduNetdiskDownload\US_folium\china-counties.json',
    data=state_data,
    columns=["county", "population"],
    key_on="feature.id",
    fill_color="BuPu",
    fill_opacity=0.7,
    line_opacity=0.1,
    legend_name="人口数量",
    bins=bins,
    reset=True,
).add_to(m)

Expected behavior A smoothing legend that could tell the number scope of every color.

Environment (please complete the following information):

Additional context Add any other context about the problem here.

Possible solutions List any solutions you may have come up with.

folium is maintained by volunteers. Can you help making a fix for this issue?

Conengmo commented 1 year ago

We just released a new feature that maybe helps with this. It's the use_jenks=True argument on Choropleth, for an example and motivation see https://nbviewer.org/github/python-visualization/folium/blob/main/examples/Choropleth%20with%20Jenks%20natural%20breaks%20optimization.ipynb. Note that you will have to install the jenkspy package for this.

Conengmo commented 1 year ago

If you're talking about a log scale legend, that's not currently available. Though here's a guide with a work around: https://github.com/python-visualization/folium/issues/1374#issuecomment-1215240840. It may be interesting to have non-linear scaling as a core feature, but it's not something I'm planning on working, so it's dependent on somebody coming along and making a reasonable implementation.

giswqs commented 1 year ago

Leafmap has some choropleth map functionality that builds upon folium and mapclassify. Here is an example: https://leafmap.org/notebooks/53_choropleth/