{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### **Title**: Rectangles Element\n",
    "\n",
    "**Dependencies**: Matplotlib\n",
    "\n",
    "**Backends**: [Bokeh](./Rectangles.ipynb), [Matplotlib](../matplotlib/Rectangles.ipynb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import holoviews as hv\n",
    "\n",
    "from holoviews import dim\n",
    "hv.extension('matplotlib')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`Rectangles` represent a collection of axis-aligned rectangles in 2D space. Unlike most 2D elements `Rectangles` have four key dimensions representing the bottom-left (x0, y0) and top-right (x1, y1) corners of each box.\n",
    "\n",
    "`Rectangles` are a convenient and efficient way of drawing multiple boxes:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "hv.Rectangles([(0, 0, 1, 1), (2, 3, 4, 6), (0.5, 2, 1.5, 4), (2, 1, 3.5, 2.5)])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Like other elements `Rectangles` support [style mapping](../../../user_guide/04-Style_Mapping.ipynb), making it possible to map value dimensions to the color, alpha and a variety of other options:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "hv.Rectangles([(0, 0, 1, 1, 1), (2, 3, 4, 6, 2), (0.5, 2, 1.5, 4, 3), (2, 1, 3.5, 2.5, 4)], vdims='value').opts(color='value')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Since `Rectangles` is a low level geometry it can be used to generate complex plot types by composing it with other components."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "xs = np.arange(100)\n",
    "ys = np.random.randn(101).cumsum()\n",
    "\n",
    "O = ys[1:]\n",
    "C = ys[:-1]\n",
    "H = np.max([O, C], axis=0) + np.random.rand(100)\n",
    "L = np.min([O, C], axis=0) - np.random.rand(100)\n",
    "\n",
    "boxes = hv.Rectangles((xs-0.25, O, xs+0.25, C))\n",
    "segments = hv.Segments((xs, L, xs, H))\n",
    "\n",
    "# Color boxes where price decreased red and where price increased green\n",
    "color_exp = (dim('y0')>dim('y1')).categorize({True: 'green', False: 'red'})\n",
    "\n",
    "boxes.opts(aspect=3, fig_size=500, color=color_exp, xlabel='Time', ylabel='Price') * segments.opts(color='black')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For full documentation and the available style and plot options, use ``hv.help(hv.Rectangles).``"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
