Violin
Overview
The violin
trace type is used to create violin plots, which visualize the distribution of numerical data. Violin plots combine aspects of box plots and density plots to show the distribution of the data, including its probability density. They are ideal for comparing distributions between different categories.
You can customize the orientation, kernel density estimation, and colors to represent the distribution data effectively.
Common Uses
- Distribution Analysis: Visualizing the distribution of a dataset, similar to box plots but with additional information about the density of data.
- Comparing Categories: Comparing the distribution of numerical data across different categories.
- Outlier Detection: Identifying outliers and the shape of the data distribution.
Check out the Attributes for the full set of configuration options
Examples
Common Configurations
Here's a simple violin
plot showing the distribution of values for two categories:
You can copy this code below to create this chart in your project:
models:
- name: violin-data
args:
- echo
- |
category,value
A,23
A,25
A,27
B,30
B,35
B,28
traces:
- name: Simple Violin Plot
model: ${ref(violin-data)}
props:
type: violin
x: ?{category}
y: ?{value}
charts:
- name: Simple Violin Chart
traces:
- ${ref(Simple Violin Plot)}
layout:
title:
text: Simple Violin Plot<br><sub>Distribution of Values by Category</sub>
This example demonstrates a violin
plot with a box plot overlay and individual data points:
Here's the code:
models:
- name: violin-data-box
args:
- echo
- |
category,value
A,23
A,25
A,27
B,30
B,35
B,28
traces:
- name: Violin Plot with Box
model: ${ref(violin-data-box)}
cohort_on: category
props:
type: violin
y: ?{category}
x: ?{value}
orientation: h
box:
visible: true
points: "all"
marker:
symbol: "cross-dot"
charts:
- name: Violin Chart with Box
traces:
- ${ref(Violin Plot with Box)}
layout:
title:
text: Violin Plot with Box Overlay<br><sub>Distribution with Box Plot and Data Points</sub>
Here's a violin
plot where the data is split by two categories, providing a side-by-side comparison of distributions:
Here's the code:
models:
- name: violin-data-split
args:
- echo
- |
category,sub_category,value
A,X,23
A,Y,25
A,Y,70
A,Y,15
A,X,27
A,X,13
A,X,21
A,X,81
B,X,30
B,X,35
B,X,4
B,Y,35
B,Y,6
B,Y,5
B,X,28
traces:
- name: Violin Plot Category X
model: ${ref(violin-data-split)}
cohort_on: sub_category
props:
type: violin
side: positive
x: ?{category}
y: ?{value}
filters:
- ?{ sub_category = 'X'}
- name: Violin Plot Category Y
model: ${ref(violin-data-split)}
cohort_on: sub_category
props:
type: violin
side: negative
x: ?{category}
y: ?{value}
filters:
- ?{ sub_category = 'Y'}
charts:
- name: Violin Chart with Split Categories
traces:
- ${ref(Violin Plot Category Y)}
- ${ref(Violin Plot Category X)}
layout:
title:
text: Violin Plot with Split Categories<br><sub>Side-by-Side Comparison of Distributions</sub>
A schema to validate plotly trace properties
Attributes
These attributes apply to traces where trace.props.type
is set to violin
. You would configure these attributes on the trace with the trace.props
object.
alignmentgroup: 'string' #(1)!
bandwidth: number
box:
fillcolor: any
line:
color: any
width: number
visible: boolean
width: number
customdata: array
customdatasrc: 'string' #(2)!
fillcolor: any
hoverinfo: array
hoverinfosrc: 'string' #(3)!
hoverlabel:
align: array
alignsrc: 'string' #(4)!
bgcolor: array
bgcolorsrc: 'string' #(5)!
bordercolor: array
bordercolorsrc: 'string' #(6)!
font:
color: array
colorsrc: 'string' #(7)!
family: 'string | array' #(8)!
familysrc: 'string' #(9)!
lineposition: array
linepositionsrc: 'string' #(10)!
shadow: 'string | array' #(11)!
shadowsrc: 'string' #(12)!
size: array
sizesrc: 'string' #(13)!
style: array
stylesrc: 'string' #(14)!
textcase: array
textcasesrc: 'string' #(15)!
variant: array
variantsrc: 'string' #(16)!
weight: array
weightsrc: 'string' #(17)!
namelength: array
namelengthsrc: 'string' #(18)!
hoveron: any
hovertemplate: 'string | array' #(19)!
hovertemplatesrc: 'string' #(20)!
hovertext: 'string | array' #(21)!
hovertextsrc: 'string' #(22)!
ids: array
idssrc: 'string' #(23)!
jitter: number
legend: string
legendgroup: 'string' #(24)!
legendgrouptitle:
font:
color: any
family: 'string' #(25)!
lineposition: any
shadow: 'string' #(26)!
size: number
style: any
textcase: any
variant: any
weight: integer
text: 'string' #(27)!
legendrank: number
legendwidth: number
line:
color: any
width: number
marker:
angle: number
color: any
line:
color: any
outliercolor: any
outlierwidth: number
width: number
opacity: number
outliercolor: any
size: number
symbol: any
meanline:
color: any
visible: boolean
width: number
metasrc: 'string' #(28)!
name: 'string' #(29)!
offsetgroup: 'string' #(30)!
opacity: number
orientation: any
pointpos: number
points: any
quartilemethod: any
scalegroup: 'string' #(31)!
scalemode: any
selected:
marker:
color: any
opacity: number
size: number
showlegend: boolean
side: any
span: array
spanmode: any
stream:
maxpoints: number
token: 'string' #(32)!
text: 'string | array' #(33)!
textsrc: 'string' #(34)!
type: violin
uid: 'string' #(35)!
unselected:
marker:
color: any
opacity: number
size: number
visible: any
width: number
x: array
xaxis: string
xhoverformat: 'string' #(36)!
xsrc: 'string' #(37)!
y: array
yaxis: string
yhoverformat: 'string' #(38)!
ysrc: 'string' #(39)!
zorder: integer
- Set several traces linked to the same position axis or matching axes to the same alignmentgroup. This controls whether bars compute their positional range dependently or independently.
- Sets the source reference on Chart Studio Cloud for
customdata
. - Sets the source reference on Chart Studio Cloud for
hoverinfo
. - Sets the source reference on Chart Studio Cloud for
align
. - Sets the source reference on Chart Studio Cloud for
bgcolor
. - Sets the source reference on Chart Studio Cloud for
bordercolor
. - Sets the source reference on Chart Studio Cloud for
color
. - HTML font family - the typeface that will be applied by the web browser. The web browser can only apply a font if it is available on the system where it runs. Provide multiple font families, separated by commas, to indicate the order in which to apply fonts if they aren't available.
- Sets the source reference on Chart Studio Cloud for
family
. - Sets the source reference on Chart Studio Cloud for
lineposition
. - Sets the shape and color of the shadow behind text. auto places minimal shadow and applies contrast text font color. See https://developer.mozilla.org/en-US/docs/Web/CSS/text-shadow for additional options.
- Sets the source reference on Chart Studio Cloud for
shadow
. - Sets the source reference on Chart Studio Cloud for
size
. - Sets the source reference on Chart Studio Cloud for
style
. - Sets the source reference on Chart Studio Cloud for
textcase
. - Sets the source reference on Chart Studio Cloud for
variant
. - Sets the source reference on Chart Studio Cloud for
weight
. - Sets the source reference on Chart Studio Cloud for
namelength
. - Template string used for rendering the information that appear on hover box. Note that this will override
hoverinfo
. Variables are inserted using %{variable}, for example "y: %{y}" as well as %{xother}, {%xother}, {%_xother}, {%xother_}. When showing info for several points, xother will be added to those with different x positions from the first point. An underscore before or after (x|y)other will add a space on that side, only when this field is shown. Numbers are formatted using d3-format's syntax %{variable:d3-format}, for example "Price: %{y:$.2f}". https://github.com/d3/d3-format/tree/v1.4.5#d3-format for details on the formatting syntax. Dates are formatted using d3-time-format's syntax %{variable|d3-time-format}, for example "Day: %{2019-01-01|%A}". https://github.com/d3/d3-time-format/tree/v2.2.3#locale_format for details on the date formatting syntax. The variables available inhovertemplate
are the ones emitted as event data described at this link https://plotly.com/javascript/plotlyjs-events/#event-data. Additionally, every attributes that can be specified per-point (the ones that arearrayOk: true
) are available. Anything contained in tag<extra>
is displayed in the secondary box, for example "{fullData.name} ". To hide the secondary box completely, use an empty tag<extra></extra>
. - Sets the source reference on Chart Studio Cloud for
hovertemplate
. - Same as
text
. - Sets the source reference on Chart Studio Cloud for
hovertext
. - Sets the source reference on Chart Studio Cloud for
ids
. - Sets the legend group for this trace. Traces and shapes part of the same legend group hide/show at the same time when toggling legend items.
- HTML font family - the typeface that will be applied by the web browser. The web browser can only apply a font if it is available on the system where it runs. Provide multiple font families, separated by commas, to indicate the order in which to apply fonts if they aren't available.
- Sets the shape and color of the shadow behind text. auto places minimal shadow and applies contrast text font color. See https://developer.mozilla.org/en-US/docs/Web/CSS/text-shadow for additional options.
- Sets the title of the legend group.
- Sets the source reference on Chart Studio Cloud for
meta
. - Sets the trace name. The trace name appears as the legend item and on hover. For violin traces, the name will also be used for the position coordinate, if
x
andx0
(y
andy0
if horizontal) are missing and the position axis is categorical. Note that the trace name is also used as a default value for attributescalegroup
(please see its description for details). - Set several traces linked to the same position axis or matching axes to the same offsetgroup where bars of the same position coordinate will line up.
- If there are multiple violins that should be sized according to to some metric (see
scalemode
), link them by providing a non-empty group id here shared by every trace in the same group. If a violin'swidth
is undefined,scalegroup
will default to the trace's name. In this case, violins with the same names will be linked together - The stream id number links a data trace on a plot with a stream. See https://chart-studio.plotly.com/settings for more details.
- Sets the text elements associated with each sample value. If a single string, the same string appears over all the data points. If an array of string, the items are mapped in order to the this trace's (x,y) coordinates. To be seen, trace
hoverinfo
must contain a text flag. - Sets the source reference on Chart Studio Cloud for
text
. - Assign an id to this trace, Use this to provide object constancy between traces during animations and transitions.
- Sets the hover text formatting rulefor
x
using d3 formatting mini-languages which are very similar to those in Python. For numbers, see: https://github.com/d3/d3-format/tree/v1.4.5#d3-format. And for dates see: https://github.com/d3/d3-time-format/tree/v2.2.3#locale_format. We add two items to d3's date formatter: %h for half of the year as a decimal number as well as %{n}f for fractional seconds with n digits. For example, 2016-10-13 09:15:23.456 with tickformat %H~%M~%S.%2f would display 09~15~23.46By default the values are formatted usingxaxis.hoverformat
. - Sets the source reference on Chart Studio Cloud for
x
. - Sets the hover text formatting rulefor
y
using d3 formatting mini-languages which are very similar to those in Python. For numbers, see: https://github.com/d3/d3-format/tree/v1.4.5#d3-format. And for dates see: https://github.com/d3/d3-time-format/tree/v2.2.3#locale_format. We add two items to d3's date formatter: %h for half of the year as a decimal number as well as %{n}f for fractional seconds with n digits. For example, 2016-10-13 09:15:23.456 with tickformat %H~%M~%S.%2f would display 09~15~23.46By default the values are formatted usingyaxis.hoverformat
. - Sets the source reference on Chart Studio Cloud for
y
.