How to plot different datasets in a scatterplot next to each other at discrete points on x-axis?

Hello,
I have 3 datasets that were obtained at positions 1-10 and have values.
The positions in the table are Strings, i.e. '1', '2', and each dataset contains values for each position.
Now I want to plot the data from all three datasets for each position next to each other in the same (scatter?) plot.
This is what I roughly want it to look like, excuse the crude image. I think I found a picture of what I want it to look like before, but I can't find it anymore.
The dots at each point should be evenly spaced out in the x-axis and there should be enoughenough spacing between the individual points 1-10 in the x-axis.
How can I achieve this? I tried using the reguar scatterplot, but that puts the dots on a line which makes it hard to read:
I pick the datapoints I want to plot from each dataset through an array of strings:
plotPositionDataset1 = [string(1:10)];
plotPositionDataset2 = [string(1:10)];
plotPositionDataset3 = [string(1:10)];
To map the strings I want to plot with the ones in my table I used categorical:
scatterPlotdataX = categorical(Table_Data_1.Position,plotPositionDataset1);
scatterPlotdataY = Table_Data_1.values;
scatter(scatterPlotdataX,scatterPlotdataY,20,"red","o","LineWidth",0.7,"DisplayName","Test1");
Then I repeat the process for all three, which results in the second from figure above.
How can I create a plot that looks like the first figure above? I looked at swarm plots, but that isn't exactly what I want.

Answers (1)

See if the swarmchart function (introduced in R2020b) will do what you want.

6 Comments

Thank you for your answer.
I looked at it before and tried it a minute ago and sadly it doesn't.
s = swarmchart(scatterPlotdataX,scatterPlotdataY,20,"red","o");
s.XJitter = "rand";
s.XJitterWidth = 0.2;
This example makes the plot look like this: (With some more positions.)
It barely makes any difference, and if I increasethe XJitter too much the positions will overlap. Not what I want to accomplish. Am I using it incorrectly?
And I don't know if it will work if I could increase the spacing between the positions on the x-axis, but I don't know how to do that. (Spacing meaning the visual distance between two position points in pixels.)
My pleasure!
I believe that you’re using it correctly. My only other suggestion is to see if you can find something in the Data Distribution Plots — Examples documentation section, and the other plot examples. And another option is to see if something exists in the File Exchange. I don’t know of any plots other than swarmchart that resemble your example.
That is really unfortunate. Looks like it were box charts that I saw, there are examples shown where you can have multiple boxes at one x-axis point.
mathworks.com/help/matlab/ref/boxchart.html
It appears that there are some ways to scale a graphs x-axis, but apparently that only affects the print. But to be honest, I didn't fully understand the few postings that I found about it.
Edit:
Found this (Scatter plot with mean for multiple groups) but the solution is over my head.
O.K.
I’ll leave my Answer here up, since I already invested some time in it, however it’s likely that there is no pre-defined graphics function that will do exactly what you want. Note that the Statistics and Machine Learning Toolbox boxplot function is similar to although not exactly the same as the core MATLAB boxchart function.
I encourage you to look through the other Toolbox plot functions (there may be hundreds of them among the various Toolboxes) to see if there is one that may exist that will do what you want.
Thank you for your effort. Not sure if I should mark this as solved.
For now I ended up using the regular scatter plot with 3 different symbols and colours. It is somewhat readable and good for now.

Sign in to comment.

Products

Release

R2023b

Asked:

on 15 Jan 2024

Commented:

on 15 Jan 2024

Community Treasure Hunt

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

Start Hunting!