Working Example:
#To load and manipulate the data
import xarray as xr
import os
import glob
import numpy as np
import math
import pandas as pd
from calendar import monthrange
import xesmf as xe
import dask
#For Viz
import matplotlib.pyplot as plt
import param
import datetime as dt
import panel as pn
import hvplot.xarray # noqa
import holoviews as hv
import panel.widgets as pnw
import cartopy.crs as ccrs
import geoviews as gv
ds = xr.tutorial.open_dataset('air_temperature')
#create a new variable to have something to change
ds['air_C'] = ds['air']-273
class GliderParams(param.Parameterized):
'''Class containing the methods for dataset and variable selection'''
surface_var = param.Selector(['air','air_C'], default = 'air', label = 'Surface Field')
#@param.depends('surface_var')
def imaging(surface_var):
image,select = ds[surface_var].hvplot(groupby='time',widget_location = 'bottom',
cmap='RdBu_r',ylim=[15,80],xlim = [200,360],
crs=ccrs.PlateCarree(),projection=ccrs.PlateCarree())
#coastline=True,
stream = hv.streams.Tap(source=image.object, x=0, y=0)
timeseries = ds[surface_var].interactive.sel(lon=stream.param.x, lat=stream.param.y, method='nearest').hvplot('time')
return pn.Column(image, select,timeseries.dmap())
gp = GliderParams()
pn.Row(pn.Param(gp, name=''),pn.bind(imaging,gp.param.surface_var))
To this, I’m trying to add two things:
- A title to the timeseries plot. I modified the imaging function to:
def imaging(surface_var):
image,select = ds[surface_var].hvplot(groupby='time',widget_location = 'bottom',
cmap='RdBu_r',ylim=[15,80],xlim = [200,360],
crs=ccrs.PlateCarree(),projection=ccrs.PlateCarree())
#coastline=True,
stream = hv.streams.Tap(source=image.object, x=0, y=0)
timeseries = ds[surface_var].interactive.sel(lon=stream.param.x, lat=stream.param.y, method='nearest').hvplot('time').apply.opts(title=f"{stream.x}, {stream.y}")
return pn.Column(image, select,timeseries.dmap())
Which returns: 0,0 as the title (the default for stream) but doesn’t update.
- I’m trying to add coastlines. In my example, not sure how to do this after the bind so I tried the following:
def imaging(surface_var):
image,select = ds[surface_var].hvplot(groupby='time',widget_location = 'bottom',
cmap='RdBu_r',ylim=[15,80],xlim = [200,360],
crs=ccrs.PlateCarree(),projection=ccrs.PlateCarree())
#coastline=True,
stream = hv.streams.Tap(source=image.object, x=0, y=0)
timeseries = ds[surface_var].interactive.sel(lon=stream.param.x, lat=stream.param.y, method='nearest').hvplot('time').apply.opts(title=f"{stream.x}, {stream.y}")
return pn.Column(image* gv.features.coastline(), select,timeseries.dmap())
And got:
AttributeError Traceback (most recent call last)
in
1 gp = GliderParams()
----> 2 pn.Row(pn.Param(gp, name=‘’),pn.bind(imaging,gp.param.surface_var))
/srv/conda/envs/notebook/lib/python3.8/site-packages/panel/layout/base.py in init(self, *objects, **params)
359 "as positional arguments or as a keyword, "
360 “not both.” % type(self).name)
→ 361 params[‘objects’] = [panel(pane) for pane in objects]
362 elif ‘objects’ in params:
363 params[‘objects’] = [panel(pane) for pane in params[‘objects’]]
/srv/conda/envs/notebook/lib/python3.8/site-packages/panel/layout/base.py in (.0)
359 "as positional arguments or as a keyword, "
360 “not both.” % type(self).name)
→ 361 params[‘objects’] = [panel(pane) for pane in objects]
362 elif ‘objects’ in params:
363 params[‘objects’] = [panel(pane) for pane in params[‘objects’]]
/srv/conda/envs/notebook/lib/python3.8/site-packages/panel/pane/base.py in panel(obj, **kwargs)
49 if kwargs.get(‘name’, False) is None:
50 kwargs.pop(‘name’)
—> 51 pane = PaneBase.get_pane_type(obj, **kwargs)(obj, **kwargs)
52 if len(pane.layout) == 1 and pane._unpack:
53 return pane.layout[0]
/srv/conda/envs/notebook/lib/python3.8/site-packages/panel/param.py in init(self, object, **params)
687 if object is not None:
688 self._validate_object()
→ 689 self._replace_pane(not self.lazy)
690
691 @param.depends(‘object’, watch=True)
/srv/conda/envs/notebook/lib/python3.8/site-packages/panel/param.py in _replace_pane(self, force, *args)
730 new_object = Spacer()
731 else:
→ 732 new_object = self.eval(self.object)
733 self._update_inner(new_object)
734 finally:
/srv/conda/envs/notebook/lib/python3.8/site-packages/panel/param.py in eval(self, function)
720 args = (getattr(dep.owner, dep.name) for dep in arg_deps)
721 kwargs = {n: getattr(dep.owner, dep.name) for n, dep in kw_deps.items()}
→ 722 return function(*args, **kwargs)
723
724 def _replace_pane(self, *args, force=False):
/srv/conda/envs/notebook/lib/python3.8/site-packages/param/parameterized.py in _depends(*args, **kw)
349 @wraps(func)
350 def _depends(*args,**kw):
→ 351 return func(*args,**kw)
352
353 deps = list(dependencies)+list(kw.values())
/srv/conda/envs/notebook/lib/python3.8/site-packages/panel/depends.py in wrapped(*wargs, **wkwargs)
171 combined_kwargs[kw] = arg
172
→ 173 return function(*combined_args, **combined_kwargs)
174 return wrapped
in imaging(surface_var)
17 stream = hv.streams.Tap(source=image.object, x=0, y=0)
18 timeseries = ds[surface_var].interactive.sel(lon=stream.param.x, lat=stream.param.y, method=‘nearest’).hvplot(‘time’).apply.opts(title=f"{stream.x}, {stream.y}")
—> 19 return pn.Column(image* gv.features.coastline(), select,timeseries.dmap())
AttributeError: module ‘geoviews’ has no attribute ‘features’