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

Version (30,2 KB) von Jonas
Function for plotting multiple histograms side-by-side in 2D - better than boxplot.
Aktualisiert 11. Feb 2017

Lizenz anzeigen

Anmerkung des Herausgebers: 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 (

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];
distributionPlot([r,rn,rn2],'histOpt',2); % histOpt=2 works better for uniform distributions than the default
set(ah,'ylim',[-1 2])
%--additional options
data = [randn(100,1);randn(50,1)+4;randn(25,1)+8];
distributionPlot(data); % defaults
distributionPlot(data,'colormap',copper,'showMM',5,'variableWidth',false) % show density via custom colormap only, show mean/std,
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

Zitieren als

Jonas (2024). Violin Plots for plotting multiple distributions (distributionPlot.m) (, MATLAB Central File Exchange. Abgerufen .

Kompatibilität der MATLAB-Version
Erstellt mit R2008a
Kompatibel mit allen Versionen
Windows macOS Linux
Mehr zu Histograms finden Sie in Help Center und MATLAB Answers

Inspiriert von: plot spread points (beeswarm plot)

Inspiriert: Violin Plot, gQSPSim

Community Treasure Hunt

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

Start Hunting!
Version Veröffentlicht Versionshinweise

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

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

Added the following new features:
- Horizontal plotting
- Plotting of half distributions
- Bugfixes

Additionally, plotSpread is now a separate submission.

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

Improved normalization options. Thanks to Jake for the suggestion.

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

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.

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.

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

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

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.