Bars#
Download this notebook from GitHub (right-click to download).
Title: Bars Element#
Dependencies: Bokeh
Backends: Bokeh, Matplotlib, Plotly
import numpy as np
import pandas as pd
import holoviews as hv
hv.extension('bokeh')
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 grouped or stacked by supplying a second key dimension representing sub-categories. Therefore the Bars
Element expects a tabular data format with one or two key dimensions (kdims
) and one or more value dimensions (vdims
). See the Tabular Datasets user guide for supported data formats, which include arrays, pandas dataframes and dictionaries of arrays.
data = [('one',8),('two', 10), ('three', 16), ('four', 8), ('five', 4), ('six', 1)]
bars = hv.Bars(data, hv.Dimension('Car occupants'), 'Count')
bars
We can achieve the same plot using a Pandas DataFrame:
hv.Bars(pd.DataFrame(data, columns=['Car occupants','Count']))
A Bars
element can be sliced and selected on like any other element:
bars[['one', 'two', 'three']] + bars[['four', 'five', 'six']]
It is possible to define an explicit ordering for a set of Bars by explicit declaring Dimension.values
either in the Dimension constructor or using the .redim.values()
approach:
occupants = hv.Dimension('Car occupants', values=['three', 'two', 'four', 'one', 'five', 'six'])
# or using .redim.values(**{'Car Occupants': ['three', 'two', 'four', 'one', 'five', 'six']})
hv.Bars(data, occupants, 'Count')
Bars
also supports continuous data and x-axis.
data = pd.DataFrame({"x": [0, 1, 5], "y": [0, 2, 10]})
hv.Bars(data, ["x"], ["y"])
And datetime data and x-axis.
data = pd.DataFrame({"x": pd.date_range("2017-01-01", "2017-01-03"), "y": [0, 2, -1]})
hv.Bars(data, ["x"], ["y"])
Bars
also supports nested categorical groupings. Next we’ll use a Pandas DataFrame to construct a random sample of pets sub-divided by male and female:
samples = 100
pets = ['Cat', 'Dog', 'Hamster', 'Rabbit']
genders = ['Female', 'Male']
pets_sample = np.random.choice(pets, samples)
gender_sample = np.random.choice(genders, samples)
count = np.random.randint(1, 5, size=samples)
df = pd.DataFrame({'Pets': pets_sample, 'Gender': gender_sample, 'Count': count})
df.head(2)
Pets | Gender | Count | |
---|---|---|---|
0 | Hamster | Male | 1 |
1 | Rabbit | Female | 4 |
bars = hv.Bars(df, kdims=['Pets', 'Gender']).aggregate(function=np.sum)
bars.opts(width=500)
Just as before we can provide an explicit ordering by declaring the Dimension.values
. Alternatively we can also make use of the .sort
method, internally Bars
will use topological sorting to ensure consistent ordering.
bars.redim.values(Pets=pets, Gender=genders) + bars.sort()
To drop the second level of tick labels we can set multi_level=False
, which will indicate the groupings using a legend instead:
bars.sort() + bars.clone().opts(multi_level=False)
Lastly, Bars can be also be stacked by setting stacked=True
:
bars.opts(stacked=True)
For full documentation and the available style and plot options, use hv.help(hv.Bars)
.
Download this notebook from GitHub (right-click to download).