{
 "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>NdLayout Container</dd>\n",
    "  <dt>Dependencies</dt> <dd>Matplotlib</dd>\n",
    "  <dt>Backends</dt> <dd><a href='./NdLayout.ipynb'>Matplotlib</a></dd> <dd><a href='../bokeh/NdLayout.ipynb'>Bokeh</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",
    "hv.extension('matplotlib')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "An ``NdLayout`` is a multi-dimensional dictionary of HoloViews elements presented side-by-side like a ``Layout``. An ``NdLayout`` can be considered as a special-case of ``HoloMap`` that can hold any one type of HoloViews container or element as long as it isn't another ``NdLayout`` or ``Layout``. Unlike a regular ``Layout`` that can be built with the ``+`` operator, the items in an ``NdOverlay`` container have corresponding keys and must all have the same type. See the [Building Composite Objects](../../../user_guide/05-Building_Composite_Objects.ipynb) user guide for details on how to compose containers."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### ``NdLayout`` holds dictionaries"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Using the ``sine_curve`` function below, we can declare a dictionary of ``Curve`` elements, where the keys correspond to the frequency values:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "frequencies = [0.5, 0.75, 1.0, 1.25]\n",
    "\n",
    "def sine_curve(phase, freq):\n",
    "    xvals = [0.1* i for i in range(100)]\n",
    "    return hv.Curve((xvals, [np.sin(phase+freq*x) for x in xvals]))\n",
    "\n",
    "curve_dict = {f:sine_curve(0,f) for f in frequencies}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "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 an ``NdLayout`` by declaring the keys as corresponding to the frequency key dimension:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "NdLayout = hv.NdLayout(curve_dict, kdims='frequency')\n",
    "NdLayout"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### ``NdLayout`` is multi-dimensional"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "By using tuple keys and making sure each position in the tuple is assigned a corresponding ``kdim``, ``NdLayouts`` allow visualization of a multi-dimensional space:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "curve_dict_2D = {(p,f):sine_curve(p,f) for p in [0, np.pi/2] for f in [0.5, 0.75]}\n",
    "NdLayout = hv.NdLayout(curve_dict_2D, kdims=['phase', 'frequency'])\n",
    "NdLayout"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### ``NdLayout`` is similar to ``HoloMap``"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Other than the difference in the visual semantics, whereby ``NdLayout`` displays its contents overlaid, ``NdLayout`` are very similar to ``HoloMap`` (see the [``HoloMap``](./HoloMap.ipynb) notebook for more information).\n",
    "\n",
    "One way to demonstrate the similarity of these two containers is to cast our ``NdLayout`` object to ``HoloMap``:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "hv.HoloMap(NdLayout)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We could now cast this ``HoloMap`` back to an ``NdLayout``. Unlike the other container examples such as [``GridSpace``](./GridSpace.ipynb) and [``NdOverlay``](./NdOverlay.ipynb), we cannot display this reconstituted ``NdLayout`` next to the ``HoloMap`` above using ``+`` as a ``Layout`` cannot hold an ``NdLayout`` in the same way than an ``NdLayout`` cannot hold a ``Layout``."
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
