Search code examples
pythonpython-3.xpandasfoliumchoropleth

Choropleth Map of Sydney


I am trying to create a Choropleth map but it's not showing.

# Sydney latitude and longitude values
latitude = -33.892319
longitude = 151.146167

sydney_geo = r'suburb-2-nsw.json' # geojson file

sydney_map = folium.Map(location=[latitude, longitude], zoom_start=8, tiles='Mapbox Bright')

    sydney_map.choropleth(
    geo_data=sydney_geo,
    data=df_cities,
    columns=['city', 'count'],
    key_on='feature.properties.nsw_loca_2',
    fill_color='YlOrRd', 
    fill_opacity=0.7, 
    line_opacity=0.2,
    legend_name=''
)

# display map
sydney_map

I am using this geojson file - https://raw.githubusercontent.com/tonywr71/GeoJson-Data/master/suburb-2-nsw.geojson

And this is how the dataframe (df_cities) looks like -

enter image description here

I am just getting blank output -

enter image description here


Solution

  • Are you sure you are using the correct extension for your geojson file?

    Using this code, it works:

    import folium
    import pandas as pd
    
    # Sydney latitude and longitude values
    latitude = -33.892319
    longitude = 151.146167
    m = folium.Map(location=[latitude, longitude],
                   zoom_start=3,
                   control_scale=True)
    
    sydney_geo = r'suburb-2-nsw.geojson' # geojson file
    
    df_cities = pd.DataFrame({'Unnamed':[2413, 815],
                              'city':['SIDNEY', 'DUBBO'],
                              'count':[593, 568]})
    
    folium.Choropleth(geo_data=sydney_geo,
                      name='choropleth',
                      data=df_cities,
                      columns=['city', 'count'],
                      key_on='feature.properties.nsw_loca_2',
                      fill_color='YlOrRd',
                      fill_opacity=0.7,
                      line_opacity=0.2,
                      legend_name='In Debt').add_to(m)
    
    folium.LayerControl().add_to(m)
    m