How to make a scatter plot where each point is colored according to a given ID and the xlabel ticks are determined by the associated string.
2 views (last 30 days)
Show older comments
The data I am trying to plot is in an Nx3 table where the label for each columns are 'Value', 'Condition' and 'ID'. I want to plot the values in 'Value', group them according to 'Condition' and color them based on 'ID' so something like swarmchart I guess I am trying to do.
To get something to work with, this is an example of how my data could look like. How do I plot this?
Value Condition ID
----------------------------- ---- -------
0.0608000000000000 'B' "ID 1"
0.0476000000000000 'B' "ID 2"
0.0483000000000000 'B' "ID 3"
0.298800000000000 'B' "ID 4"
0.0377000000000000 'B' "ID 5"
0 Comments
Answers (1)
Voss
on 7 May 2024
Here's one way:
% random data in a table
N = 1000;
Value = randn(N,1);
Condition = char(randi(4,N,1)+64);
ID = "ID "+randi(9,N,1);
T = table(Value,Condition,ID)
% do the grouping and swarmchart plotting
[idxC,CG] = findgroups(T.Condition);
[idxID,IDG] = findgroups(T.ID);
NC = numel(CG);
NID = numel(IDG);
figure('Colormap',turbo(NID))
hold on
for ii = 1:NC
idx = idxC == ii;
x = ii*ones(nnz(idx),1);
y = T.Value(idx);
c = idxID(idx);
swarmchart(x,y,[],c,'.')
end
xlim([0 NC+1])
xticks(1:NC)
xticklabels(CG)
cb = colorbar();
y = linspace(1,NID,NID+1);
cb.YTick = (y(1:end-1)+y(2:end))/2;
cb.YTickLabel = IDG;
0 Comments
See Also
Categories
Find more on Line Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!