问题
Using the dataset ds
defined below how can I attribute one colormap and its associated range per value of channels
when rendering the holomap?
import numpy as np
import holoviews as hv
from holoviews import opts
hv.extension('bokeh', 'matplotlib')
data = np.random.rand(100, 100, 3,20)
times = np.arange(0,10,.5)
channels = ['a','b','c']
ds = hv.Dataset((times,channels,
np.linspace(0., 1., 100),
np.linspace(0., 1., 100),
data),
kdims=['t', 'channel', 'y', 'x'],
vdims=hv.Dimension('T', range=(0, .9)))
opts.defaults(
opts.GridSpace(shared_xaxis=True, shared_yaxis=True),
opts.Image(cmap='jet', width=300, height=300,colorbar=True,),)
ds.to(hv.Image, ['y', 'x'])
回答1:
here is a solution that uses xarray
to manage the dataset.
import numpy as np
import holoviews as hv
from holoviews import opts
import param
import panel as pn
import panel.widgets as pnw
import xarray as xr
hv.extension('bokeh', 'matplotlib')
data = np.random.rand(10, 3, 100, 100)
times = np.arange(0,10)
channels=['a','b','c']
da = xr.DataArray(
data,
[
("t", times),
("c", channels),
("y", np.linspace(0., 1., 100)),
("x", np.linspace(0., 1., 100)),
],
)
ds = da.to_dataset(name="T")
class Parameters(param.Parameterized):
select_channel = param.Selector(default=channels[0], objects=channels)
select_time = param.Integer(default=int(times[0]), bounds=(0, int(len(times)-1) ) )
@param.depends('select_time','select_channel')
def plots(self):
if self.select_channel == channels[0]:
img = hv.Image(hv.Dataset(ds.loc[dict(c=self.select_channel,t=self.select_time)]))
cmap = 'inferno'
elif self.select_channel == channels[1]:
img = hv.Image(hv.Dataset(ds.loc[dict(c=self.select_channel,t=self.select_time)]))
cmap = 'jet'
else:
img = hv.Image(hv.Dataset(ds.loc[dict(c=self.select_channel,t=self.select_time)]))
cmap = 'gray'
frame_width = 500
return img.opts( cmap=cmap,
frame_width=frame_width,
frame_height=np.int(np.round(frame_width * ds.dims['y']/ds.dims['x'],0)),
framewise=False, axiswise=False, colorbar=False, aspect='equal',
fontsize={'title': 16,})
obj = Parameters()
fire_dmap = hv.DynamicMap(obj.plots)
widgets_parameters = pn.panel(obj.param, parameters=['select_channel','select_time'],width=200)
app = pn.Row(widgets_parameters, fire_dmap)
app
来源:https://stackoverflow.com/questions/61057910/holoviews-to-interface-how-to-attribute-cmap-and-range-according-to-an-input-p