问题
I'm working on GeoViews and I'd like to know if we could have a slider as an input for the choropleth map in GeoViews.
I've another variable in gdf, which is year. Is it possible to have a slider to show year wise Total_Crimes?
Edit: Added more information:
gdf dataframe has following variables.
beat_num Year Total_Crimes beat district sector geometry
111 2012 1449 1 01 1 POLYGON ((-87.62451050462798 41.88829675314376...
111 2013 1645 1 01 1 POLYGON ((-87.62451050462798 41.88829675314376...
111 2014 1636 1 01 1 POLYGON ((-87.62451050462798 41.88829675314376...
111 2015 1642 1 01 1 POLYGON ((-87.62451050462798 41.88829675314376...
111 2016 1836 1 01 1 POLYGON ((-87.62451050462798 41.88829675314376...
I've essentially grouped gdf by beat_num and Year and found out Total_Crimes for each group. Datatypes of gdf:
beat_num int64
Year int64
Total_Crimes int64
beat object
district object
sector object
geometry object
Entire Code:
import geopandas as gpd
import holoviews as hv
import geoviews as gv
import geoviews.tile_sources as gts
hv.extension('bokeh')
geometries = gpd.read_file('geo_export_3b3b25c2-a600-40c3-a663-2f7ad8dc2b9c.shp')
#Reading the shape file for each beat_num.
geometries['beat_num']=geometries['beat_num'].apply(int)
#Converted the beat_num to integers
gdf = gpd.GeoDataFrame(pd.merge(ca_df, geometries))
#dataframe ca_df has total crimes for each beat_num and each year and merged it with geometries to get shape for each beat_num.
plot_opts = dict(tools=['hover'], width=750, height=700, color_index='Total_Crimes',
colorbar=True, toolbar='above', xaxis=None, yaxis=None)
gts.ESRI *gv.Polygons(gdf, vdims=['beat_num', 'Total_Crimes'], label='Chicago Crime Data').opts(plot=plot_opts,style=dict(alpha=0.7))
Output:
回答1:
Yes, it's always possible to have a slider in GeoViews if you want one! I can't quite tell what you're doing from that code snippet, but I'm guessing that gdf is a GeoPandas dataframe, which would presumably have latitude and longitude as key dimensions. To get a slider, you then need to add an additional key dimension ('kdim')for the year. You may also need to aggregate the data by year, if it's not already aggregated in that way. There are examples on pyviz.org and geoviews.org that should get you started, or you can post a more complete example.
来源:https://stackoverflow.com/questions/53272108/geoviews-add-a-slider-to-choropleth-map