我正在尝试绘制一个具有每个县关联值的美国县地图。我遵循了folium文档,但在计算机不崩溃的情况下无法渲染地图。
最初,我能够使用具有相同大小的dataframe(130KB)的Geojson文件渲染一个Choropleth贴图。当我切换到Topojson时,文件崩溃。
# loading the json data
with open ('/Users/Erudition/Desktop/independent_projects/cms/PGYR17/county_files/us_counties_topjson.json') as json_file:
top_json = json.load(json_file)
# Upload the data
cms = pd.read_csv('cms_2017_cleaned.csv',
usecols = ['Recipient_FIPS_Zip_Code', 'Total_Amount_of_Payment_USDollars','Pop_est2018'],
dtype = {'Recipient_FIPS_Zip_Code' : 'category'})
# Add a column based on the amount of money per capita
cms['Payment_amount_per_capita'] = cms['Total_Amount_of_Payment_USDollars']/cms['Pop_est2018']
# Use Groupby function and reset index
cms_county = pd.DataFrame(cms.groupby(['Recipient_FIPS_Zip_Code'])['Payment_amount_per_capita'].sum())
cms_county.reset_index(inplace = True)
# Make color map
color_map = cm.LinearColormap(
colors = ['#FFFFCC','#CCFF99','#66FF66','#00FF00','#00CC00', '#006600', '#003300'],
index = [0.0,0.0407,0.1622,0.4416,1.2768,3.7663,325],
vmin= 0,
vmax= 326)
# Style function for the map
def style_function(feature):
amount = cms_county_dict.get(feature['properties']['GEOID'], None)
return {
'fillOpacity': 0.5,
'weight': 0,
'fillColor': 'grey' if amount is None else color_map(amount)
}
# Make the map
m = folium.Map(
location=[48, -102],
tiles='cartodbpositron',
zoom_start=3
)
# Topojson
folium.TopoJson(
top_json,
'objects.tl_2018_us_county',
style_function=style_function
).add_to(m)
我原本希望看到一个波形图,但是我的计算机发热并崩溃了。我的topjson文件格式化的方式可能是问题所在,但我不确定。