File Exchange

image thumbnail

Violin Plot

version 1.7.0.0 (9.07 KB) by Holger Hoffmann
Violin Plot based on kernel density estimation, using default ksdensity

158 Downloads

Updated 03 Nov 2015

View License

This function creates simple violin plots by estimating the kernel density, using matlabs default ksdensity(). Given a matrix or table with m columns, you will get violins for each of the columns.
Key Features:
a) Specify plotting features as facecolor, edgecolor, etc.
b) Provide data with variable column length as cells if wanted, e.g. Y = [100x1 double] [10x1 double]
c) Plot Violins on the desired x-position

Notes:

1) This function is not perfect. Please modify it as you like.

2) Please do consider the function by Jonas: "Violin Plots for plotting multiple distributions (distributionPlot.m)" which gets you the histograms as shape.

3) Please note: Kernel density is estimated with ksdensity which uses a gaussian kernel by default. Although the bandwidth is optimal according to the rule of thumb, it is however estimated separately for each violin. For publications, etc., please consider that you might need to use the same kernel bandwidths for all data-subsets in order to be able to really compare the distributions.

A beautiful example is given here:
http://www.matlabtips.com/beautiful-plots-in-matlab/

Comments and Ratings (11)

Pearl

Pearl (view profile)

Works great! Thank you!

Lucy Somers

Hi, Thanks for the function! I'm having trouble overlaying multiple violins though. When I set the x positions as a vector of the same value [1 1 1 1], I still get the error:
matlab.graphics.axis.Axes/set
Value must be a 1x2 vector of numeric type in which the second element is larger than the first and may be Inf
Is there a way to overlay these Violins?
Thank you!
Lucy

Omri Raccah

Really like this function! Anyway to plot standard error (e.g. as a dotted line on the violin)

Omri

lu zhang

thanks very much! I saw this on Python and MATLAB could do this too! That's wonderful!

Martin Stefl

Elisa

Elisa (view profile)

Thanks for this function! When I specified a bandwidth, I got an error in the parsing of the arguments to ksdensity.

I solved this by replacing line 163 to
[f, u, bb]=ksdensity(Y{i},'width',b(i));

(i.e., I changed the name of the parameter 'bandwidth' to what ksdensity is looking for namely 'width'). For reference, I'm using R2012a

nice code, seems very useful for my work. however, commenting is mainly lacking, which makes the outline harder to understand, especially if modifications are needed to change what is displayed.

Thanks!

Thanks for the Feedback! The function makes use of ksdensity(). When using ksdensity, you also have this:

a=[1 1 1 1 1 2 2 2 3 3 3 4 5 10];
ksdensity(a)

Please note the negative vals displayed.
When using strictly positive values as in the case above, you may modify the function to

ksdensity(a,'support','positive')

Please have a look here:

http://www.mathworks.de/de/help/stats/ksdensity.html

I will think of a proper modification.
Cheers!

Xen

Xen (view profile)

The function is easy and creates cool violin plots.
My only comment is that when I have data that by definition fall within a specific range (e.g. 0-1) the function sometimes estimates a distribution that lies outside that range (e.g. 0-1.2), probably because my data are highly skewed. Would be nice if that issue was addressed.
Thanks!

Updates

1.7.0.0

title changed

1.7.0.0

title changed

1.7.0.0

Description updated

1.7.0.0

Documentation improved

1.6.0.0

When providing x, axis was set incorrectly previously. This is fixed now.

1.5.0.0

- changed varargin list to parameter-value combination list
- plot violins on specific x-axis position possible now

1.4.0.0

- removed unnecessary text display

1.1.0.0

removed: set(gcf,'Color','w','Position',[2000 100 800 600])

MATLAB Release Compatibility
Created with R2013a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Discover Live Editor

Create scripts with code, output, and formatted text in a single executable document.


Learn About Live Editor