[英]Choropleth map - no color differentiation
我正在尝试根据多伦多社区负担不起的住房价格创建一个等值线 map。 我在网上找到了一个数据集,我在 aa csv 中读取了该数据集,然后将数据调整为适当的列,然后尝试创建我的 map。
我的数据和条件代码如下所示:
df = pd.read_csv('torontodata.csv')
df = df.transpose()
df.columns = df.iloc[4]
df.drop(['_id' , 'Category', 'Topic', 'Data Source', 'Characteristic'], axis=0, inplace=True)
df = df.reset_index()
df = pd.DataFrame(df[['index', 'Rate of unaffordable housing']])
df.drop(df.loc[df['index']=='City of Toronto'].index, inplace=True)
df['Rate of unaffordable housing'] = df['Rate of unaffordable housing'].astype(float)
我相当有信心这样做是正确的,因为它完全按照应有的方式返回: dataframe 。 我认为错误必须在 choropleth map 的 'key_on' 参数中,但我不知道我做错了什么。 我检查了原始 geojson 文件,似乎我有正确的路径。
!wget --quiet https://raw.githubusercontent.com/codeforamerica/click_that_hood/master/public/data/toronto.geojson
print('GeoJSON file downloaded!')
toronto_geo = r'toronto_crs84.geojson' # geojson file
address = 'Toronto, ON'
geolocator = Nominatim(user_agent="foursquare_agent")
location = geolocator.geocode(address)
latitude = location.latitude
longitude = location.longitude
print(latitude, longitude)
toronto_map = folium.Map(location=[latitude, longitude], zoom_start=13) # generate map centred around Toronto
threshold_scale = np.linspace(df['Rate of unaffordable housing'].min(),
df['Rate of unaffordable housing'].max(),
6, dtype=int)
threshold_scale = threshold_scale.tolist() # change the numpy array to a list
threshold_scale[-1] = threshold_scale[-1] + 1 # make sure that the last value of the list is greater than the maximum immigration
# display map
toronto_map.choropleth(
geo_data=toronto_geo,
data=df,
columns=['index', 'Rate of unaffordable housing'],
key_on='feature.properties.name',
threshold_scale=threshold_scale,
fill_color='YlOrRd',
fill_opacity=0.3,
line_opacity=0.2,
legend_name='Affordable housing in Canada'
)
toronto_map
这不会返回错误,但我的 map 都是一种颜色值。 我被这个问题困扰了一整天。 我什至尝试使用不同的 json 文件,但我遇到了同样的问题。 任何帮助,将不胜感激。
https://raw.githubusercontent.com/codeforamerica/click_that_hood/master/public/data/toronto.geojson
您尝试读取的数据是 JSON 文件,而不是 CSV 文件。
import json
with open('toronto.geojson.txt', 'rb') as f:
data = json.load(f)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.