#!/usr/bin/env python # coding: utf-8 # # Time Series Picker # Click on a map of geo data, get a time series plot at the nearest location # In[1]: import panel as pn import xarray as xr import holoviews as hv import hvplot.xarray pn.extension() ds = xr.tutorial.open_dataset('air_temperature') ds = ds.assign_coords(lon=(((ds.lon + 180) % 360) - 180), lat=ds.lat*1.0) image, select = ds.hvplot('lon', 'lat', geo=True, cmap='viridis', widgets={'time':pn.widgets.Select}) stream = hv.streams.Tap(source=image.object, x=-88, y=40) def timeseries(x, y): return ds.sel(lon=x, lat=y, method='nearest').hvplot('time', grid=True) timeseries_plot = pn.bind(timeseries, x=stream.param.x, y=stream.param.y) pn.Column(pn.Row(select), pn.Row(image, timeseries_plot)) # In[15]: import pandas as pd import hvplot.pandas df = pd.DataFrame({'lon':[-88.0], 'lat':[40], 'station':[0]}) station_plot = df.hvplot.points(x='lon', y='lat', geo=True, by='station', size=15, frame_height=200) # In[16]: station_plot # In[17]: image.object * station_plot # In[ ]: image # ## Picker with time series comparison (multiple time series) # In[ ]: import pandas as pd import hvplot.pandas # In[ ]: sta = 0 ts = {} # In[ ]: def timeseries(x, y): global sta, ts sta += 1 sta_str = f'sta {sta:02d}' ts[sta_str] = ds.sel(lon=x, lat=y, method='nearest')['air'] df = pd.DataFrame(index=ds.time ,data=ts) return df.hvplot(grid=True) # In[ ]: multi_timeseries = pn.bind(timeseries, x=stream.param.x, y=stream.param.y, watch=False) # In[ ]: clear_button = pn.widgets.Button(name='Clear time series data', button_type='success') # In[ ]: def clear(event): global sta,ts sta = 0 ts = {} # In[ ]: pn.bind(clear, clear_button) # In[ ]: pn.Column(pn.Row(image,multi_timeseries), pn.Row(select, clear_button))