"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import holoviews as hv\n",
"hv.extension('plotly')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Like ``Raster``, a HoloViews ``Image`` allows you to view 2D arrays using an arbitrary color map. Unlike ``Raster``, an ``Image`` is associated with a [2D coordinate system in continuous space](Continuous_Coordinates.ipynb), which is appropriate for values sampled from some underlying continuous distribution (as in a photograph or other measurements from locations in real space)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ls = np.linspace(0, 10, 200)\n",
"xx, yy = np.meshgrid(ls, ls)\n",
"\n",
"bounds=(-1,-1,1,1) # Coordinate system: (left, bottom, right, top)\n",
"img = hv.Image(np.sin(xx)*np.cos(yy), bounds=bounds)\n",
"img"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Slicing, sampling, etc. on an ``Image`` all operate in this continuous space, whereas the corresponding operations on a ``Raster`` work on the raw array coordinates."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"img + img[-0.5:0.5, -0.5:0.5]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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 ``Image`` is defined as a continuous space it will snap to the closest coordinate, to inspect the closest coordinate we can use the ``closest`` method:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"closest = img.closest((0.1,0.1))\n",
"points = hv.Points([closest])\n",
"print('The value at position %s is %s' % (closest, img[0.1, 0.1]))\n",
"img * points.opts(color='black', marker='cross', size=10)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can also easily take cross-sections of the Image by using the sample method or collapse a dimension using the ``reduce`` method:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"img.sample(x=0) + img.reduce(x=np.mean)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For full documentation and the available style and plot options, use ``hv.help(hv.Image).``"
]
}
],
"metadata": {
"language_info": {
"name": "python",
"pygments_lexer": "ipython3"
}
},
"nbformat": 4,
"nbformat_minor": 1
}