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/
Holger Hoffmann (2021). Violin Plot (https://www.mathworks.com/matlabcentral/fileexchange/45134-violin-plot), MATLAB Central File Exchange. Retrieved .
Inspired by: Violin Plots for plotting multiple distributions (distributionPlot.m)
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Create scripts with code, output, and formatted text in a single executable document.
Great Job! I use this all the time
excellent work
Nice function! Is there already a solution for skewed data?
Works great! Thank you!
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
Really like this function! Anyway to plot standard error (e.g. as a dotted line on the violin)
Omri
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!
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!
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!