问题
I've got a problem that my choropleth map is not rendering correctly.
I've got a bunch of ride-hailing data of the city of Chicago and I would like to create a choropleth map by census tract. I checked that the key_on
feature is "geoid10" in the geojson file and ensured that the Pickup Census Tracts are all matching.
I also ensured that the data types of the key in the geojson file and the dataframe are the same (they are both objects)
Yet still, my choropleth map renders a black/grey tone instead of a properly toned map.
This is my code:
# 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
This is the current output in my Jupyter Notebook:
Despite that I believe that I matched the key_on feature correctly with the dataframe, I still think that the problem still lies in properly matching the geojson file with the dataframe.
Please find the code, data, and geojson file here: https://github.com/Doncorleone1018/Chicago-choropleth
- Current python version: 3.7.4
- Current folium version: 0.10.1
回答1:
Simply change this line:
pickup_by_censustract["Pickup Census Tract"].astype(str)
into:
pickup_by_censustract["Pickup Census Tract"] = pickup_by_censustract["Pickup Census Tract"].astype(str)
and you get:
来源:https://stackoverflow.com/questions/60815295/folium-chropleth-map-renders-grey-shading-instead-of-true-colors-of-the-thematic