import numpy as np import holoviews as hv from holoviews import opts hv.extension('bokeh')
Violin element is used to visualise the distribution of a dataset by displaying its probability density. It is very similar to the
BoxWhisker element but provides a more faithful representation even for bi- or multimodal data. The probability density is shown by the area akin to a vertical and mirrored
Distribution element. The thick black bar in the centre represents the interquartile range, the thin black line extended from it represents the 95% confidence intervals, and the white dot is the median.
The data of a
Violin Element may have any number of key dimensions representing the grouping of the value dimension and a single value dimensions representing the distribution of values within each group. See the Tabular Datasets user guide for supported data formats, which include arrays, pandas dataframes and dictionaries of arrays.
In the simplest case a
Violin can be used to display a single distribution of values, such as a NumPy array of normally distributed values:
np.random.seed(37) violin = hv.Violin(np.random.randn(100), vdims='Value') violin
The Violin element supports multiple options for indicating the distribution values in addition to the default
inner value of ‘box’. The ‘stick’ option visualizes each sample as a single line while ‘quartiles’ highlights the first, second and third quartiles. Additionally the
cut options may be used to control the kernel density estimate:
stick = violin.relabel(group='Stick').opts(opts.Violin(inner='stick', cut=0.1, bandwidth=0.1)) quartiles = violin.relabel(group='Quartiles').opts(opts.Violin(inner='quartiles', cut=1., bandwidth=1)) stick + quartiles
Violin element is particularly useful to compare multiple distribution across different categories. As a simple example we can create a dataset of values with randomly assigned Group and Category values and compare the distributions.
groups = [chr(65+g) for g in np.random.randint(0, 3, 200)] violin = hv.Violin((groups, np.random.randint(0, 5, 200), np.random.randn(200)), ['Group', 'Category'], 'Value') violin.opts(opts.Violin(height=400, show_legend=False, width=600, violin_color=hv.dim('Category').str()), clone=True)
Alternatively we can also use the
split keyword to split compare two conditions for each
Violin (in this example we use a style mapping to create a True/False conditions for ‘Category’ values above and below 2):
violin.opts(opts.Violin(height=400, show_legend=True, width=600, split=hv.dim('Category')>2))
For full documentation and the available style and plot options, use