# Violin Plots for plotting multiple distributions (distributionPlot.m​)

Version 1.15.0.0 (30.2 KB) by
Function for plotting multiple histograms side-by-side in 2D - better than boxplot.
Updated 11 Feb 2017

Editor's Note: This file was selected as MATLAB Central Pick of the Week

The zip-file contains the following files for visualizing distributions:
- distributionPlot.m: main function that allows creating violin plots
- myHistogram.m: generate histograms with 'ideal' bin width given the number of data points and the spread (Freedman-Diaconis rule). Note that for integer-valued data, each integer gets its own bin.

In addition, the zip file contains four helper functions: countEntries, colorCode2rgb, isEven, myErrorbar

If you want to overlay individual data points, you need to download the separate submission plotSpread (http://www.mathworks.com/matlabcentral/fileexchange/37105).

DistributionPlot allows visualizing multiple distributions side by side. It is useful for skewed unimodal data and indispensable for multimodal data. DistributionPlot is especially useful for showing the time evolution of a distribution.

Some of the examples from the help:

r = rand(1000,1);
rn = randn(1000,1)*0.38+0.5;
rn2 = [randn(500,1)*0.1+0.27;randn(500,1)*0.1+0.73];
rn2=min(rn2,1);rn2=max(rn2,0);
figure
ah(1)=subplot(2,4,1:2);
boxplot([r,rn,rn2])
ah(2)=subplot(2,4,3:4);
distributionPlot([r,rn,rn2],'histOpt',2); % histOpt=2 works better for uniform distributions than the default
set(ah,'ylim',[-1 2])
data = [randn(100,1);randn(50,1)+4;randn(25,1)+8];
subplot(2,4,5)
distributionPlot(data); % defaults
subplot(2,4,6)
distributionPlot(data,'colormap',copper,'showMM',5,'variableWidth',false) % show density via custom colormap only, show mean/std,
subplot(2,4,7:8)
distributionPlot({data(1:5:end),repmat(data,2,1)},'addSpread',true,'showMM',false,'histOpt',2) %auto-binwidth depends on # of datapoints; for small n, plotting the data is useful

### Cite As

Jonas (2024). Violin Plots for plotting multiple distributions (distributionPlot.m) (https://www.mathworks.com/matlabcentral/fileexchange/23661-violin-plots-for-plotting-multiple-distributions-distributionplot-m), MATLAB Central File Exchange. Retrieved .

##### MATLAB Release Compatibility
Created with R2008a
Compatible with any release
##### Platform Compatibility
Windows macOS Linux
##### Categories
Find more on Histograms in Help Center and MATLAB Answers
##### Acknowledgements

Inspired by: plot spread points (beeswarm plot)

Inspired: Violin Plot, gQSPSim

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

#### distributionPlot/

Version Published Release Notes
1.15.0.0

renamed histogram to myHistogram to avoid clash with new Matlab function of the same name, added support for boxplot overlays

1.14.0.0

Improved documentation (more examples, link to plotSpread), added quantiles (thanks to Warwick for suggestion & testing). Also, belated thanks to Kelly for suggesting horizontal orientation.

1.13.0.0

- Horizontal plotting
- Plotting of half distributions
- Bugfixes

1.12.0.0

Added option to align the bars at the left or the right (option "histOri"), as suggested by Yuri. Also, bugfix.

1.9.0.0

Improved normalization options. Thanks to Jake for the suggestion.

1.7.0.0

Fixed a bug in the code, and two mistakes in the example.

1.6.0.0

Made colorbar more meaningful if there is only one colormap and the bins are normalized globally (i.e. globalNorm is set to 1). Thanks to Brian Katz for the suggestion.

1.4.0.0

Changed input from optional arguments to parameterName/parameterValue pairs (note that the old syntax still works!).
Added several new features, such as support for grouped variables, overlay of data points, and user-defined colormaps.

1.3.0.0

Updated title to Violin Plot, because that's how (part) of these plots are called elsewhere.

1.2.0.0

Documented previously undocumented functionality, chose better screenshot to demonstrate how distributionPlot is better for comparing distributions than boxplot

1.1.0.0

Fixed cryptic error if the data was all NaNs (thanks Christopher for pointing it out!).
distributionPlot now also automatically converts arrays in cells to vectors and throws a warning.

1.0.0.0