python - 等值线图未显示
问题描述
我是 Python 数据科学的新手,这是我在这里的第一个帮助请求(然后为一些错误提前道歉)。需要您的支持才能了解为什么此(基于简单数据框的)等值线图未显示。阅读了很多关于该论点的讨论,然后我验证了所有主要内容:str 等中的地区名称和 NAAM(在 geojson 中) - 但我仍然卡住了,我看不到地图(只有图例)。让我知道是否需要更多信息,您可以在下面找到代码:在 [9] 中:
df_clo=dfrc.groupby(['District']).mean()
df_clo.reset_index(inplace=True)
df_clo=df_clo[['District','Rent']]
df_clo['District'] = df_clo['District'].str.upper()
df_clo
出[9]:
District Rent
0 BINNENSTAD 1792.281250
1 NOORDOOST 1763.558824
2 OOST 1739.186047
3 ZUID 1562.142857
4 ZUIDWEST 1397.689655
在[10]中:
latitude = 52.09083
longitude = 5.12222
print('The geograpical coordinate of Utrecht are {}, {}.'.format(latitude, longitude))# create map of Utrecht using latitude and longitude values
utrecht_geo = r'https://raw.githubusercontent.com/umbesallfi/Coursera_Capstone/master/wijk_.geojson'
# create a numpy array of length 6 and has linear spacing from the minium total immigration to the maximum total immigration
threshold_scale = np.linspace(df_clo['Rent'].min(),
df_clo['Rent'].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
# let Folium determine the scale.
map_utr = folium.Map(location=[latitude, longitude], zoom_start=2, tiles='Mapbox Bright')
map_utr.choropleth(
geo_data=utrecht_geo,
data=df_clo,
columns=['District', 'Rent'],
key_on='feature.properties.NAAM',
threshold_scale=threshold_scale,
fill_color='YlOrRd',
fill_opacity=0.7,
line_opacity=0.2,
legend_name='Price in Utrecht by Wijk',
reset=True
)
map_utr
解决方案
地区名称不会以正楷存储在您的wijk_.geojson
文件中。因此,删除这一行就足够了:
df_clo['District'] = df_clo['District'].str.upper()
我的代码:
import folium
import pandas as pd
import numpy as np
m = folium.Map(location=[52.09083, 5.12222],
zoom_start=12,
control_scale=True)
df_clo = pd.DataFrame({'District':['Binnenstad','Noordoost','Oost','Zuid','Zuidwest'],
'Rent':[1792.281250,
1763.558824,
1739.186047,
1562.142857,
1397.689655]})
threshold_scale = np.linspace(df_clo['Rent'].min(),
df_clo['Rent'].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
utrecht_geo = 'wijk_.geojson'
folium.Choropleth(geo_data=utrecht_geo,
name='choropleth',
data=df_clo,
columns=['District', 'Rent'],
key_on='feature.properties.NAAM',
threshold_scale=threshold_scale,
fill_color='YlOrRd',
fill_opacity=0.7,
line_opacity=0.2,
legend_name='Price in Utrecht by Wijk',).add_to(m)
folium.LayerControl().add_to(m)
m
返回此地图:
推荐阅读
- python - 多次实例化时异步锁中的事件循环错误
- linux-kernel - 互斥锁是否使等待线程在 POSIX API 中进入睡眠状态?
- python - 如何将具有分组依据的列的最大值和最小值提取到嵌套字典中
- mysql - 为什么必须使用“GMT-6:00”更正 MySQL JDBC 中的时间戳?
- angular - 带有 HTTP 的 Rxjs - 获取第一页,然后是其余页面
- python - Python将列表中的部分字符串相互比较
- c++ - 成员变量与函数概念检查
- php - 无法运行 websocket 服务器
- neo4j - 如何定义枚举类型 Neo4j?
- ios - 自定义 TextField 在 SwiftUI 代码中的 VStack 中不起作用