File Exchange

image thumbnail

Violin Plot

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


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


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:

Comments and Ratings (13)

Lieke Numan

Nice function! Is there already a solution for skewed data?


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:
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!

Omri Raccah

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


lu zhang

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


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 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];

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


Please have a look here:

I will think of a proper modification.


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.


title changed

title changed

Description updated

Documentation improved

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

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

- removed unnecessary text display

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