Folium Choropleth Map 呈现灰色阴影而不是专题图的真实颜色
Folium Chropleth Map renders grey shading instead of true colors of the thematic map
我遇到了一个问题,我的分区统计图没有正确渲染。
我有一堆芝加哥市的叫车数据,我想按人口普查区创建一个等值线图。我检查了 geojson 文件中的 key_on
特征是 "geoid10" 并确保 Pickup Census Tracts 都匹配。
我还确保了geojson文件和dataframe中key的数据类型是一样的(都是对象)
然而,我的等值线图呈现 black/grey 色调而不是适当色调的地图。
这是我的代码:
# Import packages
import pandas as pd
import geopandas as gpd
import folium
# Load in GeoJSON file, because works with Folium
geosjon_file = "Boundaries - Census Tracts - 2010.geojson"
chicago_census_tracts = gpd.read_file(geosjon_file)
# Pickup by census tract
pickup_by_censustract = pd.read_csv("pickup_demand_by_censustract_test.csv")
# Convert Pickup Census Tract to dtype: object, as key_on object in geosjon_file is also an dtype: object
pickup_by_censustract["Pickup Census Tract"].astype(str)
# Creating Chicago map with Folium
chicago_map = folium.Map(
location=[41.881832, -87.623177],
zoom_start=9)
#folium.TileLayer("CartoDB positron", name="Light Map", control=False).add_to(chicago_map)
# Overlaying Chicago map with bounderies of census tracts
folium.GeoJson(chicago_census_tracts).add_to(chicago_map)
# Creating Choropleth
folium.Choropleth(
geo_data=chicago_census_tracts,
name="choropleth",
data=pickup_by_censustract,
columns=["Pickup Census Tract", "Count"],
key_on="feature.properties.geoid10",
fill_color="YlGn",
fill_opacity=0.7,
line_opacity=0.2,
legend_name="Ride-hailing demand"
).add_to(chicago_map)
folium.LayerControl().add_to(chicago_map)
chicago_map
这是我的 Jupyter Notebook 中的当前输出:
尽管我相信我已将 key_on 特征与数据框正确匹配,但我仍然认为问题仍然在于将 geojson 文件与数据框正确匹配。
请在此处找到代码、数据和 geojson 文件:
https://github.com/Doncorleone1018/Chicago-choropleth
- 当前python版本:3.7.4
- 当前 folium 版本:0.10.1
只需更改此行:
pickup_by_censustract["Pickup Census Tract"].astype(str)
进入:
pickup_by_censustract["Pickup Census Tract"] = pickup_by_censustract["Pickup Census Tract"].astype(str)
你得到:
我遇到了一个问题,我的分区统计图没有正确渲染。
我有一堆芝加哥市的叫车数据,我想按人口普查区创建一个等值线图。我检查了 geojson 文件中的 key_on
特征是 "geoid10" 并确保 Pickup Census Tracts 都匹配。
我还确保了geojson文件和dataframe中key的数据类型是一样的(都是对象)
然而,我的等值线图呈现 black/grey 色调而不是适当色调的地图。
这是我的代码:
# Import packages
import pandas as pd
import geopandas as gpd
import folium
# Load in GeoJSON file, because works with Folium
geosjon_file = "Boundaries - Census Tracts - 2010.geojson"
chicago_census_tracts = gpd.read_file(geosjon_file)
# Pickup by census tract
pickup_by_censustract = pd.read_csv("pickup_demand_by_censustract_test.csv")
# Convert Pickup Census Tract to dtype: object, as key_on object in geosjon_file is also an dtype: object
pickup_by_censustract["Pickup Census Tract"].astype(str)
# Creating Chicago map with Folium
chicago_map = folium.Map(
location=[41.881832, -87.623177],
zoom_start=9)
#folium.TileLayer("CartoDB positron", name="Light Map", control=False).add_to(chicago_map)
# Overlaying Chicago map with bounderies of census tracts
folium.GeoJson(chicago_census_tracts).add_to(chicago_map)
# Creating Choropleth
folium.Choropleth(
geo_data=chicago_census_tracts,
name="choropleth",
data=pickup_by_censustract,
columns=["Pickup Census Tract", "Count"],
key_on="feature.properties.geoid10",
fill_color="YlGn",
fill_opacity=0.7,
line_opacity=0.2,
legend_name="Ride-hailing demand"
).add_to(chicago_map)
folium.LayerControl().add_to(chicago_map)
chicago_map
这是我的 Jupyter Notebook 中的当前输出:
尽管我相信我已将 key_on 特征与数据框正确匹配,但我仍然认为问题仍然在于将 geojson 文件与数据框正确匹配。
请在此处找到代码、数据和 geojson 文件: https://github.com/Doncorleone1018/Chicago-choropleth
- 当前python版本:3.7.4
- 当前 folium 版本:0.10.1
只需更改此行:
pickup_by_censustract["Pickup Census Tract"].astype(str)
进入:
pickup_by_censustract["Pickup Census Tract"] = pickup_by_censustract["Pickup Census Tract"].astype(str)
你得到: