In http://geo.holoviews.org/user_guide/Geometries.html the line
gv.Polygons(world, vdims='pop_est')
yields a world map with polygons shaded by population, and I think what it's somehow doing is getting lat/long as the kdims but I don't understand how this works. If I try to specify 'geometry' as the kdims I get an error "Expected two kdims to use GeoDataFrame, found 1."
My end goal is to be able specify the kdims so that I can also specify a "year" parameter as the kdims and get a scrollbar on the map.
Answer
The behavior you are observing with gv.Polygons(world, vdims='pop_est')
in GeoViews leverages GeoPandas' support for geographic data, where geometry
is implicitly understood as the spatial dimension, and additional columns (like pop_est
) are treated as value dimensions (vdims
). Here's an explanation of how kdims
and vdims
work and how you can adapt them to achieve your goal of adding a "year" parameter with a slider:
Understanding kdims
and vdims
in GeoViews
-
kdims
(Key Dimensions):- Represent the independent variables of your data.
- For geographic data, GeoViews automatically infers
kdims
as latitude (y
) and longitude (x
) when using a GeoDataFrame, without requiring you to explicitly specify them.
-
vdims
(Value Dimensions):- Represent dependent variables (attributes) used to color or scale your geometries (e.g.,
pop_est
in your case).
- Represent dependent variables (attributes) used to color or scale your geometries (e.g.,
Why does specifying 'geometry'
as kdims
fail?
geometry
in a GeoDataFrame is treated as a column containing spatial data. GeoViews expects the spatial dimensions to be two separate kdims
: x
(longitude) and y
(latitude). If you specify kdims=['geometry']
, GeoViews cannot map it to the expected latitude and longitude dimensions, hence the error.
Adding a "year" Dimension with a Slider
To achieve your goal of adding a "year" parameter, you need to structure your data such that the year
becomes an additional kdims
. Here's how you can do it:
Steps:
-
Prepare Your Data:
- Ensure your data is a GeoDataFrame with a
geometry
column for spatial data and additional columns foryear
and the variable of interest (e.g.,pop_est
).
- Ensure your data is a GeoDataFrame with a
-
Use HoloViews
HoloMap
for Interactive Widgets:- Convert your data into a format where each unique
year
corresponds to a separate set of polygons. - Use
HoloMap
to map theyear
dimension to a slider.
- Convert your data into a format where each unique
Example Code:
import geopandas as gpd
import geoviews as gv
import holoviews as hv
from holoviews import opts
gv.extension('bokeh')
# Example GeoDataFrame
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
# Add a dummy 'year' column for demonstration (e.g., duplicate data for multiple years)
years = [2000, 2010, 2020]
world_repeated = gpd.GeoDataFrame(
pd.concat([world.assign(year=year) for year in years], ignore_index=True)
)
# Create a dictionary of HoloMaps, mapping 'year' to polygons
polygons_by_year = {
year: gv.Polygons(world_repeated[world_repeated['year'] == year], vdims='pop_est')
for year in years
}
# Combine into a HoloMap
polygons_hmap = hv.HoloMap(polygons_by_year, kdims='year')
# Customize plot options
polygons_hmap.opts(
opts.Polygons(cmap='Viridis', colorbar=True, tools=['hover'], width=800, height=500)
)
Key Notes:
-
Why Use
HoloMap
?HoloMap
allows you to add interactive widgets like sliders. Thekdims
parameter inHoloMap
defines the dimensions controlled by the widgets.
-
What Happens Behind the Scenes?
- GeoViews recognizes the
geometry
column for spatial data and treats other columns (e.g.,year
andpop_est
) as dimensions. Whenyear
is specified as akdims
, it creates a mapping between theyear
and its corresponding polygons.
- GeoViews recognizes the
-
Widget Interactivity:
- With
HoloMap
, a slider foryear
will automatically be created when you render the map in a Jupyter Notebook or other interactive environment.
- With
Output:
The map will display polygons shaded by pop_est
, and you can use the slider to switch between years. This approach can be extended to other dimensions (e.g., additional variables or categories).