Python - Folium Choropleth 地图 - 颜色不正确

Python - Folium Choropleth Map - colors incorrect

我的问题是郊区在 Folium 地图上显示的颜色不正确。例如,Dandenong 和 Frankston 应该用最深的颜色着色,因为它们在数据框中的计数最高,但它们用较浅的颜色着色。

数据框缺少一些郊区。那些郊区被涂上了最深的颜色。

另一个问题是 csv 中的所有郊区都是大写的,但 geojson 混合了大小写,例如 "Frankston"、"St Kilda" 或 "McKinnon"。如果 choropleth 代码不关心大小写,那将会很有帮助。我可以更改数据框中的文本,使 "FRANKSTON"、"Frankston" 和 "ST KILDA"、"St Kilda",但 "MCKINNON" 到 "McKinnon" 证明有点棘手。

创建数据框

import csv 
import pandas as pd
csv_path='Data_tables_Criminal_Incidents_Visualisation_year_ending_June_2018.csv'
df=pd.read_csv(csv_path)

with open(csv_path, 'r') as csvfile: 
    # creating a csv reader object 
    csvreader = csv.reader(csvfile) 
    # create a list of headings from the first row of the csv file
    headings = next(csvreader)

# create a dictionary, where keys are Suburb/Town Name and values are number of occurences
# index 2 of the headings list are the suburbs
neighborhood_dict = df[headings[2]].value_counts().to_dict()

# make first letter uppercase eg St Kilda
neighborhood_dict = dict((k.title(), v) for k, v in neighborhood_dict.items())


# make neighborhood_list from neighborhood_dict
neighborhood_list=[]
for key, value in neighborhood_dict.items():
    temp = [key,value]
    neighborhood_list.append(temp)

# make dataframe from neighborhood_list
df = pd.DataFrame(neighborhood_list, columns=['Suburb','Count'])

print(df.to_string()) 

创建地图

import folium

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

world_map.choropleth(
        geo_data='vic.geojson',
        data=df,
        columns=['Suburb','Count'],
        key_on='feature.properties.Suburb_Name',
        fill_color='YlOrRd',
        fill_opacity=0.7,
        line_opacity=0.2,
        legend_name='Crime Rate in Victoria'
        )

world_map.save('index.html')

我都弄明白了。缺失值是灰色的,图例是用我选择的间隔定制的。清理 geojson,删除尾随的白色 space,并使所有郊区名称大写解决了很多问题。

Files are here

创建词典

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()

样式函数

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 地图

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)

色图

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')