pythonchoropleth

Trouble making a Choropleth map with Folium


I am looking to make a chloropleth map of UK cities I am using this geojson: https://geoportal.statistics.gov.uk/datasets/ons::counties-december-2019-boundaries-en-bfc/explore

venue_map = folium.Map(location = [20,78], zoom_start = 2.5)

folium.Choropleth(
    geo_data = cty19nm, 
    data = data_to_plot1,
    columns = ["city","xg"],
    key_on = 'feature.properties.st_nm',
    fill_color = 'YlOrBr', fill_opacity=0.6, line_opacity=0.1,
    legend_name = "Expected Goals per city").add_to(venue_map)
folium.LayerControl().add_to(venue_map)

venue_map

I get this error message: NameError: name 'cty19nm' is not defined

How do I find the geo_data name? And is there anything else wrong with my code?

Thank you!!!

I have tried finding the data but don't seem to know where to look. Any help is much appreciated!


Solution

  • It seems like the error you're encountering is due to the fact that the variable cty19nm is not defined in your code. The variable cty19nm is supposed to hold the GeoJSON data that defines the boundaries of the UK cities. To fix this issue, you need to load the GeoJSON data into the cty19nm variable before using it in the folium.Choropleth function

    Here's an example

        import folium
    
    # Load the GeoJSON data
    cty19nm = 'https://opendata.arcgis.com/datasets/c30ad3a7dd7441dea76a3e1b9f855ae7_0.geojson'
    
    # Your data_to_plot1 and other relevant data setup here
    
    # Create the map
    venue_map = folium.Map(location=[20, 78], zoom_start=2.5)
    
    # Create the Choropleth map
    folium.Choropleth(
        geo_data=cty19nm,
        data=data_to_plot1,
        columns=["city", "xg"],
        key_on='feature.properties.st_nm',
        fill_color='YlOrBr', fill_opacity=0.6, line_opacity=0.1,
        legend_name="Expected Goals per city"
    ).add_to(venue_map)
    
    # Add layer control
    folium.LayerControl().add_to(venue_map)
    
    # Display the map
    venue_map
    

    Hope that helps.