Bars

Title
Bars Element
Dependencies
Matplotlib
Backends
Matplotlib
Bokeh
In [1]:
import numpy as np
import holoviews as hv
hv.extension('matplotlib')

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.

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 Columnar Data Tutorial for supported data formats, which include arrays, pandas dataframes and dictionaries of arrays.

In [2]:
data = [('one',8),('two', 10), ('three', 16), ('four', 8), ('five', 4), ('six', 1)]
bars = hv.Bars(data, kdims=[hv.Dimension('Car occupants')], vdims=['Count'])
bars
Out[2]:

You can 'slice' a Bars element by selecting categories as follows:

In [3]:
bars[['one', 'two', 'three']] + bars[['four', 'five', 'six']]
Out[3]:

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:

In [4]:
%%opts Bars.Grouped [category_index=0 group_index=1 stack_index=5] 
%%opts Bars.Stacked [color_by=['stack'] stack_index=1 category_index=5]
from itertools import product
np.random.seed(3)
index, groups = ['A', 'B'], ['a', 'b']
keys = product(index, groups)
bars = hv.Bars([k+(np.random.rand()*100.,) for k in keys],
               kdims=['Index', 'Group'], vdims=['Count'])
bars.relabel(group='Grouped') + bars.relabel(group='Stacked')
Out[4]:

Download this notebook from GitHub (right-click to download).