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.

 Respuesta aceptada

Matt J
Matt J el 29 de En. de 2020
Editada: Matt J el 29 de En. de 2020

2 votos

13 comentarios

Joe Sheppard
Joe Sheppard el 22 de Abr. de 2021
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 el 22 de Abr. de 2021
Editada: Matt J el 22 de Abr. de 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 el 30 de Mayo de 2021
Editada: Camilla Ancona el 30 de Mayo de 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 el 30 de Mayo de 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.
Camilla Ancona
Camilla Ancona el 30 de Mayo de 2021
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 el 31 de Mayo de 2021
Editada: Matt J el 31 de Mayo de 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;
Camilla Ancona
Camilla Ancona el 31 de Mayo de 2021
thank you vey much! this has been very helpful
Daniel Madigan
Daniel Madigan el 23 de Mayo de 2022
I am consistently having a problem with the 'varargin' variable, even when follwing examples exactly....can anyone explain?
Matt J
Matt J el 23 de Mayo de 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.
AARISH NAWAZ
AARISH NAWAZ el 24 de Mayo de 2022
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 el 24 de Mayo de 2022
Editada: Matt J el 24 de Mayo de 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 el 24 de Mayo de 2022
Editada: AARISH NAWAZ el 25 de Mayo de 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
AARISH NAWAZ
AARISH NAWAZ el 26 de Mayo de 2022
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

Iniciar sesión para comentar.

Más respuestas (2)

Holger Hoffmann
Holger Hoffmann el 9 de Dic. de 2021
Editada: Holger Hoffmann el 9 de Dic. de 2021

0 votos

Hi - you may also consider to use this one and modify it: Violin Plot - File Exchange - MATLAB Central (mathworks.com)
Adam Danz
Adam Danz el 9 de Oct. de 2024
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));

Categorías

Más información sobre Creating, Deleting, and Querying Graphics Objects en Centro de ayuda y File Exchange.

Productos

Preguntada:

el 27 de En. de 2020

Respondida:

el 9 de Oct. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by