使用日期滑块创建地图以显示值的进展

Posted

技术标签:

【中文标题】使用日期滑块创建地图以显示值的进展【英文标题】:Creating map with date slider to show the progression of values 【发布时间】:2020-11-04 00:34:38 【问题描述】:

我正在尝试使用日期滑块创建一张地图,显示全球范围内的渐进式锁定措施。到目前为止,我能够为静态日期创建地图,但我对滑块的尝试不起作用,因为它似乎只接受整数或浮点数而不是日期范围 (Date_slider = pnw.IntSlider(start=0 ,end=365,value=0)

基本上,我想创建在这里找到的地图:https://www.bsg.ox.ac.uk/research/research-projects/coronavirus-government-response-tracker

import pandas as pd
import geopandas as gpd
import json
import matplotlib as mpl
import pylab as plt
from bokeh.io import output_file, show, output_notebook, export_png
from bokeh.models import ColumnDataSource, GeoJSONDataSource, LinearColorMapper, ColorBar
from bokeh.plotting import figure
from bokeh.palettes import brewer
import panel as pn
import panel.widgets as pnw
import descartes
output_notebook()
pn.extension()

读取形状文件

shapefile = '/Users/sam/Desktop/ne_110m_admin_0_countries/ne_110m_admin_0_countries.shp'
gdf = gpd.read_file(shapefile)[['ADMIN', 'ADM0_A3', 'geometry']]
gdf.columns = ['country', 'country_code', 'geometry']
gdf = gdf.drop(gdf.index[159])

读取数据

def get_dataset(name,key=None,Date=None):

df = pd.read_csv(r'/Users/sam/Desktop/Big Data Project/covid-stringency-index-2.csv')
        if Date is not None:
            df = df[df['Date'] == Date]
        #Merge dataframes gdf and df_2016.
        if key is None:
            key = df.columns[2]
        merged = gdf.merge(df, left_on = 'country', right_on = 'Entity', how = 'left')
        merged[key] = merged[key].fillna(0)    
        return merged, key

使用 matplotlib 绘图

datasetname='Lockdown Measures'
df,key = get_dataset(datasetname, Date='Jul 10, 2020')
fig, ax = plt.subplots(1, figsize=(14, 8))
df.plot(column=key, cmap='OrRd', linewidth=1.0, ax=ax, edgecolor='Black')
ax.axis('off')
ax.set_title('%s Jul 10, 2020' %datasetname, fontsize=25)
plt.tight_layout()
plt.savefig('test_map.png',dpi=150)

输出:https://i.stack.imgur.com/6GrbF.png

散景绘图

def get_geodatasource(gdf):    
    """Get getjsondatasource from geopandas object"""
    json_data = json.dumps(json.loads(gdf.to_json()))
    return GeoJSONDataSource(geojson = json_data)


def map_dash():
    """Map dashboard"""
    
    from bokeh.models.widgets import DataTable
    map_pane = pn.pane.Bokeh(width=600)
    data_select = pnw.Select(name='dataset',options=list(df))
    Date_slider = pnw.IntSlider(start=0,end=365,value=0)
    def update_map(event):
        gdf,key = get_dataset(name=data_select.value,Date=Date_slider.value)        
        map_pane.object = bokeh_plot_map(gdf, key)        
        return
    Date_slider.param.watch(update_map,'value')
    Date_slider.param.trigger('value')
    data_select.param.watch(update_map,'value')
    app = pn.Column(pn.Row(data_select,Date_slider),map_pane)
    return app

app = map_dash()
app

【问题讨论】:

如果你想使用ipywidgets,看看这个答案:***.com/a/50506926/6256241 【参考方案1】:

你必须使用 pn.widgets.DateRangeSlider(...)

https://panel.holoviz.org/reference/widgets/DateRangeSlider.html#widgets-gallery-daterangeslider

【讨论】:

以上是关于使用日期滑块创建地图以显示值的进展的主要内容,如果未能解决你的问题,请参考以下文章

Bootstrap datepicker 显示的日期格式与值的格式不同

2019/08/24最新进展

如何以编程方式创建默认滑块值?

显示多个滑块值的 UILabel

创建修改先前值的地图值

创建滑块以动态显示图像中两条线的距离