foo*_*con 6 python maps geopandas altair
我能够在鼠标悬停时生成带有工具提示的世界等值线地图,然后单击选择以将未选择的国家/地区变灰。我希望能够点击一个国家并让地图平移和放大。我已经弄清楚如何为每个国家使用经度/纬度质心并手动更改比例/经度/纬度以进行平移和缩放,但我我正在努力如何通过选择方法更改 lon/lat,以便用户可以单击国家/地区进行放大(此外,我需要能够以某种方式放大回默认的完整视图)。是否有可能以某种方式使用transform_calculate或某些东西为 lon/lat 赋值?谢谢。(地图数据来自naturalearthdata)
import altair as alt
import geopandas as gpd
import json
import numpy as np
world_shp = gpd.read_file('data_world/ne_110m_admin_0_countries.shp')[['ADMIN', 'geometry']]
world_shp.rename(columns={'ADMIN': 'Country'}, inplace=True)
world_shp = world_shp.drop(159) # remove antarctica
world_shp.sort_values(by='Country')
# Add centroids
world_shp['centroid_lon'] = world_shp['geometry'].centroid.x
world_shp['centroid_lat'] = world_shp['geometry'].centroid.y
# Add column to map_df of some dummy data to plot
dummy = np.random.randint(10, 1000, len(world_shp))
world_shp['Cases'] = dummy
# Setup data
world_json = json.loads(world_shp.to_json())
world_data = alt.Data(values=world_json['features'])
# Plotting:
selection = alt.selection_single(fields=['properties.Cases'])
color = alt.condition(
selection,
alt.Color('properties.Cases',
type='quantitative',
scale=alt.Scale(scheme='bluegreen')),
alt.value('lightgray')
)
# Add Choropleth Layer
world = alt.Chart(world_data).mark_geoshape().encode(
color=color,
tooltip=['properties.Country:O', 'properties.Cases:Q']
).properties(
width=600,
height=400
).add_selection(
selection
)
# Add top layer to mark boundaries
boundaries = alt.Chart(world_data, title='Title Here').mark_geoshape(
stroke='white',
strokeWidth=1,
fill=None
)
world + boundaries
Run Code Online (Sandbox Code Playgroud)
以及手动平移/缩放方法:
centroid = 'China'
lon = world_shp[world_shp['Country'] == centroid]['centroid_lon'].iloc[0]
lat = world_shp[world_shp['Country'] == centroid]['centroid_lat'].iloc[0]
world.project(
scale=250,
center=[lon, lat]
)
Run Code Online (Sandbox Code Playgroud)
数据框如下所示: