如何解决如何在预定义的分位数上更改folium python中的style_function以更改特征的不透明度?
这是一个例子
import pandas as pd
import os
import numpy as np
import geopandas as gpd
import matplotlib.pyplot as plt
import seaborn as sns
import json
import requests
import folium as f
import geojson
import mapclassify as mc
url = 'https://raw.githubusercontent.com/python-visualization/folium/master/examples/data'
county_data = f'{url}/us_county_data.csv'
county_geo = f'{url}/us_counties_20m_topo.json'
# read the data
df = pd.read_csv(county_data,na_values=[' '])
# define the quantiles,teh vector is then sored as quantiles.yb
quantiles = mc.Quantiles(df['Unemployed_2011'],k=5)
colorscale = branca.colormap.linear.YlOrRd_09.scale(0,50e3)
employed_series = df.set_index('FIPS_Code')['Employed_2011']
def style_function(feature):
employed = employed_series.get(int(feature['id'][-5:]),None)
return {
'fillOpacity': quantiles.yb,'weight': 0,'fillColor': '#black' if employed is None else colorscale(employed)
}
m = f.Map(
location=[48,-102],tiles='cartodbpositron',zoom_start=3
)
f.TopoJson(
json.loads(requests.get(county_geo).text),'objects.us_counties_20m',style_function=style_function
).add_to(m)
m
这使我出错;
TypeError: Object of type ndarray is not JSON serializable
基本上,我想基于具有权重的数组来更改多边形的不透明度,该权重是根据数据集中的变量之一计算得出的。我猜想lambda可以以某种方式使用,但是我的努力都不起作用。有人可以帮忙吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。