Stream: jupyterlab-hub

Topic: hvPlot and JupyterLab


view this post on Zulip Keith Lindsay (May 14 2021 at 13:54):

I'm trying to explore using hvPlot for interactive graphics in jupyter notebooks, using the JupyterLab interface. The hvPlot docs instruct one to run the command

jupyter labextension install @pyviz/jupyterlab_pyviz

in order to use hvPlot with JupyterLab. When I run this command, I get the error message

An error occured.
ValueError: The extension "@pyviz/jupyterlab_pyviz" does not yet support the current version of JupyterLab.
Conflicting Dependencies:
JupyterLab Extension Package
=3.0.10 <3.1.0 >=2.0.0 <3.0.0 @jupyterlab/application
=3.0.8 <3.1.0 >=2.0.0 <3.0.0 @jupyterlab/apputils
=3.0.10 <3.1.0 >=2.0.0 <3.0.0 @jupyterlab/notebook
=3.0.8 <3.1.0 >=2.0.0 <3.0.0 @jupyterlab/rendermime-interfaces
=6.0.8 <6.1.0 >=5.0.0 <6.0.0 @jupyterlab/services
See the log file for details: /glade/scratch/klindsay/tmp/jupyterlab-debug-hwnqf8_b.log

My conda environment has jupyterlab version 3.0.15. I don't understand this error message, so I don't see what I can do to work around this problem. Suggestions?

The yaml file for my conda environment is at
/glade/work/klindsay/analysis/notebooks/environment.yml.

view this post on Zulip Keith Lindsay (May 14 2021 at 14:37):

I see now that zulip munged my cut-n-paste error message. It is

Conflicting Dependencies:
JupyterLab                        Extension       Package
>=3.0.10 <3.1.0                   >=2.0.0 <3.0.0  @jupyterlab/application
>=3.0.8 <3.1.0                    >=2.0.0 <3.0.0  @jupyterlab/apputils
>=3.0.10 <3.1.0                   >=2.0.0 <3.0.0  @jupyterlab/notebook
>=3.0.8 <3.1.0                    >=2.0.0 <3.0.0  @jupyterlab/rendermime-interfaces
>=6.0.8 <6.1.0                    >=5.0.0 <6.0.0  @jupyterlab/services

view this post on Zulip Max Grover (May 14 2021 at 15:49):

@Keith Lindsay have you tried using holoviews without the extension? Are you accessing this through the JupyterHub?

view this post on Zulip Max Grover (May 14 2021 at 15:52):

Here is an example of the imports I used, the only additional package you will need is pooch to get the sample dataset

import xarray as xr
import numpy as np
import holoviews as hv
from holoviews import opts
hv.extension('matplotlib')

xr_ds = xr.tutorial.open_dataset("air_temperature").load()

hv_ds = hv.Dataset(xr_ds)[:, :, "2013-01-01"]

airtemp = hv_ds.to(hv.Image, kdims=["lon", "lat"], dynamic=False)
airtemp[:, 220:320, :].opts(colorbar=True, fig_size=200)

This should return a plot inline which has an interactive slider

view this post on Zulip Haiying Xu (May 14 2021 at 15:54):

@Keith Lindsay I don't think you need install jupyterlab_pyviz if you had jupyterlab in your environment already. You can run pip install holoviews and hvplot based on your existing environment.

view this post on Zulip Keith Lindsay (May 14 2021 at 16:01):

I have to admit that I'm a bit confused about how hvPlot, holoviews, and bokeh fit together, and what part of that puzzle is providing the interactive framework.

I hadn't tried using hvPlot yet. I was taking their word for it that I needed to install the extension before proceeding. I'll give it a try.

view this post on Zulip Keith Lindsay (May 14 2021 at 16:27):

@Max Grover , I'm able to run your example and get a plot with a time slider.
What I'm really interested in the ability to zoom.
It appears like the examples at https://hvplot.holoviz.org/ have this functionality,
though I might be misunderstanding what the icons mean.
I think, but am not certain that these icons are from the bokeh interface.
Unfortunately, I've been unable to get their example that includes an xr dataset to work in my notebook.
When I cut-n-paste their example, I get the error

...
/glade/work/klindsay/miniconda3/envs/analysis_notebooks/lib/python3.7/site-packages/holoviews/core/data/grid.py in init(cls, eltype, data, kdims, vdims)
    146                 raise error('Key dimension values and value array %s '
    147                             'shapes do not match. Expected shape %s, '
--> 148                             'actual shape: %s' % (vdim, valid_shape, shape), cls)
    149         return data, {'kdims':kdims, 'vdims':vdims}, {}
    150

DataError: Key dimension values and value array air shapes do not match. Expected shape (1325, 1325), actual shape: (53, 25)

GridInterface expects gridded data, for more information on supported datatypes see http://holoviews.org/user_guide/Gridded_Datasets.html

So I'm kinda stumped on that front.

Any suggestions for creating interactive plots with zoom capability?

view this post on Zulip Max Grover (May 14 2021 at 16:29):

Can you share what dataset you working with?

view this post on Zulip Max Grover (May 14 2021 at 16:32):

Try this

import xarray as xr
import numpy as np
import holoviews as hv
from holoviews import opts
hv.extension('bokeh')

import xarray as xr, cartopy.crs as crs
import hvplot.xarray  # noqa

air_ds = xr.tutorial.open_dataset('air_temperature').load()
proj = crs.Orthographic(-90, 30)

air_ds.air.isel(time=slice(0, 9, 3)).hvplot.quadmesh(
    'lon', 'lat', projection=proj, project=True, global_extent=True,
    cmap='viridis', rasterize=True, dynamic=False, coastline=True,
    frame_width=500)

the key is replacing matplotlib with bokeh since bokeh allows the zooming ability

view this post on Zulip Haiying Xu (May 14 2021 at 16:32):

I found out this: "You no longer require the extension for JupyterLab 3.0, it'll be automatically installed if you have pyviz_comms>=2.0 installed. The docs have been updated but the website hasn't been rebuilt since".

view this post on Zulip Keith Lindsay (May 14 2021 at 16:33):

I cut-n-pasted their example code

import xarray as xr, cartopy.crs as crs
import hvplot.xarray  # noqa

air_ds = xr.tutorial.open_dataset('air_temperature').load()
proj = crs.Orthographic(-90, 30)

air_ds.air.isel(time=slice(0, 9, 3)).hvplot.quadmesh(
    'lon', 'lat', projection=proj, project=True, global_extent=True,
    cmap='viridis', rasterize=True, dynamic=False, coastline=True,
    frame_width=500)

I'm generating my conda environment from /glade/work/klindsay/analysis/notebooks/environment.yml.

view this post on Zulip Haiying Xu (May 14 2021 at 16:38):

@Keith Lindsay You have to set active_tools in plotting as this example:
self.plot = self.df.hvplot.scatter(x='x',y='y',c=self.color_val).opts(active_tools=['pan','wheel_zoom'])

view this post on Zulip Keith Lindsay (May 14 2021 at 16:48):

@Max Grover , when I cut-n-paste your code into a notebook, I still get the error

...
/glade/work/klindsay/miniconda3/envs/analysis_notebooks/lib/python3.7/site-packages/holoviews/core/data/grid.py in init(cls, eltype, data, kdims, vdims)
    146                 raise error('Key dimension values and value array %s '
    147                             'shapes do not match. Expected shape %s, '
--> 148                             'actual shape: %s' % (vdim, valid_shape, shape), cls)
    149         return data, {'kdims':kdims, 'vdims':vdims}, {}
    150

DataError: Key dimension values and value array air shapes do not match. Expected shape (1325, 1325), actual shape: (53, 25)

GridInterface expects gridded data, for more information on supported datatypes see http://holoviews.org/user_guide/Gridded_Datasets.html

view this post on Zulip Keith Lindsay (May 14 2021 at 16:51):

@Haiying Xu , I don't understand what I need to do to set active_tools. I don't see how to relate self and self.df in your example to the code that I've posted.

view this post on Zulip Keith Lindsay (May 14 2021 at 17:00):

update: If I drop all of the arguments to quadmesh in the hvPlot example, I get a bokeh interface with zoom capability. This gives me something to start with to modify their example to apply it to my dataset. Thanks @Max Grover and @Haiying Xu for your assistance.

view this post on Zulip Haiying Xu (May 14 2021 at 17:05):

Yes, that's what I get to pan, wheel_zoom and box_zoom as well.

view this post on Zulip John Clyne (May 14 2021 at 17:09):

@Max Grover , @Keith Lindsay I would kindly encourage you to consider contributing your results (once they're working :-) to the GeoCAT-examples repo. We're presently lacking interactive content and would love to have some. I'm sure it would benefit others in the community.

cc @geocat

view this post on Zulip Orhan Eroglu (May 20 2021 at 16:49):

@Keith Lindsay I agree with @John Clyne . We could help include this example under GeoCAT-examples if you would be interested.

In addition, your last message sounds like you figured out some workaround for the time, I'd be happy to help if any further help is needed.

view this post on Zulip Haiying Xu (Jul 12 2021 at 21:41):

I replied Keith Lindsay before: I found out this online: "You no longer require the extension for JupyterLab 3.0, it'll be automatically installed if you have pyviz_comms>=2.0 installed. The docs have been updated but the website hasn't been rebuilt since".


Last updated: May 16 2025 at 17:14 UTC