如何处理 Folium 地图上的极值
How to deal with extreme values on a Folium map
我对 folium 地图的经验很少。
我需要制作一张包含每个部门的机构数量的地图,问题是首都的机构数量远远多于内部,所以当我创建颜色层时,我得到的首都是深蓝色,其余的都是相同的较浅的颜色。这样地图就没那么有用了...
我该如何解决?我想过用这个值除以人口,但最好还是用原来的值。
在文档中,我没有找到参数化颜色的方法。
df1 = pd.DataFrame({'code':['75','77','78','91','92','93','94','95'],'value':['13000','2000','2500','2300','2150','2600','1630','1300']})
dep_geo = geopandas.read_file('./dep.json', driver="JSON") #geodata taken from https://github.com/gregoiredavid/france-geojson/blob/master/departements.geojson
departments = {'75', '77', '78', '91', '92', '93', '94', '95'}
dep_geo = dep_geo[dep_geo['code'].isin(departments)]
df_map = dep_geo.merge(df1, how="left", left_on=['code'], right_on=['code'])
my_map = folium.Map(location=[48.856614, 2.3522219],
zoom_start = 9, tiles='cartodbpositron')
folium.Choropleth(
geo_data=df_map,
data=df_map,
columns=['code',"value"],
key_on="feature.properties.code",
fill_color='YlGnBu',
fill_opacity=0.5,
line_opacity=0.2,
legend_name="value ",
smooth_factor=0,
Highlight= True,
line_color = "black",
name = "value",
show=False,
overlay=True,
nan_fill_color = "White"
).add_to(my_map)
结果:
感谢您的帮助!
- 就像使用 vmax 参数一样简单。我已设置为第 85 个百分位数
- 还使用 geopandas
explore()
生成了 folium 地图
import geopandas as gpd
import pandas as pd
import folium
df1 = pd.DataFrame(
{
"code": ["75", "77", "78", "91", "92", "93", "94", "95"],
"value": ["13000", "2000", "2500", "2300", "2150", "2600", "1630", "1300"],
}
)
# dep_geo = geopandas.read_file('./dep.json', driver="JSON") #geodata taken from https://github.com/gregoiredavid/france-geojson/blob/master/departements.geojson
dep_geo = gpd.read_file(
"https://github.com/gregoiredavid/france-geojson/raw/master/departements.geojson"
) # geodata taken from https://github.com/gregoiredavid/france-geojson/blob/master/departements.geojson
departments = {"75", "77", "78", "91", "92", "93", "94", "95"}
dep_geo = dep_geo[dep_geo["code"].isin(departments)]
df_map = dep_geo.merge(df1, how="left", left_on=["code"], right_on=["code"])
df_map["value"] = pd.to_numeric(df_map["value"])
df_map.explore(
column="value",
cmap="YlGnBu",
vmax=df_map["value"].quantile(0.85),
style_kwds=dict(
color="rgba(0,0,0,.2)",
),
location=[48.856614, 2.3522219],
zoom_start=9,
tiles="cartodbpositron",
)
我对 folium 地图的经验很少。 我需要制作一张包含每个部门的机构数量的地图,问题是首都的机构数量远远多于内部,所以当我创建颜色层时,我得到的首都是深蓝色,其余的都是相同的较浅的颜色。这样地图就没那么有用了... 我该如何解决?我想过用这个值除以人口,但最好还是用原来的值。 在文档中,我没有找到参数化颜色的方法。
df1 = pd.DataFrame({'code':['75','77','78','91','92','93','94','95'],'value':['13000','2000','2500','2300','2150','2600','1630','1300']})
dep_geo = geopandas.read_file('./dep.json', driver="JSON") #geodata taken from https://github.com/gregoiredavid/france-geojson/blob/master/departements.geojson
departments = {'75', '77', '78', '91', '92', '93', '94', '95'}
dep_geo = dep_geo[dep_geo['code'].isin(departments)]
df_map = dep_geo.merge(df1, how="left", left_on=['code'], right_on=['code'])
my_map = folium.Map(location=[48.856614, 2.3522219],
zoom_start = 9, tiles='cartodbpositron')
folium.Choropleth(
geo_data=df_map,
data=df_map,
columns=['code',"value"],
key_on="feature.properties.code",
fill_color='YlGnBu',
fill_opacity=0.5,
line_opacity=0.2,
legend_name="value ",
smooth_factor=0,
Highlight= True,
line_color = "black",
name = "value",
show=False,
overlay=True,
nan_fill_color = "White"
).add_to(my_map)
结果:
感谢您的帮助!
- 就像使用 vmax 参数一样简单。我已设置为第 85 个百分位数
- 还使用 geopandas
explore()
生成了 folium 地图
import geopandas as gpd
import pandas as pd
import folium
df1 = pd.DataFrame(
{
"code": ["75", "77", "78", "91", "92", "93", "94", "95"],
"value": ["13000", "2000", "2500", "2300", "2150", "2600", "1630", "1300"],
}
)
# dep_geo = geopandas.read_file('./dep.json', driver="JSON") #geodata taken from https://github.com/gregoiredavid/france-geojson/blob/master/departements.geojson
dep_geo = gpd.read_file(
"https://github.com/gregoiredavid/france-geojson/raw/master/departements.geojson"
) # geodata taken from https://github.com/gregoiredavid/france-geojson/blob/master/departements.geojson
departments = {"75", "77", "78", "91", "92", "93", "94", "95"}
dep_geo = dep_geo[dep_geo["code"].isin(departments)]
df_map = dep_geo.merge(df1, how="left", left_on=["code"], right_on=["code"])
df_map["value"] = pd.to_numeric(df_map["value"])
df_map.explore(
column="value",
cmap="YlGnBu",
vmax=df_map["value"].quantile(0.85),
style_kwds=dict(
color="rgba(0,0,0,.2)",
),
location=[48.856614, 2.3522219],
zoom_start=9,
tiles="cartodbpositron",
)