Click on a map of geo data, get a time series plot at the nearest location
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))
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)
station_plot
image.object * station_plot
image
import pandas as pd
import hvplot.pandas
sta = 0
ts = {}
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)
multi_timeseries = pn.bind(timeseries, x=stream.param.x, y=stream.param.y, watch=False)
clear_button = pn.widgets.Button(name='Clear time series data', button_type='success')
def clear(event):
global sta,ts
sta = 0
ts = {}
pn.bind(clear, clear_button)
pn.Column(pn.Row(image,multi_timeseries), pn.Row(select, clear_button))