我想知道是否可以动态地更改GeoPandas GeoDataFrame中的哪一列显示在geoplot中。例如,如果我有一个包含代表不同日期的全局数据的不同列的GeoDataFrame,我怎么能有一个交互式滑块来允许我在地理绘图中显示特定日期的数据呢?我看到matplotlib.widgets有一个滑块,但我不知道如何将它应用于GeoDataFrame和geoplot。
发布于 2020-05-12 14:26:34
ipywidgets.interact装饰器可用于将函数快速转换为交互式小部件
from ipywidgets import interact
# plot some GeoDataFrame, e.g. states
@interact(x=states.columns)
def on_trait_change(x):
states.plot(x)

发布于 2021-08-18 08:07:01
我发现使用interact设置交互式小部件很方便,它结合了一个根据小部件中选择的参数修改数据/绘图的功能。为了演示,我实现了一个滑块小部件和一个下拉菜单小部件。根据您的用例,您可能只需要一个。
# import relevant modules
import geopandas as gpd
import ipywidgets
import numpy as np
# load a sample data set
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
# set seed for reproducability
np.random.seed(0)
# generate 3 artifical columns: random proportions of the gdp_md_est column (logarithmized)
for date in ['date1', 'date2', 'date3']:
world[date] = np.log(world.gdp_md_est*np.random.rand(len(world)))
# function defining what should happen if the user selects a specific date and continent
def on_trait_change(date, continent):
df=world[world['continent'] == continent] # sub set data
df.plot(f'date{date}') # to plot for example column'date2'
# generating the interactive plot with two widgets
interact(on_trait_change, date=ipywidgets.widgets.IntSlider(min=1, max=3, value=2), continent=list(set(world.continent)))https://stackoverflow.com/questions/61717810
复制相似问题