{
 "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> Bars Element</dd>\n",
    "  <dt>Dependencies</dt> <dd>Matplotlib</dd>\n",
    "  <dt>Backends</dt> <dd><a href='./Bars.ipynb'>Matplotlib</a></dd> <dd><a href='../bokeh/Bars.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": [
    "The ``Bars`` Element uses bars to show discrete, numerical comparisons across categories. One axis of the chart shows the specific categories being compared and the other axis represents a continuous value.\n",
    "\n",
    "Bars may also be stacked by supplying a second key dimensions representing sub-categories. Therefore the ``Bars`` Element expects a tabular data format with one or two key dimensions and one value dimension. See the [Tabular Datasets](../../../user_guide/07-Tabular_Datasets.ipynb) user guide for supported data formats, which include arrays, pandas dataframes and dictionaries of arrays."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = [('one',8),('two', 10), ('three', 16), ('four', 8), ('five', 4), ('six', 1)]\n",
    "bars = hv.Bars(data, hv.Dimension('Car occupants'), 'Count')\n",
    "bars"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can 'slice' a ``Bars`` element by selecting categories as follows:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "bars[['one', 'two', 'three']] + bars[['four', 'five', 'six']]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "``Bars`` support stacking just like the ``Area`` element as well as grouping by a second key dimension. To activate grouping and stacking set the ``group_index`` or ``stack_index`` to the dimension name or dimension index:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%opts Bars.Grouped [category_index=0 group_index=1 stack_index=5] \n",
    "%%opts Bars.Stacked [color_by=['stack'] stack_index=1 category_index=5]\n",
    "from itertools import product\n",
    "np.random.seed(3)\n",
    "index, groups = ['A', 'B'], ['a', 'b']\n",
    "keys = product(index, groups)\n",
    "bars = hv.Bars([k+(np.random.rand()*100.,) for k in keys],\n",
    "               ['Index', 'Group'], 'Count')\n",
    "bars.relabel(group='Grouped') + bars.relabel(group='Stacked')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For full documentation and the available style and plot options, use ``hv.help(hv.Bars).``"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
