how to do violin plot in matlab

I want to plot the Violin Graph with my data, but I am not able to get how to plot those graphs. I saw the Distribution Plot,Distribution Plot in File exchange for Matlab, but I am unable to find the place to input my x-values and y-values.

 Accepted Answer

Matt J
Matt J on 29 Jan 2020
Edited: Matt J on 29 Jan 2020

13 Comments

Hey, I want to start using this more but I'm having trouble changing the color of the individual data points within the body of the violin. The face color and edge color of the violins have options to change in matlab property editor, but not the points themselves. Do you know a way around this?
Thanks for recommending this code by the way, I like it, just need to get around this small issue.
Matt J
Matt J on 22 Apr 2021
Edited: Matt J on 22 Apr 2021
The output object has a handle to the scatter plot of the data points which you can use to adjust this, e.g.,
violins = violinplot(data, cats, _____);
violins(1).ScatterPlot.MarkerFaceColor='red';
I'm trying to visualize my data by means of this package, but I was wondering if there is a way to color differently the datapoints accordingly to their belonging of a certain range of equivalently the area of the violin. this is my attemot in substituiting the CData matrix with arranged RGB triplets but this approach does not works
vs = violinplot(data,[],'Width',0.2,'Bandwidth',0.3,'ViolinColor',[0 0 1],'ViolinAlpha',0.5,'EdgeColor',[0 0 1],'BoxColor',[0 0 0],'MedianColor',[1 0 0]);
hold on
h = vs(1).ScatterPlot;
cdata = vs.ScatterPlot.CData;
c = repmat(cdata,[1446 1]);
c(1:freq_new_net(1),:) = repmat([1 0 0],[freq_new_net(1) 1]);
c(freq_new_net(1)+1:freq_new_net(1)+freq_new_net(2),:) = repmat([1 1 0],[freq_new_net(2) 1]);
c(freq_new_net(1)+freq_new_net(2)+1:freq_new_net(1)+freq_new_net(2)+freq_new_net(3),:) = repmat([1 1 1],[freq_new_net(3) 1]);
c(freq_new_net(1)+freq_new_net(2)+freq_new_net(3)+1:freq_new_net(1)+ freq_new_net(2)+freq_new_net(3)+freq_new_net(4),:) = repmat([0 0 1],[freq_new_net(4) 1]);
c(freq_new_net(1)+freq_new_net(2)+freq_new_net(3)+freq_new_net(4)+1:1446,:) = repmat([0 1 0],[freq_new_net(5) 1]);
h.CData = c;
vs.ScatterPlot.CData = c;
You haven't shown us the result of the code, nor described ot us how the result differs from your expectations, so we can't know what "does not work" means.
I am sorry to not have been clear before, this is the violin plot resulting from the code posted before. i would like to color the datapoints with different colors accordingly to their belonging to a certain range(e.g., datapoints ranging betweeen 0-0.2 in red, between 0.2-0.4 in green and so on) or even better if I could color differently the section of the violin area with different colors (with the same reasoning of the datapoints mentioned above). thank you
In the scatter plot handle, you need to set MarkerFaceColor='flat':
v=violinplot(rand(100,1)); v.ShowData=true; v.ViolinAlpha=0.1;
h=v.ScatterPlot; h.MarkerFaceAlpha=0.4; h.MarkerFaceColor='flat';
h.CData=repmat(h.CData,numel(h.YData),1);
index=h.YData<0.5; h.CData(index,1)=1; h.CData(index,2)=0; h.CData(index,3)=0;
thank you vey much! this has been very helpful
I am consistently having a problem with the 'varargin' variable, even when follwing examples exactly....can anyone explain?
I am consistently having a problem with the 'varargin' variable, even when follwing examples exactly....can anyone explain?
Perhapsw, if you show what you did, and what the result was.
Hi I was able to use the code from the github library, I want to plot multiple violin plots at different x values (eg 0, 1, 2, 3, 4) but when I use this function it automatically plots at x=1 and I am unable to change the X position. Can u please help me, how can i change the X position)
Matt J
Matt J on 24 May 2022
Edited: Matt J on 24 May 2022
I suggest you show us the code you used to attempt the plot, and attach the input data that will let us run it.
AARISH NAWAZ
AARISH NAWAZ on 24 May 2022
Edited: AARISH NAWAZ on 25 May 2022
I have plotted a red line first (see attchment) and using hold on I plotted the violin plots on the same figure but it starts with 1 I want it to start from zero (I want to move each violin plot to one place left).
@Matt J I have attached the matlab file and the .mat data file. Please have a look
Thanking you in anticipation
How to change Violin Plot axis? I am able to plot it but It starts with one by default, I want it to start from zero. Please see the previous comment from me. You can find all the codes and data required to understand the question. @Matt J

Sign in to comment.

More Answers (2)

MATLAB now has a violinplot, introduced in R2024b.
See also swarmchart
tiledlayout(3,1)
violinplot(nexttile,rand(20,3).*[.8 1. 1.2]+[-1 0 1],'Orientation','horizontal')
violinplot(nexttile,0,'DensityDirection','positive')
hold on
violinplot(2,'DensityDirection','negative')
violinplot(nexttile,magic(4));

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Products

Asked:

on 27 Jan 2020

Answered:

on 9 Oct 2024

Community Treasure Hunt

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

Start Hunting!