【问题标题】:Python - Folium Choropleth Map - colors incorrectPython - Folium Choropleth Map - 颜色不正确
【发布时间】:2018-10-21 02:30:16
【问题描述】:

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

【问题讨论】:

    标签: python dataframe geojson choropleth folium


    【解决方案1】:

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

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

    大叶地图

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

    【讨论】:

    • 作为弗兰克斯顿的纳税人,感谢您将弗兰克斯顿放在地图上 :)
    猜你喜欢
    • 1970-01-01
    • 2020-09-01
    • 1970-01-01
    • 2020-07-09
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 2022-12-18
    • 1970-01-01
    相关资源
    最近更新 更多