How to plot a swarm chart with no specific x-values?

Hi everyone,
I have a huge dataset, around 1 million values in a column. The range of these values are limited. They, for example, fluctuate between -100 to +100 (I need to find these two values by min and max). The fluctuation is also very erratic (there might be some patterns though).
So, I was having an idea to use swarm chart and plot the distribution of this dataset on something like a swarm chart. Do you think it would be possible using swarm chart or any other chart type on MATLAB? (I know we can use histogram but something like what I described would be easier to understand for my audience)
I mean something like this:

댓글 수: 5

@Russel Burgess thanks Russel, I was exited about it first but then it said x and y should be the same lenght :(
I'm not sure if it's exactly what you're after, but you can just set the x values to all be 0, for example:
x = zeros(1e3,1);
y = randn(1e3,1);
swarmchart(x,y);
Produces:
@Russel Burgess thank you Russel, i will try that tomorrow and paste the results here. Thanks
@Russel Burgess Hi Russel, would you please put your comment in the answer so that I can pick it up :D you saved my life dude and it looks super cool. Just a quick question, how do I draw a line on the chart using the widest area of that swarm chart. I mean, how can I find the y value where the swarm chart is the widest.
Thanks

댓글을 달려면 로그인하십시오.

 채택된 답변

Russel Burgess
Russel Burgess 2021년 3월 15일
Following on from the comments:
y = randn(1e3,1);
swarmchart(zeros(size(y)), y);
Will produce the desired chart, in R2020b.
For drawing a line on the chart at the widest point - unfortunately (as far as I know) swarmchart doesn't return or expose the plotted coordinates... publicly. That means you'll have to break into the structure and ignore some warnings.
figure(1);
clf;
hold('on');
y = randn(1e3,1);
s = swarmchart(zeros(size(y)), y);
raw_s = struct(s);
raw_data = raw_s.XYZJittered;
[xmax,maxidx] = max(abs(raw_data(:,1)));
ymax = raw_data(maxidx,2);
plot([-xmax xmax], [ymax ymax], 'r-', 'LineWidth', 3);
This may not be exactly what you want but it should be enough to get started: those last 5 lines of code expose the private scatter object fields, including the "XYZJittered" field which appears to contain the actual plotted data in 3 columns. I then just find the coordinates of the point with x value farthest from 0, and plot the line there. This yields something like:
This is a little crude - but once you've got the raw plotted data you could process it in other ways. Another option would be to calculate the kernel density directly to find the maximums (if you have the stats toolbox, or here but I haven't tried it).

추가 답변 (1개)

Matt J
Matt J 2021년 3월 12일
편집: Matt J 2021년 3월 12일

0 개 추천

This Github offering has worked pretty well for me:

댓글 수: 5

@Matt J hey, thanks, that might help but how to use it as a new user :D it's too much code. Could you please teach me where I should put my excel to plot it? Thanks
Matt J
Matt J 2021년 3월 12일
편집: Matt J 2021년 3월 12일
There is an example at the link I gave you. After you've read the data from the excel file, you just pass it as input to violinplot().
@Matt J Yes, I saw that but to be honest :D I need a dummy guide as I don't know how to call functions. So, let's say I have xlsread in a variable like X. Then, in my code, I just write, violinplot(X)?
I guess you figured it out (sinc eyou Accept-clicked the answer)?
@Matt J to be honest not really. :D I even downloaded newer MATLAB files from file exchange which are apparently way more simpler too (like the one from 2019) but I couldn't get it going. So, I just gave up on it :( but I would really appreciate it if you could teach me.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

제품

릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by