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.

1 回表示 (過去 30 日間)
Alex
Alex 2024 年 5 月 7 日
回答済み: Voss 2024 年 5 月 7 日
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"

回答 (1 件)

Voss
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)
T = 1000x3 table
Value Condition ID _________ _________ ______ -2.2255 A "ID 7" -0.098719 B "ID 5" -2.473 B "ID 4" 1.3498 B "ID 3" 0.6995 A "ID 9" -0.48031 A "ID 6" 0.38059 C "ID 6" 1.5143 B "ID 9" -1.7896 C "ID 5" 0.55064 D "ID 6" 0.27981 B "ID 3" 1.5408 D "ID 1" 0.0011779 D "ID 5" 0.71058 D "ID 9" -0.90439 C "ID 8" -0.79044 C "ID 1"
% 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;

カテゴリ

Help Center および File ExchangeLine Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by