<commit_before>{
 "metadata": {
  "name": "",
  "signature": "sha256:5f4a43bac664129af5fcdccc51a115be10869161c76b0fb6b2eec980247a114b"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "HoloViews Components"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "This notebook serves as a reference for all the core components of HoloViews, with an extensive list of small, self-contained examples wherever possible, allowing each component to be understood and tested independently.  Further information is available in the docstrings for each component, generally available in IPython by typing the component name followed by \"``?``\".\n",
      "\n",
      "* ***[Section I: Elements](#Elements)*** -- the basic or fundamental types of data that can be visualized.\n",
      "* ***[Section II: Trees](#Trees)*** -- ``+`` and ``*`` operators to adjoin or overlay components\n",
      "* ***[Section III: Paths](#Paths)*** -- line-based components that can be overlaid onto other components\n",
      "* ***[Section IV: Annotations](#Annotations)*** -- other overlayable information\n",
      "* ***[Section V: Parameter Spaces](#Parameter Spaces)*** -- grids and other ways to visualize components covering parameter spaces\n",
      "\n",
      "\n",
      "All HoloViews components have extensive documentation built-in that can be viewed interactively using our [documentation system](./HoloViews_Introduction.ipynb#ParamDoc).\n",
      "\n",
      "\n",
      "Other Tutorials and Notebooks:\n",
      "\n",
      "* [Showcase](http://ioam.github.io/media/holoviews/HoloViews_Showcase.html): Introduction to the overall capabilities of HoloViews\n",
      "\n",
      "* [Introduction](http://ioam.github.io/media/holoviews/HoloViews_Tutorial.html): Step-by-step explanation of the basic concepts in HoloViews\n",
      "\n",
      "* [HoloMap Introduction](http://ioam.github.io/media/holoviews/HoloMap_Tutorial.html) and [HoloMap Advanced](http://ioam.github.io/media/holoviews/HoloMap_Advanced.html): Using a HoloMap to collect all your data so that it can be selected, combined, and visualized flexibly\n",
      "\n",
      "* [Pandas and Seaborn](http://ioam.github.io/media/holoviews/Pandas_and_Seaborn.html): Overview of components that link to third party libraries"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "%load_ext holoviews.ipython"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Section I: Elements <a id='Elements'/>"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "The elements are the basic building blocks for any HoloViews visualization. Using the compositional tree operators [``+`` and ``*``](#Trees), you can compose all these elements together in a multitude of ways. \n",
      "Here in this tutorial, we show how to build these elements directly out of Python or Numpy data structures, but an even more powerful way to use them is via a \n",
      "[``HoloMap``](http://ioam.github.io/media/holoviews/HoloMap_Tutorial.html), documented separately, that allows you to explore, slice, and animate all these elements (and their tree compositions) easily and flexibly.\n",
      "\n",
      "### ``Elements``\n",
      "\n",
      "<dl class=\"dl-horizontal\">\n",
      "  <dt>[``Element``](#Element)</dt><dd>The base class of all ``Elements``.</dd>\n",
      "</dl>\n",
      "  \n",
      "### <a id='ChartIndex'/> [``Chart Elements:``](#Chart Elements) \n",
      "\n",
      "<dl class=\"dl-horizontal\">\n",
      "  <dt>[``Curve``](#Curve)</dt><dd>A continuous relation between a dependent and an independent variable.</dd>\n",
      "  <dt>[``Bars``](#Bars)</dt><dd>Data collected and binned into categories.</dd>\n",
      "  <dt>[``Histogram``](#Histogram)</dt><dd>Data collected and binned in a continuous space using specified bin edges.</dd>\n",
      "  <dt>[``Scatter``](#Scatter)</dt><dd>Discontinuous collection of points indexed over a single dimension.</dd>\n",
      "  <dt>[``Points``](#Points)</dt><dd>Discontinuous collection of points indexed over two dimensions.</dd>\n",
      "  <dt>[``VectorField``](#VectorField)</dt><dd>Cyclic variable (and optional auxillary data) distributed over two dimensional space.</dd>\n",
      "  <dt>[``SideHistogram``](#SideHistogram)</dt><dd>Histogram binning data contained by some other ``Element``.</dd>\n",
      "  </dl>\n",
      "\n",
      "### <a id='Chart3DIndex'/> [``Chart3D Elements:``](#Chart3D Elements)\n",
      "\n",
      "<dl class=\"dl-horizontal\">\n",
      "  <dt>[``Surface``](#Surface)</dt><dd>Continuous collection of points in a three-dimensional space.</dd>\n",
      "  <dt>[``Scatter3D``](#Scatter3D)</dt><dd>Discontinuous collection of points in a three-dimensional space.</dd>\n",
      "</dl>\n",
      "\n",
      "\n",
      "### <a id='RasterIndex'/>  [``Raster Elements:``](#Raster Elements)\n",
      "\n",
      "<dl class=\"dl-horizontal\">\n",
      "  <dt>[``Raster``](#Raster)</dt><dd>The base class of all rasters containing two-dimensional arrays (cannot be sliced).</dd>\n",
      "  <dt>[``HeatMap``](#HeatMap)</dt><dd>Raster displaying sparse data collected in a two-dimensional space.</dd>\n",
      "  <dt>[``Image``](#Image)</dt><dd>Raster containing two-dimensional array in continuous space (sliceable).</dd>\n",
      "  <dt>[``RGB``](#RGB)</dt><dd>Raster of 3 or 4 channels containing colour channel information.</dd>\n",
      "</dl>\n",
      "\n",
      "\n",
      "### <a id='TabularIndex'/>  [``Tabular Elements:``](#Tabular Elements)\n",
      "\n",
      "\n",
      "<dl class=\"dl-horizontal\">\n",
      "  <dt>[``ItemTable``](#ItemTable)</dt><dd>Ordered collection of key-value pairs (ordered dictionary).</dd>\n",
      "  <dt>[``Table``](#Table)</dt><dd>Collection of arbitrary data with arbitrary key and value dimensions.</dd>\n",
      "  </dl>"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "``Element`` <a id='Element'/>"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "``Element`` is the base class for all the other HoloViews objects shown in this section.\n",
      "\n",
      "All ``Element`` objects accept data as the first argument to define the contents of that element. In addition to its implicit type, each element object has a ``value`` string and a ``label`` used to address it.\n",
      "\n",
      "When rich display is off, or if no visualization has been defined for that type of ``Element``, the ``Element`` is presented in ``{type}.{value}.{label}`` format:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from holoviews import Element\n",
      "Element(None, value='Value', label='Label')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "In addition, ``Element`` has ``key_dimensions``, ``value_dimensions`` and ``constant_dimensions`` to describe the semantics of indexing within the ``Element``, the semantics of the underlying contained by the ``Element``, and any constant parameters associated with the object.\n",
      "For more information on ``value``, ``label``, and the use of dimensions, please see our [introductory tutorial](http://ioam.github.io/media/holoviews/HoloViews_Tutorial.html)."
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "``Chart`` Elements <a id='Chart Elements'/>"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "The first large class of ``Elements`` is the ``Chart`` elements. These are generally indexable/sliceable along the $x$-axis, but not the $y$-axis, because they are intended for data values $y$ measured for a given $x$ value."
     ]
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "``Curve`` <a id='Curve'/>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import numpy as np\n",
      "from holoviews import Curve\n",
      "points = [(0.1*i, np.sin(0.1*i)) for i in range(100)]\n",
      "Curve(points)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "``Bars`` <a id='Bars'/>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import numpy as np\n",
      "from holoviews import Bars\n",
      "data = [('one',8),('two', 10), ('three', 16), ('four', 8), ('five', 4), ('six', 1)]\n",
      "Bars(data, key_dimensions=['Car occupants'], value_dimensions=['Frequency'])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "``Histogram`` <a id='Histogram'/>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import numpy as np\n",
      "from holoviews import Histogram\n",
      "data = [np.random.normal() for i in range(10000)]\n",
      "frequencies, edges = np.histogram(data, 20)\n",
      "Histogram(frequencies, edges)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "``Scatter`` <a id='Scatter'/>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "%%opts Scatter (color='k', marker='s', s=50) \n",
      "import numpy as np # marker can e.g. be s,d,o; see http://matplotlib.org/api/markers_api.html\n",
      "np.random.seed(42)\n",
      "from holoviews import Scatter\n",
      "points = [(i, np.random.random()) for i in range(20)]\n",
      "Scatter(points) + Scatter(points)[12:20]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "``Scatter`` is very similar to ``Points``, below, but it is intended for $y$ data measured given an independent value $x$, and thus a ``Scatter`` object can only be sliced along the $x$ axis. For instance, the right-hand plot above shows a slice that is the right side of the ``Scatter`` plot, while the right-hand plot of ``Points`` below shows a two-dimensional slice."
     ]
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "``Points`` <a id='Points'/>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "%opts Points {+groupwise}\n",
      "import numpy as np\n",
      "from holoviews import Points\n",
      "np.random.seed(12)\n",
      "points = np.random.rand(50,2)\n",
      "Points(points) + Points(points)[0.6:0.8,0.2:0.5]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Points are plotted and are sliceable in a two dimensional space, but they can actually be fairly high-dimensional (four in this example):"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "%%opts Points [color_index=2 size_index=3 scaling_factor=50]\n",
      "np.random.seed(10)\n",
      "data = np.random.rand(100,4)\n",
      "points = Points(data, value_dimensions=['z', 'alpha'])\n",
      "points + points[0.3:0.7, 0.3:0.7].hist()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "``VectorField`` <a id='VectorField'/>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import numpy as np\n",
      "from holoviews import VectorField\n",
      "\n",
      "x,y  = np.mgrid[-10:10,-10:10] * 0.25\n",
      "sine_rings  = np.sin(x**2+y**2)*np.pi+np.pi\n",
      "exp_falloff = 1/np.exp((x**2+y**2)/8)\n",
      "\n",
      "vector_data = np.array([x.flatten()/5.,           # X positions\n",
      "                        y.flatten()/5.,           # Y positions\n",
      "                        sine_rings.flatten(),     # Arrow angles\n",
      "                        exp_falloff.flatten()])   # Arrow sizes\n",
      "VectorField(vector_data.T)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "As you can see above, the $x$ and $y$ positions are in a regular grid. The arrow angles follow a sinsoidal ring pattern and the arrow lengths fall off exponentially from the center, so this plot has four dimensions of data (direction and length for each $x,y$ position).\n",
      "\n",
      "Using the ``%%opts`` cell-magic (described in the [introductory tutorial](http://ioam.github.io/media/holoviews/HoloViews_Tutorial.html)), we can also use color as a redundant indicator to the direction or magnitude:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "%%opts VectorField.A [color_dim='angle'] VectorField.M [color_dim='magnitude']\n",
      "VectorField(vector_data.T, value='A')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "``SideHistogram`` <a id='SideHistogram'/>"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "The ``.hist`` method conveniently adjoins a histogram to the side of any ``Chart``, ``Surface``, or ``Raster`` component.  For a ``Raster`` using color or grayscale to show values (below), the side histogram doubles as a color bar or key."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import numpy as np\n",
      "np.random.seed(42)\n",
      "from holoviews import Scatter\n",
      "points = [(i, np.random.normal()) for i in range(800)]\n",
      "Scatter(points).hist()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "``Chart3D`` Elements <a id='Chart3D Elements'/>"
     ]
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "``Surface`` <a id='Surface'/>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "%%opts Surface (cmap='jet' rstride=20, cstride=2)\n",
      "import numpy as np\n",
      "from holoviews import Surface\n",
      "Surface(np.sin(np.linspace(0,100*np.pi*2,10000)).reshape(100,100))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "``Scatter3D`` <a id='Scatter3D'/>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "%%opts Scatter3D [azimuth=40 elevation=20]\n",
      "import numpy as np\n",
      "from holoviews import Scatter3D\n",
      "x,y = np.mgrid[-5:5, -5:5] * 0.1\n",
      "heights = np.sin(x**2+y**2)\n",
      "Scatter3D(np.array(zip(x.flat,y.flat,heights.flat)))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "``Raster`` Elements <a id='Raster Elements'/>"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "The second large class of ``Elements`` is the raster elements. Unlike most ``Chart`` elements (apart from ``Points``), these live in a two dimensional space. For the ``Image`` and ``RGB`` elements, the coordinates of this two-dimensional space are defined in a continuous coordinate system."
     ]
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "``Raster`` <a id='Raster'/>"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "This is primarily the base class of all ``Raster elements``, but may be used directly to visualize 2D-arrays using a color map. However, unlike the ``Image`` subclass, ``Raster`` does not support slicing semantics (although you can always slice your input numpy array as required)."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import numpy as np\n",
      "from holoviews import Raster\n",
      "x,y = np.mgrid[-50:51, -50:51] * 0.1\n",
      "Raster(np.sin(x**2+y**2))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "The default extents of a ``Raster`` correspond to the shape of the input array."
     ]
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "``HeatMap`` <a id='HeatMap'/>"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "A ``HeatMap`` displays like a typical raster image, but the input is a dictionary indexed with two-dimensional keys, not a Numpy array. As many rows and columns as required will be created to display the values in an appropriate grid format.  Values unspecified are labeled ``nan``, and the keys can be any Python datatype (not necessarily numeric).  One typical usage is to show values from a set of experiments, such as a parameter space exploration, and other such visualizations are shown below in the [``Spaces``](#Spaces) section below. Each value is labeled explicitly , and so this component is not meant for very large numbers of samples."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import numpy as np\n",
      "from holoviews import HeatMap\n",
      "data = {(chr(65+i),chr(97+j)):i*j for i in range(5) for j in range(5) if i!=j}\n",
      "HeatMap(data)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "``Image`` <a id='Image'/>"
     ]
    },
    {
     "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.  The coordinate system allows slicing, which makes ``Image`` more useful than ``Raster`` in general."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import numpy as np\n",
      "from holoviews import Image\n",
      "\n",
      "x,y = np.mgrid[-50:51, -50:51] * 0.1\n",
      "bounds=(-1,-1,1,1)   # Coordinate system: (left, bottom, top, right)\n",
      "\n",
      "(Image(np.sin(x**2+y**2),   bounds=bounds) \n",
      " + Image(np.sin(x**2+y**2), bounds=bounds)[-0.5:0.5, -0.5:0.5])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Note that 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.\n",
      "\n",
      "It is also worth noting that the name ``Image`` can clash with other common libraries, which is one reason to avoid unqualified imports like the ``from holoviews import *`` statement that we use in these tutorials for brevity. For instance, the Python Imaging Libray provides an ``Image`` module, and IPython itself supplies an ``Image`` class in ``IPython.display``.  Python namespaces allow you to avoid such problems, e.g. using ``from PIL import Image as PILImage`` or using ``import holoviews as hv`` and then ``hv.Image()``."
     ]
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "``RGB`` <a id='RGB'/>"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "The ``RGB`` element is a ``Raster`` that supports red, green, blue, and optionally an alpha channel."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import numpy as np\n",
      "from holoviews import RGB\n",
      "\n",
      "x,y = np.mgrid[-50:51, -50:51] * 0.1\n",
      "r = 0.5*np.sin(np.pi  +x**2+y**2)+0.5\n",
      "g = 0.5*np.sin(np.pi/2+x**2+y**2)+0.5\n",
      "b = 0.5*np.sin(x**2+y**2)+0.5\n",
      "a = 0.5*np.sin(0.2*(x**2+y**2))+0.5\n",
      "\n",
      "RGB(np.dstack([r,g,b])) +  RGB(np.dstack([r,g,b,a]))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "``RGB`` elements can be constructed using arrays for Hue, Saturation, and Value using the ``toHCS`` operator, as long as the Hue dimension is a cyclic quantity with a declared range:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from holoviews import Dimension\n",
      "from holoviews.operation import toHCS\n",
      "\n",
      "x,y = np.mgrid[-50:51, -50:51] * 0.1\n",
      "hue = np.sin(0.2*(x**2+y**2))\n",
      "exp_falloff = np.exp((x**2+y**2)/20) - 1.0\n",
      "\n",
      "Hue = Image(hue, value_dimensions=[Dimension('Hue', range=(-1,1), cyclic=True)])\n",
      "toHCS(Hue * Image(np.ones((101,101))) * Image(exp_falloff))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "``Tabular`` Elements <a id='Tabular Elements'/>"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},<commit_msg>Added HSV element to components notebook<commit_after>{
 "metadata": {
  "name": "",
  "signature": "sha256:c5651392b4344b3114e0da3f85d372aecb2383f9ffc24dbead1a14f671edc898"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "HoloViews Components"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "This notebook serves as a reference for all the core components of HoloViews, with an extensive list of small, self-contained examples wherever possible, allowing each component to be understood and tested independently.  Further information is available in the docstrings for each component, generally available in IPython by typing the component name followed by \"``?``\".\n",
      "\n",
      "* ***[Section I: Elements](#Elements)*** -- the basic or fundamental types of data that can be visualized.\n",
      "* ***[Section II: Trees](#Trees)*** -- ``+`` and ``*`` operators to adjoin or overlay components\n",
      "* ***[Section III: Paths](#Paths)*** -- line-based components that can be overlaid onto other components\n",
      "* ***[Section IV: Annotations](#Annotations)*** -- other overlayable information\n",
      "* ***[Section V: Parameter Spaces](#Parameter Spaces)*** -- grids and other ways to visualize components covering parameter spaces\n",
      "\n",
      "\n",
      "All HoloViews components have extensive documentation built-in that can be viewed interactively using our [documentation system](./HoloViews_Introduction.ipynb#ParamDoc).\n",
      "\n",
      "\n",
      "Other Tutorials and Notebooks:\n",
      "\n",
      "* [Showcase](http://ioam.github.io/media/holoviews/HoloViews_Showcase.html): Introduction to the overall capabilities of HoloViews\n",
      "\n",
      "* [Introduction](http://ioam.github.io/media/holoviews/HoloViews_Tutorial.html): Step-by-step explanation of the basic concepts in HoloViews\n",
      "\n",
      "* [HoloMap Introduction](http://ioam.github.io/media/holoviews/HoloMap_Tutorial.html) and [HoloMap Advanced](http://ioam.github.io/media/holoviews/HoloMap_Advanced.html): Using a HoloMap to collect all your data so that it can be selected, combined, and visualized flexibly\n",
      "\n",
      "* [Pandas and Seaborn](http://ioam.github.io/media/holoviews/Pandas_and_Seaborn.html): Overview of components that link to third party libraries"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "%load_ext holoviews.ipython"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Section I: Elements <a id='Elements'/>"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "The elements are the basic building blocks for any HoloViews visualization. Using the compositional tree operators [``+`` and ``*``](#Trees), you can compose all these elements together in a multitude of ways. \n",
      "Here in this tutorial, we show how to build these elements directly out of Python or Numpy data structures, but an even more powerful way to use them is via a \n",
      "[``HoloMap``](http://ioam.github.io/media/holoviews/HoloMap_Tutorial.html), documented separately, that allows you to explore, slice, and animate all these elements (and their tree compositions) easily and flexibly.\n",
      "\n",
      "### ``Elements``\n",
      "\n",
      "<dl class=\"dl-horizontal\">\n",
      "  <dt>[``Element``](#Element)</dt><dd>The base class of all ``Elements``.</dd>\n",
      "</dl>\n",
      "  \n",
      "### <a id='ChartIndex'/> [``Chart Elements:``](#Chart Elements) \n",
      "\n",
      "<dl class=\"dl-horizontal\">\n",
      "  <dt>[``Curve``](#Curve)</dt><dd>A continuous relation between a dependent and an independent variable.</dd>\n",
      "  <dt>[``Bars``](#Bars)</dt><dd>Data collected and binned into categories.</dd>\n",
      "  <dt>[``Histogram``](#Histogram)</dt><dd>Data collected and binned in a continuous space using specified bin edges.</dd>\n",
      "  <dt>[``Scatter``](#Scatter)</dt><dd>Discontinuous collection of points indexed over a single dimension.</dd>\n",
      "  <dt>[``Points``](#Points)</dt><dd>Discontinuous collection of points indexed over two dimensions.</dd>\n",
      "  <dt>[``VectorField``](#VectorField)</dt><dd>Cyclic variable (and optional auxillary data) distributed over two dimensional space.</dd>\n",
      "  <dt>[``SideHistogram``](#SideHistogram)</dt><dd>Histogram binning data contained by some other ``Element``.</dd>\n",
      "  </dl>\n",
      "\n",
      "### <a id='Chart3DIndex'/> [``Chart3D Elements:``](#Chart3D Elements)\n",
      "\n",
      "<dl class=\"dl-horizontal\">\n",
      "  <dt>[``Surface``](#Surface)</dt><dd>Continuous collection of points in a three-dimensional space.</dd>\n",
      "  <dt>[``Scatter3D``](#Scatter3D)</dt><dd>Discontinuous collection of points in a three-dimensional space.</dd>\n",
      "</dl>\n",
      "\n",
      "\n",
      "### <a id='RasterIndex'/>  [``Raster Elements:``](#Raster Elements)\n",
      "\n",
      "<dl class=\"dl-horizontal\">\n",
      "  <dt>[``Raster``](#Raster)</dt><dd>The base class of all rasters containing two-dimensional arrays (cannot be sliced).</dd>\n",
      "  <dt>[``HeatMap``](#HeatMap)</dt><dd>Raster displaying sparse data collected in a two-dimensional space.</dd>\n",
      "  <dt>[``Image``](#Image)</dt><dd>Raster containing two-dimensional array in continuous space (sliceable).</dd>\n",
      "  <dt>[``RGB``](#RGB)</dt><dd>Raster of 3 or 4 channels containing colour channel information.</dd>\n",
      "</dl>\n",
      "\n",
      "\n",
      "### <a id='TabularIndex'/>  [``Tabular Elements:``](#Tabular Elements)\n",
      "\n",
      "\n",
      "<dl class=\"dl-horizontal\">\n",
      "  <dt>[``ItemTable``](#ItemTable)</dt><dd>Ordered collection of key-value pairs (ordered dictionary).</dd>\n",
      "  <dt>[``Table``](#Table)</dt><dd>Collection of arbitrary data with arbitrary key and value dimensions.</dd>\n",
      "  </dl>"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "``Element`` <a id='Element'/>"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "``Element`` is the base class for all the other HoloViews objects shown in this section.\n",
      "\n",
      "All ``Element`` objects accept data as the first argument to define the contents of that element. In addition to its implicit type, each element object has a ``value`` string and a ``label`` used to address it.\n",
      "\n",
      "When rich display is off, or if no visualization has been defined for that type of ``Element``, the ``Element`` is presented in ``{type}.{value}.{label}`` format:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from holoviews import Element\n",
      "Element(None, value='Value', label='Label')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "In addition, ``Element`` has ``key_dimensions``, ``value_dimensions`` and ``constant_dimensions`` to describe the semantics of indexing within the ``Element``, the semantics of the underlying contained by the ``Element``, and any constant parameters associated with the object.\n",
      "For more information on ``value``, ``label``, and the use of dimensions, please see our [introductory tutorial](http://ioam.github.io/media/holoviews/HoloViews_Tutorial.html)."
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "``Chart`` Elements <a id='Chart Elements'/>"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "The first large class of ``Elements`` is the ``Chart`` elements. These are generally indexable/sliceable along the $x$-axis, but not the $y$-axis, because they are intended for data values $y$ measured for a given $x$ value."
     ]
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "``Curve`` <a id='Curve'/>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import numpy as np\n",
      "from holoviews import Curve\n",
      "points = [(0.1*i, np.sin(0.1*i)) for i in range(100)]\n",
      "Curve(points)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "``Bars`` <a id='Bars'/>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import numpy as np\n",
      "from holoviews import Bars\n",
      "data = [('one',8),('two', 10), ('three', 16), ('four', 8), ('five', 4), ('six', 1)]\n",
      "Bars(data, key_dimensions=['Car occupants'], value_dimensions=['Frequency'])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "``Histogram`` <a id='Histogram'/>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import numpy as np\n",
      "from holoviews import Histogram\n",
      "data = [np.random.normal() for i in range(10000)]\n",
      "frequencies, edges = np.histogram(data, 20)\n",
      "Histogram(frequencies, edges)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "``Scatter`` <a id='Scatter'/>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "%%opts Scatter (color='k', marker='s', s=50) \n",
      "import numpy as np # marker can e.g. be s,d,o; see http://matplotlib.org/api/markers_api.html\n",
      "np.random.seed(42)\n",
      "from holoviews import Scatter\n",
      "points = [(i, np.random.random()) for i in range(20)]\n",
      "Scatter(points) + Scatter(points)[12:20]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "``Scatter`` is very similar to ``Points``, below, but it is intended for $y$ data measured given an independent value $x$, and thus a ``Scatter`` object can only be sliced along the $x$ axis. For instance, the right-hand plot above shows a slice that is the right side of the ``Scatter`` plot, while the right-hand plot of ``Points`` below shows a two-dimensional slice."
     ]
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "``Points`` <a id='Points'/>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "%opts Points {+groupwise}\n",
      "import numpy as np\n",
      "from holoviews import Points\n",
      "np.random.seed(12)\n",
      "points = np.random.rand(50,2)\n",
      "Points(points) + Points(points)[0.6:0.8,0.2:0.5]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Points are plotted and are sliceable in a two dimensional space, but they can actually be fairly high-dimensional (four in this example):"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "%%opts Points [color_index=2 size_index=3 scaling_factor=50]\n",
      "np.random.seed(10)\n",
      "data = np.random.rand(100,4)\n",
      "points = Points(data, value_dimensions=['z', 'alpha'])\n",
      "points + points[0.3:0.7, 0.3:0.7].hist()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "``VectorField`` <a id='VectorField'/>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import numpy as np\n",
      "from holoviews import VectorField\n",
      "\n",
      "x,y  = np.mgrid[-10:10,-10:10] * 0.25\n",
      "sine_rings  = np.sin(x**2+y**2)*np.pi+np.pi\n",
      "exp_falloff = 1/np.exp((x**2+y**2)/8)\n",
      "\n",
      "vector_data = np.array([x.flatten()/5.,           # X positions\n",
      "                        y.flatten()/5.,           # Y positions\n",
      "                        sine_rings.flatten(),     # Arrow angles\n",
      "                        exp_falloff.flatten()])   # Arrow sizes\n",
      "VectorField(vector_data.T)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "As you can see above, the $x$ and $y$ positions are in a regular grid. The arrow angles follow a sinsoidal ring pattern and the arrow lengths fall off exponentially from the center, so this plot has four dimensions of data (direction and length for each $x,y$ position).\n",
      "\n",
      "Using the ``%%opts`` cell-magic (described in the [introductory tutorial](http://ioam.github.io/media/holoviews/HoloViews_Tutorial.html)), we can also use color as a redundant indicator to the direction or magnitude:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "%%opts VectorField.A [color_dim='angle'] VectorField.M [color_dim='magnitude']\n",
      "VectorField(vector_data.T, value='A')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "``SideHistogram`` <a id='SideHistogram'/>"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "The ``.hist`` method conveniently adjoins a histogram to the side of any ``Chart``, ``Surface``, or ``Raster`` component.  For a ``Raster`` using color or grayscale to show values (below), the side histogram doubles as a color bar or key."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import numpy as np\n",
      "np.random.seed(42)\n",
      "from holoviews import Scatter\n",
      "points = [(i, np.random.normal()) for i in range(800)]\n",
      "Scatter(points).hist()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "``Chart3D`` Elements <a id='Chart3D Elements'/>"
     ]
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "``Surface`` <a id='Surface'/>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "%%opts Surface (cmap='jet' rstride=20, cstride=2)\n",
      "import numpy as np\n",
      "from holoviews import Surface\n",
      "Surface(np.sin(np.linspace(0,100*np.pi*2,10000)).reshape(100,100))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "``Scatter3D`` <a id='Scatter3D'/>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "%%opts Scatter3D [azimuth=40 elevation=20]\n",
      "import numpy as np\n",
      "from holoviews import Scatter3D\n",
      "x,y = np.mgrid[-5:5, -5:5] * 0.1\n",
      "heights = np.sin(x**2+y**2)\n",
      "Scatter3D(np.array(zip(x.flat,y.flat,heights.flat)))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "``Raster`` Elements <a id='Raster Elements'/>"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "The second large class of ``Elements`` is the raster elements. Unlike most ``Chart`` elements (apart from ``Points``), these live in a two dimensional space. For the ``Image`` and ``RGB`` elements, the coordinates of this two-dimensional space are defined in a continuous coordinate system."
     ]
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "``Raster`` <a id='Raster'/>"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "This is primarily the base class of all ``Raster elements``, but may be used directly to visualize 2D-arrays using a color map. However, unlike the ``Image`` subclass, ``Raster`` does not support slicing semantics (although you can always slice your input numpy array as required)."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import numpy as np\n",
      "from holoviews import Raster\n",
      "x,y = np.mgrid[-50:51, -50:51] * 0.1\n",
      "Raster(np.sin(x**2+y**2))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "The default extents of a ``Raster`` correspond to the shape of the input array."
     ]
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "``HeatMap`` <a id='HeatMap'/>"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "A ``HeatMap`` displays like a typical raster image, but the input is a dictionary indexed with two-dimensional keys, not a Numpy array. As many rows and columns as required will be created to display the values in an appropriate grid format.  Values unspecified are labeled ``nan``, and the keys can be any Python datatype (not necessarily numeric).  One typical usage is to show values from a set of experiments, such as a parameter space exploration, and other such visualizations are shown below in the [``Spaces``](#Spaces) section below. Each value is labeled explicitly , and so this component is not meant for very large numbers of samples."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import numpy as np\n",
      "from holoviews import HeatMap\n",
      "data = {(chr(65+i),chr(97+j)):i*j for i in range(5) for j in range(5) if i!=j}\n",
      "HeatMap(data)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "``Image`` <a id='Image'/>"
     ]
    },
    {
     "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.  The coordinate system allows slicing, which makes ``Image`` more useful than ``Raster`` in general."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import numpy as np\n",
      "from holoviews import Image\n",
      "\n",
      "x,y = np.mgrid[-50:51, -50:51] * 0.1\n",
      "bounds=(-1,-1,1,1)   # Coordinate system: (left, bottom, top, right)\n",
      "\n",
      "(Image(np.sin(x**2+y**2),   bounds=bounds) \n",
      " + Image(np.sin(x**2+y**2), bounds=bounds)[-0.5:0.5, -0.5:0.5])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Note that 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.\n",
      "\n",
      "It is also worth noting that the name ``Image`` can clash with other common libraries, which is one reason to avoid unqualified imports like the ``from holoviews import *`` statement that we use in these tutorials for brevity. For instance, the Python Imaging Libray provides an ``Image`` module, and IPython itself supplies an ``Image`` class in ``IPython.display``.  Python namespaces allow you to avoid such problems, e.g. using ``from PIL import Image as PILImage`` or using ``import holoviews as hv`` and then ``hv.Image()``."
     ]
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "``RGB`` <a id='RGB'/>"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "The ``RGB`` element is a ``Raster`` that supports red, green, blue, and optionally an alpha channel."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import numpy as np\n",
      "from holoviews import RGB\n",
      "\n",
      "x,y = np.mgrid[-50:51, -50:51] * 0.1\n",
      "r = 0.5*np.sin(np.pi  +x**2+y**2)+0.5\n",
      "g = 0.5*np.sin(np.pi/2+x**2+y**2)+0.5\n",
      "b = 0.5*np.sin(x**2+y**2)+0.5\n",
      "a = 0.5*np.sin(0.2*(x**2+y**2))+0.5\n",
      "\n",
      "RGB(np.dstack([r,g,b])) +  RGB(np.dstack([r,g,b,a]))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "HSV"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "HoloViews makes it trivial to  work in any color space using very simple subclasses of ``RGB``. As the HSV (hue, saturation, value) color space is also commonly used it is provided by default:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import numpy as np\n",
      "from holoviews import HSV\n",
      "\n",
      "x,y = np.mgrid[-50:51, -50:51] * 0.1\n",
      "h = 0.5 + np.sin(0.2*(x**2+y**2)) / 2.0\n",
      "s = 0.5 + np.cos(0.2*(x**2+y**2)) / 2.0\n",
      "v = np.ones((101,101))\n",
      "\n",
      "HSV(np.dstack([h, s, v]))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "``Tabular`` Elements <a id='Tabular Elements'/>"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
{'fraction_non_alphanumeric': 0.18750138112390338, 'fraction_numerical': 0.013678651139150996, 'mean_word_length': 2.600159108989658, 'pattern_counts': {'":': 500, '<': 197, '<?xml version=': 0, '>': 197, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6188559', 'n_tokens_mistral': 15383, 'n_tokens_neox': 14783, 'n_words': 4037}