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.

 Risposta accettata

Matt J
Matt J il 29 Gen 2020
Modificato: Matt J il 29 Gen 2020

2 voti

13 Commenti

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 il 22 Apr 2021
Modificato: Matt J il 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';
Camilla Ancona
Camilla Ancona il 30 Mag 2021
Modificato: Camilla Ancona il 30 Mag 2021
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;
Matt J
Matt J il 30 Mag 2021
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
Matt J
Matt J il 31 Mag 2021
Modificato: Matt J il 31 Mag 2021
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?
Matt J
Matt J il 23 Mag 2022
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 il 24 Mag 2022
Modificato: Matt J il 24 Mag 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 il 24 Mag 2022
Modificato: AARISH NAWAZ il 25 Mag 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

Accedi per commentare.

Più risposte (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));

Categorie

Scopri di più su Creating, Deleting, and Querying Graphics Objects in Centro assistenza e File Exchange

Prodotti

Richiesto:

il 27 Gen 2020

Risposto:

il 9 Ott 2024

Community Treasure Hunt

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

Start Hunting!

Translated by