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

时间:2018-10-21 02:30:16

标签: python dataframe geojson choropleth folium

我的问题是郊区没有在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')

Dataframe Image

Legend

Map Image

1 个答案:

答案 0 :(得分:1)

我都明白了。缺少的值显示为灰色,并且按我选择的间隔自定义图例。清理geojson,删除尾随的空白以及使所有郊区名称都变成大写字母解决了很多问题。

Files are here

Demo image

创建字典

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,
    }

福利地图

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