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 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Voss
2024-5-7
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Line Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!