Python - Folium Choropleth Map - colors incorrect

与世无争的帅哥 提交于 2019-12-04 21:26:10

I got it all figured out. Missing values are coloured grey, and the legend is customized with intervals of my choice. Cleaning up the geojson, removing trailing white space, and making all suburb names UPPERCASE solved a lot of problems.

Files are here

Create Dictionary

import pandas as pd
import csv 

csv_path='Data_tables_Criminal_Incidents_Visualisation_year_ending_June_2018.csv'
df=pd.read_csv(csv_path)

# sum the number of incidents recorded for each suburb
df=df.groupby(['Suburb/Town Name'])['Incidents Recorded'].agg(
    # make the numbers numeric otherwise it just concatenates strings
    lambda x: pd.to_numeric(x, errors='coerce').sum()
)

# create a dictionary, where keys are Suburb/Town Name and values are number of incidents
suburb_dict = df.to_dict()

Style Function

def style_function(feature):
    suburb = suburb_dict.get(feature['properties']['Suburb_Name'])
    return {
        'fillColor': '#gray' if suburb is None else colormap(suburb),
        'fillOpacity': 0.6,
        #borders
        'weight': 0.2,
    }

Folium Map

import folium

world_map = folium.Map(
        location=[-38.292102, 144.727880],
        zoom_start=6,
        tiles='openstreetmap'
        )

folium.GeoJson(
    data = 'vic_for_crime_2018.geojson',
    style_function = style_function    
).add_to(world_map)

Colormap

import branca

colormap = branca.colormap.linear.YlOrRd_09.scale(0, 8500)
colormap = colormap.to_step(index=[0, 1000, 3000, 5000, 8500])
colormap.caption = 'Incidents of Crime in Victoria (year ending June 2018)'
colormap.add_to(world_map)

world_map.save('vic_final.html')
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!