import numpy as np import holoviews as hv hv.extension('matplotlib')
, a HoloViews
allows you to view 2D arrays using an arbitrary color map. Unlike
is associated with a
2D coordinate system in continuous space
, which is appropriate for values sampled from some underlying continuous distribution (as in a photograph or other measurements from locations in real space).
ls = np.linspace(0, 10, 200) xx, yy = np.meshgrid(ls, ls) bounds=(-1,-1,1,1) # Coordinate system: (left, bottom, top, right) img = hv.Image(np.sin(xx)*np.cos(yy), bounds=bounds) img
Slicing, sampling, etc. on an
all operate in this continuous space, whereas the corresponding operations on a
work on the raw array coordinates.
img + img[-0.5:0.5, -0.5:0.5]
Notice how, because our declared coordinate system is continuous, we can slice with any floating-point value we choose. The appropriate range of the samples in the input numpy array will always be displayed, whether or not there are samples at those specific floating-point values. This also allows us to index by a floating value, since the
is defined as a continuous space it will snap to the closest coordinate, to inspect the closest coordinate we can use the
%%opts Points (color='black' marker='x' size=20) closest = img.closest((0.1,0.1)) print('The value at position %s is %s' % (closest, img[0.1, 0.1])) img * hv.Points([img.closest((0.1,0.1))])
The value at position (0.105, 0.095000000000000001) is 0.129347201702
We can also easily take cross-sections of the Image by using the sample method or collapse a dimension using the
img.sample(x=0) + img.reduce(x=np.mean)
One additional way to create Image objects is via the separate ImaGen library, which creates parameterized streams of images for experiments, simulations, or machine-learning applications.
For full documentation and the available style and plot options, use