How to assign diffrent colors and shapes to diffirent groups in a plot?

1 回表示 (過去 30 日間)
Zhe Dong
Zhe Dong 2023 年 12 月 25 日
コメント済み: Zhe Dong 2023 年 12 月 26 日
Hello, I'm trying to make a plot containing multiple groups, and each group has multiple values, but I don't know how to give different groups different colors and shapes
here's an example of my code, in this case I want to assign diffirent colors and shaples to different groups (A,B,C,D,E).
Many thanks!
clear;clc;close all;
Group = {'A','B','C','D','E'};
C3 = cellstr(Group); % convert cell to string
C4 = categorical(C3);% convert string to categorical
a = [1; 2; 3]; b = [4; 5; 6]; c = [6; 7; 8]; d = [9; 10; 11]; e = [12;13; 14];
data=[a,b,c,d,e];
plot(data, C4, 'LineStyle','none', 'Marker','o');
  2 件のコメント
Voss
Voss 2023 年 12 月 25 日
cellstr does nothing in that case, since Group is already a cell array of character vectors.
Group = {'A','B','C','D','E'};
C3 = cellstr(Group);
isequal(C3,Group)
ans = logical
1
Zhe Dong
Zhe Dong 2023 年 12 月 25 日
Yes, I've already learned the lession from Dyuman Joshi, who helped me with this question, but still thanks!

サインインしてコメントする。

採用された回答

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023 年 12 月 25 日
Is this what you wanted to achieve:
clearvars;clc;close all;
Group = {'A','B','C','D','E'};
C3 = cellstr(Group); % convert cell to string
C4 = categorical(C3);% convert string to categorical
a = [1; 2; 3]; b = [4; 5; 6]; c = [6; 7; 8]; d = [9; 10; 11]; e = [12;13; 14];
data=[a,b,c,d,e];
MT = {'d', 'o', 's', 'p', '^'};
MC = {'r', 'g', 'b', 'c', 'm'};
MFC = {'r', 'g', 'b', 'c', 'm'};
for ii=1:numel(C4)
plot(data(:,ii), C4(ii), strcat(MT{ii}, MC{ii})); hold all
end
  9 件のコメント
Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023 年 12 月 25 日
Most welcome! Glad to help.
Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023 年 12 月 25 日
Thumbs up! :)

サインインしてコメントする。

その他の回答 (1 件)

Dyuman Joshi
Dyuman Joshi 2023 年 12 月 25 日
編集済み: Dyuman Joshi 2023 年 12 月 25 日
If you have the Statistics and ML toolbox, you can utilize gscatter -
Group = {'A','B','C','D','E'};
%You can convert to categorical directly, cellstr is redundant
C4 = categorical(Group)
C4 = 1×5 categorical array
A B C D E
a = [1; 2; 3]; b = [4; 5; 6]; c = [6; 7; 8]; d = [9; 10; 11]; e = [12;13; 14];
data=[a,b,c,d,e];
%Number of groups
s = numel(C4);
%Select colors for each group from a colormap
colors = hsv(s);
%Define symbols for each group
symbols = 'o.xd*';
%The symbols are respectively - circle dot cross diamond star/asterik
gscatter(data.', C4, C4, colors, symbols, 8, 'doleg', 'off')
  4 件のコメント
Dyuman Joshi
Dyuman Joshi 2023 年 12 月 25 日
Yes, you can -
Group = {'A','B','C','D','E'};
%You can convert to categorical directly, cellstr is redundant
C4 = categorical(Group)
C4 = 1×5 categorical array
A B C D E
a = [1; 2; 3]; b = [4; 5; 6]; c = [6; 7; 8]; d = [9; 10; 11]; e = [12;13; 14];
data=[a,b,c,d,e];
%Number of groups
s = numel(C4);
%Define colors manually
colors = [1 0 0; 0 1 0; 0 0 1; 1 0 1; 0 1 1];
%Define symbols for each group
symbols = 'o.xd*';
%The symbols are respectively - circle dot cross diamond star/asterik
gscatter(data.', C4, C4, colors, symbols, 8, 'doleg', 'off')
Zhe Dong
Zhe Dong 2023 年 12 月 26 日
Many thanks for the help!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeChristmas / Winter についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by