Box plots and violin plots are used similarly to show the distribution of a numerical quantity. These can be created using the BoxPlot and the ViolinPlot classes, respectively. Each instance of these classes draws a single box/violin; multiple instances can be combined in the same plot in order to compare different distributions.
The following example shows how to use these classes:
Expand source code
The BoxPlot class
The BoxPlot class draws a box plot. When creating an instance of this class using the constructor, you will have to supply the position (i.e., the data space coordinates for the centre of the box, such as the median or mean of a distribution), the direction (i.e., a vector defining the direction along which the box is drawn, still in data space coordinates), as well as the distance between the centre and the ends of the whiskers and the sides of the box. In most cases, for the direction you will want to provide something like {0, 1} (for vertical box plots) or {1, 0} (for horizontal box plots), and then use negative values for the first whisker and the first side of the box, and positive values for the second side of the box and the second whisker.
On the one hand, this means that you have to compute the positions of the various elements “manually” (e.g., by computing the interquartile range as in the example above); on the other end, this allows you to use the BoxPlot class for things that are not actually distributions of values; for example, you could use BoxPlot instances to draw error bars in a bar chart.
The Violin class
The Violin class draws a violin plot representing the distribution of a numerical variable. When creating an instance of this class using the constructor, you will have to supply a position in data space coordinates, which will correspond to the value 0 for the numerical variable. You will also need a direction along which the violin will extend; values for the numerical variable will be multiplied by this to determine their corresponding coordinates in the plot. The violin will extend perpendicularly from the direction, on one or both sides (as determined by the Sides property). Unlike the box plot, a violin plot must necessarily represent a distribution of values.