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

バージョン (30.2 KB) 作成者: Jonas
Function for plotting multiple histograms side-by-side in 2D - better than boxplot.
ダウンロード: 14K
更新 2017/2/11


編集メモ: 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


Jonas (2024). Violin Plots for plotting multiple distributions (distributionPlot.m) (, MATLAB Central File Exchange. 取得済み .

MATLAB リリースの互換性
作成: R2008a
Windows macOS Linux
Help Center および MATLAB AnswersHistograms についてさらに検索

ヒントを得たファイル: plot spread points (beeswarm plot)

ヒントを与えたファイル: Violin Plot, gQSPSim

Community Treasure Hunt

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

Start Hunting!
バージョン 公開済み リリース ノート

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.