提问人:Vaibhav 提问时间:9/15/2023 更新时间:9/15/2023 访问量:42
Folium 地图适合屏幕,当滚动缩放过多时
Folium map fit to screen, when scrolled zooms too much
问:
我正在尝试使用 Foium 将美国地图调整到屏幕上。滚动浏览地图时,它会放大和缩小太多,这不清晰可见。我需要将地图调整到屏幕上才能查看它。
问题出在我得到的参数 zoom_start=5 在地图下方
我需要正确拟合地图,以便我可以正确地看到州名称。 这是我的示例代码:
from folium.features import DivIcon
import folium
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import geopandas as gpd
import requests
import folium, os
ky_counties = gpd.read_file('cb_2021_us_county_500k.zip')
lat_long_df=pd.read_excel('long_lat.xlsx')
ky_counties['fips'] = ky_counties['GEOID']
lat_long_df['fips'] = lat_long_df['fips'].astype(str).str.zfill(5)
ky_counties = (ky_counties.merge(lat_long_df, how = 'left', on = 'fips'))
df=pd.read_excel('plot_data.xlsx')
df['fips'] = df['FIPS'].astype(str).str.zfill(5)
df_map = (ky_counties.merge(df, how = 'left', on = 'fips')
.rename(columns = {'incidence' : 'Incidence Rate',
'hcp_cnt' : '# of HCPs',
'NAME' : 'County Name',
'STATE_NAME':'State'}))
new_df_map= df_map.copy()
df_map['# of HCPs'] = df_map['# of HCPs'].fillna('-')
import matplotlib
def colors2cmap(*args, name=None):
cmap_data = [matplotlib.colors.to_hex(c) for c in args]
cmap = matplotlib.colors.LinearSegmentedColormap.from_list(name, cmap_data)
return cmap
color_list=colors2cmap("#e1f2f2", "#A8DCDC", "#115F5F", name='color_list')
country_geo = os.path.join('us-states.json')
from branca import colormap as cm
m = folium.Map(location=[40, -96], zoom_start=4) #zoom_start = 5, max_zoom = 5
m.choropleth(
geo_data=country_geo,
name='choropleth',
data=df_map,
columns=['State', 'Incidence Rate'],
key_on='feature.properties.name',
fill_color='YlGn',
fill_opacity=0.6, #.7
line_opacity=0.1, #.2
legend_name='Test'
)
plot_ll=new_df_map.groupby(by=['STUSPS','State'], as_index=False)['# of HCPs'].sum()
plot_ll['# of HCPs']=plot_ll['# of HCPs'].astype(int)
plot_ll=plot_ll[plot_ll['# of HCPs']>=1]
st_lat_lng=pd.read_csv('statelatlong.csv')
plot_ll=plot_ll.merge(st_lat_lng,how = 'inner', left_on='STUSPS', right_on='State')
# plot_ll=new_df_map.groupby(by=['STUSPS','State','lat','lng'], as_index=False)['# of HCPs'].sum()
locations=list(zip(plot_ll.lat, plot_ll.lng))
hcp_cnt_disp=list(plot_ll['# of HCPs'])
state_name=list(plot_ll['City'])
linear = cm.LinearColormap(["yellow", "yellow"], vmin=plot_ll['# of HCPs'].min(), vmax=plot_ll['# of HCPs'].max())
for i in range(len(locations)):
folium.Marker(locations[i], icon=DivIcon(
# icon_size=(150,36),
icon_anchor=(5,7),
html='<div style="font-size: 7pt; color : black; text-align: center">%s</div>'%hcp_cnt_disp[i],
)).add_to(m)
# m.add_child(folium.CircleMarker(p2, radius=15))
m.add_child(folium.CircleMarker(location=locations[i],radius=8,fill=True, color = linear(hcp_cnt_disp[i]),
opacity=0.4, fill_opacity=.3,
tooltip='{}: {}'.format(",County Name: <b>"+state_name[i]+"</b><br>", "HCP Count: <b>"+str(hcp_cnt_disp[i])+"</b>")))
folium.TileLayer("CartoDB positron", show=False).add_to(m)
folium.LayerControl().add_to(m)
m.save('new_base_map.html')
请帮忙。TIA!
答: 暂无答案
评论
zoom_start
int
zoom_start=4