简体   繁体   English

folium:使用底图和 pandas 创建等值线图 map

[英]folium: create a choropleth map using basemap and pandas

I am using folium to create a choropleth map of Los Angeles showing "frequency"(mydata) based on zipcode.我正在使用 folium 创建洛杉矶的 choropleth map,显示基于邮政编码的“频率”(mydata)。 I am following the documentation.我正在关注文档。 However, I'm not sure what to write for key_on and always shows ValueError: Cannot render objects with any missing geometries.但是,我不确定要为 key_on 写什么,并且总是显示 ValueError: Cannot render objects with any missing geometrys。 I am using the zipcode geojson from county of la open data ( https://data.lacounty.gov/Geospatial/ZIP-Codes/65v5-jw9f ).我正在使用洛杉矶开放数据县的邮政编码 geojson ( https://data.lacounty.gov/Geospatial/ZIP-Codes/65v5-jw9f )。 My csv file look like this:我的 csv 文件如下所示:

zipcode_frequency.csv邮政编码_频率.csv

According to tutorial of Choropleth maps( https://python-visualization.github.io/folium/quickstart.html#GeoJSON/TopoJSON-Overlays ), I wrote:根据 Choropleth 地图教程( https://python-visualization.github.io/folium/quickstart.html#GeoJSON/TopoJSON-Overlays ),我写道:

m = folium.Map([34.2012,-118.4662], zoom_start=10)
folium.GeoJson('ZIP Codes.geojson').add_to(m)
m
zipcode_data = pd.read_csv('zipcode_frequency.csv')

frequency.head()

m = folium.Map([34.2012,-118.4662], zoom_start=10)

folium.Choropleth(
    geo_data=folium.GeoJson('ZIP Codes.geojson'),
    name="choropleth",
    data=zipcode_data,
    columns=["zipcode", "occurance frequency"],
    fill_color='YlGn',
    fill_opacity=0.7,
    line_opacity=0.2,
    legend_name="frequency"
).add_to(m)

There are two issues that are not displayed: first, the zip code must be a string, not a number.不显示有两个问题:第一,zip code必须是字符串,不能是数字。 The reason is that the zip code in the geojson file is a string;原因是geojson文件中的zip编码是一个字符串; the second is that the connection key, in this geojson file, is under properties, so key_on='properties.zipcode' .第二个是此 geojson 文件中的连接密钥位于属性下,因此key_on='properties.zipcode' There are some places on the map where the colors are not displayed, but if all the data is available, it will show up fine. map有些地方没有显示colors,但是如果所有数据都可以显示就可以了。

import folium
import pandas as pd
import numpy as np
import io
import json

data = '''
zipcode "occurance frequency"
90027 34
90731 30
90011 28
90042 23
90033 22
90031 21
91342 18
90044 18
90026 18
91331 17
90012 17
91402 15
90065 15
90037 15
90032 15
90018 15
90016 15
91352 14
91601 12
'''

zipcode_data = pd.read_csv(io.StringIO(data), delim_whitespace=True)
zipcode_data['zipcode'] = zipcode_data['zipcode'].astype(str)

file_path = "./data/ZIP_Codes.geojson"

with open(file_path, 'r') as j:
     geo_json = json.loads(j.read())

m = folium.Map([34.2012,-118.4662], zoom_start=9)

folium.Choropleth(
    geo_data=geo_json,
    name="choropleth",
    data=zipcode_data,
    columns=["zipcode", "occurance frequency"],
    key_on='properties.zipcode',
    fill_color='YlGn',
    fill_opacity=0.7,
    line_opacity=0.2,
    legend_name="frequency"
).add_to(m)
m

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM