简体   繁体   中英

holoviews overaly of quadmesh and points with time slider not working

I am trying to overaly hv.points on top of hv.quadmesh or hv.image where both these plots have a time dimension.

import xarray as xr
import numpy as np
import pandas as pd
import holoviews as hv
hv.extension('bokeh')

#create data
np.random.seed(0)
temperature = 15 + 8 * np.random.randn(2, 2, 3)
precipitation = 10 * np.random.rand(2, 2, 3)
lon = [[-99.83, -99.32], [-99.79, -99.23]]
lat = [[42.25, 42.21], [42.63, 42.59]]
time = pd.date_range("2014-09-06", periods=3)
reference_time = pd.Timestamp("2014-09-05")

ds = xr.Dataset(
    data_vars=dict(
        temperature=(["x", "y", "time"], temperature),
        precipitation=(["x", "y", "time"], precipitation),
    ),
    coords=dict(
        lon=(["x", "y"], lon),
        lat=(["x", "y"], lat),
        time=time,
        reference_time=reference_time,
    ),
    attrs=dict(description="Weather related data."),
)
df = ds.temperature.to_dataframe().reset_index()


#create quadmesh plot
img = hv.Dataset(ds, ['lon', 'lat','time'],['temperature']).to(hv.QuadMesh,['lon', 'lat'],['temperature'])
img

图片

#create points plot
pnt = hv.Points(df,vdims=['temperature','time'], kdims=['lon','lat']).groupby('time').opts(color='temperature',cmap='turbo')
pnt

积分

Overaly of both image and point only shows first plot

img*pnt

图像*点

If i remove the time component from the points plot, I can overlay the data but then the slider does not change points value with time

img*hv.Points(df,vdims=['temperature','time'], kdims=['lon','lat']).opts(color='temperature',cmap='turbo')
[image*points with no time component][4]

Thank you for your help !!

Your problem is likely due to the transformation of the time objects when you cast to a dataframe.

If you manually try to select data for a particular time step via

dfSubset = df.where(df.time == ds.time[0]).dropna()

you will encounter an error.

In my experience a manual selection of the time and then letting HoloViews decide how to handle the objects works better than grouping and then transforming via .to() .

In your case this would be the following 3 lines:

dsSubset = ds.where(ds.time == ds.time[0], drop=True)
dfSubset = df.where(df.time == df.time[0]).dropna()
hv.QuadMesh(dsSubset, ['lon', 'lat'] ) * hv.Points(dfSubset, ['lon','lat'], vdims=['temperature']).opts(color='temperature',cmap='plasma',colorbar=True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM