简体   繁体   中英

choropleth map not seen using folium

I am trying to produce a basic Choropleth map, using Folium. This is the code I am using:

    import os
    import pandas as pd
    import geopandas as gpd
    import folium
    from folium import Choropleth, Circle, Marker
    from folium.plugins import HeatMap, MarkerCluster
    
    zcta_shp = gpd.read_file("https://data.cityofnewyork.us/api/geospatial/pri4-ifjk?method=export&format=GeoJSON")
    zcta_shp.pop_est = zcta_shp.pop_est.astype(int)
    zcta_map = folium.Map(location = [40.8,-73.9], tiles = 'cartodbpositron', zoom_start=9)
    folium.Choropleth(geo_data = zcta_shp['geometry'].__geo_interface__,
              name = 'choropleth',
              data = zcta_shp['pop_est'],
              key_on = 'feature.id',
              fill_color = 'YlGnBu',
                     legend_name='Population(by ZCTA)').add_to(zcta_map)

I am not seeing any error message in ym Jupyter Notebook, just this message: <folium.features.Choropleth at 0x7f4e82944c40>

Can't figure out where I am going wrong, any help? Thanks!

There are three reasons why the map is not color-coded: first, a geojson file is used as user data, but there is no id column associated with the geojson data. Second, the geojson file is not specified as user data, and third, the column specification for the user data is not specified. The task is for you to update the id column of the user data with the correct id to match the id in the geojson.

import os
import pandas as pd
import numpy as np
import geopandas as gpd
import folium
from folium import Choropleth, Circle, Marker
from folium.plugins import HeatMap, MarkerCluster
    
zcta_shp = gpd.read_file("https://data.cityofnewyork.us/api/geospatial/pri4-ifjk?method=export&format=GeoJSON")
zcta_shp.pop_est = zcta_shp.pop_est.astype(int)
zcta_shp['id'] = [str(x) for x in np.arange(0, 178,1)]

zcta_map = folium.Map(location = [40.8,-73.9], tiles = 'cartodbpositron', zoom_start=10)
folium.Choropleth(
    geo_data = zcta_shp['geometry'].__geo_interface__,
    name = 'choropleth',
    data = zcta_shp,
    columns=['id', 'pop_est'],
    key_on = 'feature.id',
    fill_color = 'YlGnBu',
    legend_name='Population(by ZCTA)'
).add_to(zcta_map)

zcta_map

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM