Python-Choropleth贴图未在输出中显示颜色变化
作者:互联网
即使在choropleth方法与geo_data链接并且数据框与choropleth方法中的data参数链接之后,我的输出映射中也没有任何颜色变化.
我正确地提供了“ key_on”参数,正确地提供了“ columns”参数.我已经从数据框中删除了所有NULL值.
import pandas as pd
from pandas import read_csv
import folium
import os
import webbrowser
crimes = read_csv('Dataframe.csv',error_bad_lines=False)
vis = os.path.join('Community_Areas.geojson')
m = folium.Map(location = [41.878113, -87.629799], zoom_start = 10, tiles = "cartodbpositron")
m.choropleth(geo_data=vis, data = crimes, columns = ['Community Area', 'count'], fill_color = 'YlGn', key_on = 'feature.properties.area_numbe')
folium.LayerControl().add_to(m)
m.save('map.html')
webbrowser.open(filepath)
我期望彩色的choropleth贴图,但是实际输出是完全灰色的.我将在下面的链接中添加代码,数据和输出.
代码链接:https://github.com/rahul0070/Stackoverflow_question_data
解决方法:
读取数据后,我发现您没有将Community Area列的数据类型转换为字符串类型,因为geojson文件包含字符串形式的密钥.因此,键和列的数据类型都应该匹配.
这是您答案的完整解决方案
# importing libraries
import pandas as pd
from pandas import read_csv
import folium
import os
import webbrowser
# read the data
crimes = read_csv('Dataframe.csv',error_bad_lines=False)
# convert float to int then to string
crimes['Community Area'] = crimes['Community Area'].astype('int').astype('str')
# choropleth map
vis = 'Community_Areas.geojson'
m = folium.Map(location = [41.878113, -87.629799], zoom_start = 10, tiles = "cartodbpositron")
m.choropleth(geo_data=vis, data = crimes, columns = ['Community Area', 'count'], fill_color = 'YlGn', key_on = 'feature.properties.area_num_1')
folium.LayerControl().add_to(m)
m.save('map.html')
webbrowser.open('map.html')
如果您难以理解代码的任何部分,请发表评论.对于key_on参数,您也可以尝试feature.properties.area_numbe
标签:geojson,python,pandas,choropleth,folium 来源: https://codeday.me/bug/20191013/1909101.html