Function: h = violin (x)
Function: h = violin (…, property, value, …)
Function: h = violin (hax, …)
Function: h = violin (…, "horizontal")

Produce a Violin plot of the data x.

The input data x can be a N-by-m array containg N observations of m variables. It can also be a cell with m elements, for the case in which the varibales are not uniformly sampled.

The following property can be set using property/value pairs (default values in parenthesis). The value of the property can be a scalar indicating that it applies to all the variables in the data. It can also be a cell/array, indicating the property for each variable. In this case it should have m columns (as many as variables).

Color

("y") Indicates the filling color of the violins.

Nbins

(50) Internally, the function calls hist to compute the histogram of the data. This property indicates how many bins to use. See help hist for more details.

SmoothFactor

(4) The fuction performs simple kernel density estimation and automatically finds the bandwith of the kernel function that best approximates the histogram using optimization (sqp). The result is in general very noisy. To smooth the result the bandwidth is multiplied by the value of this property. The higher the value the smoother the violings, but values too high might remove features from the data distribution.

Bandwidth

(NA) If this property is given a value other than NA, it sets the bandwith of the kernel function. No optimization is peformed and the property SmoothFactor is ignored.

Width

(0.5) Sets the maximum width of the violins. Violins are centered at integer axis values. The distance between two violin middle axis is 1. Setting a value higher thna 1 in this property will cause the violins to overlap.

If the string "Horizontal" is among the input arguments, the violin plot is rendered along the x axis with the variables in the y axis.

The returned structure h has handles to the plot elements, allowing customization of the visualization using set/get functions.

Example:

title ("Grade 3 heights");
axis ([0,3]);
set (gca, "xtick", 1:2, "xticklabel", {"girls"; "boys"});
h = violin ({randn(100,1)*5+140, randn(130,1)*8+135}, "Nbins", 10);
set (h.violin, "linewidth", 2)

See also: boxplot, hist.

Demonstration 1

The following code

 clf
 x = zeros (9e2, 10);
 for i=1:10
   x(:,i) = (0.1 * randn (3e2, 3) * (randn (3,1) + 1) + ...
          2 * randn (1,3))(:);
 endfor
 h = violin (x, "color", "c");
 axis tight
 set (h.violin, "linewidth", 2);
 set (gca, "xgrid", "on");
 xlabel ("Variables")
 ylabel ("Values")

Produces the following figure

Figure 1

Demonstration 2

The following code

 clf
 data = {randn(100,1)*5+140, randn(130,1)*8+135};
 subplot (1,2,1)
 title ("Grade 3 heights - vertical");
 set (gca, "xtick", 1:2, "xticklabel", {"girls"; "boys"});
 violin (data, "Nbins", 10);
 axis tight

 subplot(1,2,2)
 title ("Grade 3 heights - horizontal");
 set (gca, "ytick", 1:2, "yticklabel", {"girls"; "boys"});
 violin (data, "horizontal", "Nbins", 10);
 axis tight

Produces the following figure

Figure 1

Demonstration 3

The following code

 clf
 data = exprnd (0.1, 500,4);
 violin (data, "nbins", {5,10,50,100});
 axis ([0 5 0 max(data(:))])

Produces the following figure

Figure 1

Demonstration 4

The following code

 clf
 data = repmat(exprnd (0.1, 500,1), 1, 4);
 violin (data, "width", linspace (0.1,0.5,4));
 axis ([0 5 0 max(data(:))])

Produces the following figure

Figure 1

Demonstration 5

The following code

 clf
 data = repmat(exprnd (0.1, 500,1), 1, 4);
 violin (data, "nbins", [5,10,50,100], "smoothfactor", [4 4 8 10]);
 axis ([0 5 0 max(data(:))])

Produces the following figure

Figure 1

Package: statistics