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 Version History

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:

Cite As

Holger Hoffmann (2021). Violin Plot (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (25)

Moldovanu Simona

jing zhang


Ning An

Sia Xie

Ilya Belevich


Cooper Penner

Great Job! I use this all the time

Zhanchao Li

Haochuan Feng

excellent work

Alexandra Avram

Uri Cohen

Julie Pourtois

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!

Heather Sussman

Ronald D Smith

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!

Martin Stefl


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.


Holger Hoffmann

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.

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

Community Treasure Hunt

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

Start Hunting!