gpt4 book ai didi

python - 使用交互式平移和缩放制作 Altair map

转载 作者:行者123 更新时间:2023-12-03 14:54:12 31 4
gpt4 key购买 nike

我能够在鼠标悬停时生成带有工具提示的世界等值线 map ,然后单击选择以使未选择的国家变灰。我希望能够点击一个国家并让 map 平移和放大。我已经弄清楚如何为每个国家使用经度/纬度质心并手动更改比例/经度/纬度以进行平移和缩放,但我我正在努力如何通过选择方法更改 lon/lat,以便用户可以单击国家/地区进行放大(此外,我需要能够以某种方式放大回默认的完整 View )。是否可以以某种方式使用 transform_calculate或者给 lon/lat 赋值?谢谢。
( map 数据来自 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

以及手动平移/缩放方法:
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]
)

数据框如下所示:

enter image description here

最佳答案

正如评论中所指出的,目前这在 Atair/Vegalite 中是不可能的,并且正在 this issue 中进行跟踪。 .

关于python - 使用交互式平移和缩放制作 Altair map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60493277/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com