{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div class=\"contentcontainer med left\" style=\"margin-left: -50px;\">\n",
    "<dl class=\"dl-horizontal\">\n",
    "  <dt>Title</dt> <dd> HeatMap Element</dd>\n",
    "  <dt>Dependencies</dt> <dd>Bokeh</dd>\n",
    "  <dt>Backends</dt>\n",
    "    <dd><a href='./HeatMap.ipynb'>Bokeh</a></dd>\n",
    "    <dd><a href='../matplotlib/HeatMap.ipynb'>Matplotlib</a></dd>\n",
    "    <dd><a href='../plotly/HeatMap.ipynb'>Plotly</a></dd>\n",
    "</dl>\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import holoviews as hv\n",
    "from holoviews import opts\n",
    "hv.extension('bokeh')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "``HeatMap`` visualises tabular data indexed by two key dimensions as a grid of colored values. This allows spotting correlations in multivariate data and provides a high-level overview of how the two variables are plotted.\n",
    "\n",
    "<p><center><div class=\\\"alert alert-info\\\" role=\\\"alert\\\"><b>Note:</b> Some browsers with ad-blocking software are unable to render files named \"HeatMap.ipynb\". If you encounter this error locally, you can avoid it by renaming this file temporarily, or by disabling the ad blocker.</div></center></p>\n",
    "\n",
    "The data for a ``HeatMap`` may be supplied as 2D tabular data with one or more associated value dimensions. The first value dimension will be colormapped, but further value dimensions may be revealed using the hover tool."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = [(i, chr(97+j),  i*j) for i in range(5) for j in range(5) if i!=j]\n",
    "hm = hv.HeatMap(data).sort()\n",
    "hm"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For a coarse grid like this, it can be useful to overlay the actual values onto the heatmap cells:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "hm * hv.Labels(hm).opts(padding=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It is important to note that the data should be aggregated before plotting as the ``HeatMap`` cannot display multiple values for one coordinate and will simply use the first value it finds for each combination of x- and y-coordinates (causing a warning)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "heatmap = hv.HeatMap([(0, 0, 1), (0, 0, 10), (1, 0, 2), (1, 1, 3)])\n",
    "heatmap + heatmap.aggregate(function=np.max)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As the above example shows before aggregating the second value for the (0, 0) is ignored unless we aggregate the data first.\n",
    "\n",
    "To reveal the values of a ``HeatMap`` we can either enable a ``colorbar`` or add a hover tool. The hover tools even allows displaying any number of additional value dimensions, providing additional information a static plot could not capture. \n",
    "\n",
    "Note that a HeatMap allows mixtures of categorical, numeric and datetime values along the x- and y-axes:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "heatmap = hv.HeatMap((np.random.randint(0, 10, 100), np.random.choice(['A', 'B', 'C', 'D', 'E'], 100), \n",
    "                      np.random.randn(100), np.random.randn(100)), vdims=['z', 'z2']).sort().aggregate(function=np.mean)\n",
    "\n",
    "heatmap.opts(opts.HeatMap(tools=['hover'], colorbar=True, width=325, toolbar='above', clim=(-2, 2)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For full documentation and the available style and plot options, use ``hv.help(hv.HeatMap).``"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
