地理视图:将滑块添加到Choropleth地图 [英] Geoviews: Add a slider to choropleth map
问题描述
我正在研究GeoViews,我想知道是否可以在GeoViews中将滑块作为输入来作为Choropleth映射的输入.
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.
我在gdf中还有另一个变量,即year.可以使用滑块显示按年份显示的Total_Crimes吗?
I've another variable in gdf, which is year. Is it possible to have a slider to show year wise Total_Crimes?
添加了更多信息:
gdf数据框具有以下变量.
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...
我基本上将gdf按 beat_num 和 Year 分组,并找出每个组的Total_Crimes. gdf的数据类型:
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
完整代码:
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))
输出:
推荐答案
是的,如果需要,总是可以在GeoViews中使用一个滑块!我不能从代码片段中清楚地知道您在做什么,但是我猜测gdf是一个GeoPandas数据框,大概会将纬度和经度作为关键维度.要获得滑块,则需要为年份添加其他关键尺寸("kdim").如果尚未以这种方式汇总数据,则可能还需要按年份汇总数据. pyviz.org和geoviews.org上有一些示例可以帮助您入门,或者可以发布更完整的示例.
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.
这篇关于地理视图:将滑块添加到Choropleth地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!