Holomap¶
Download this notebook from GitHub (right-click to download).
- Title
- HoloMap Container
- Dependencies
- Plotly
- Backends
- Matplotlib
- Bokeh
- Plotly
import numpy as np
import holoviews as hv
hv.extension('matplotlib')
A HoloMap is an explorable multi-dimensional dictionary of HoloViews objects. A HoloMap
cannot contain Layouts
, NdLayouts
, GridSpaces
or other HoloMaps
or DyamicMap
but can contain any other HoloViews object. See the Building Composite Objects user guide for details on how to compose containers.
HoloMap
holds dictionaries¶
As a HoloMap
is a dictionary of elements, let us now create a dictionary of sine curves:
frequencies = [0.5, 0.75, 1.0, 1.25]
def sine_curve(phase, freq):
xvals = [0.1* i for i in range(100)]
return hv.Curve((xvals, [np.sin(phase+freq*x) for x in xvals]))
curve_dict = {f:sine_curve(0,f) for f in frequencies}
We now have a dictionary where the frequency is the key and the corresponding curve element is the value. We can now turn this dictionary into a HoloMap
by declaring the keys as corresponding to the frequency key dimension:
hmap = hv.HoloMap(curve_dict, kdims='frequency')
hmap
HoloMap
is multi-dimensional¶
By using tuple keys and making sure each position in the tuple is assigned a corresponding kdim
, HoloMaps
allow exploration of a multi-dimensional space:
phases = [0, np.pi/2, np.pi, 3*np.pi/2]
curve_dict_2D = {(p,f):sine_curve(p,f) for p in phases for f in frequencies}
hmap = hv.HoloMap(curve_dict_2D, kdims=['phase', 'frequency'])
hmap
HoloMap
supports dictionary-like behavior¶
HoloMaps support a number of features similar to regular dictionaries, including assignment:
hmap = hv.HoloMap(kdims=['phase', 'frequency'])
for (phase, freq) in [(0,0.5), (0.5,0.5), (0.5,1), (0,1)]:
hmap[(phase, freq)] = sine_curve(phase,freq)
Key membership predicate:
(0, 0.5) in hmap
The get
method::
hmap.get((0,0.5))
HoloMap
supports multi-dimensional indexing and slicing¶
One difference with regular dictionaries, is that HoloMaps
support multi-dimensional indexing:
hmap[0,1] + hmap[0,:]
See the [User Guide] for more information on selecting, slicing and indexing.
HoloMap
is ordered¶
One difference with regular Python dictionaries is that they are ordered, which can be observed by inspecting the .data
attribute:
hmap.data
We see that internally, HoloMaps
uses OrderedDict
where the keys are sorted by default. You can set sort=False
and then either supply an ordered list of (key, value) tuples, an OrderedDict
or insert items in a chosen order.
That said, there is generally very-little reason to ever use sort=False
as regular Python dictionaries do not have a well-defined key ordering and HoloViews
sliders work regardless of the ordering used. The only reason to set the ordering is if you wish to iterate over a HoloMap
using the items
, keys
, values
methods or use the iterator interface.
Download this notebook from GitHub (right-click to download).