Plot RGB triplets from a matrix as markers 'o'

48 ビュー (過去 30 日間)
Stylianos
Stylianos 2025 年 2 月 7 日 18:11
コメント済み: Stylianos 2025 年 2 月 10 日 15:45
I have a matrix B 6-by-3. I want using a for loop to plot six different coloured markers 'o' in the same figure and in one line (y=0).
Each row has 3 elements of 0 and 1 representing RGB triplet colors.
My code is:
% Define axis limits
x = linspace(0,10,10);
y = linspace(0,10,10);
xlabel('X-axis');
ylabel('Y-axis');
axis([0 10 0 10]);
B =[1 0 0
0 1 0
1 0 1
0 1 1
0 0 1
0 0 1];
for k=1:6
VW = B(k,:);
plot((VW),'o','LineWidth',5);
hold on
end
hold off
I get the following figure with wrong colors
Any help is appreciated.
Thanks a lot.
Stelios Kas.

回答 (2 件)

Cris LaPierre
Cris LaPierre 2025 年 2 月 7 日 18:27
編集済み: Cris LaPierre 2025 年 2 月 7 日 18:29
Your plot command is adding 3 points each time. Try this. It specifies a unique x location for each row of C, and colors the marker using the RGB combo for that row. Y=0 for all 6 points.
Note that the order of your commands matters when formatting the appearance of the figure, since calling plot with the syntax you use creates a new axis.
% Define axis limits
C =[1 0 0
0 1 0
1 0 1
0 1 1
0 0 1
0 0 1];
scatter(1:size(C,1),0,[],C,'filled');
xlabel('X-axis');
ylabel('Y-axis');
axis([0 10 0 10]);
  3 件のコメント
Cris LaPierre
Cris LaPierre 2025 年 2 月 7 日 19:19
Ah, you are probably using a slighly older version of MATLAB. Here's updated code.
% Define axis limits
C =[1 0 0
0 1 0
1 0 1
0 1 1
0 0 1
0 0 1];
scatter(1:size(C,1),zeros(1,size(C,1)),[],C,'filled');
xlabel('X-axis');
ylabel('Y-axis');
axis([0 10 0 10]);
Stylianos
Stylianos 2025 年 2 月 7 日 19:30
Thank you very much Chris.
Now it works, I use Matlab 2019b.
Much obliged.
Stelios Kas

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


Fangjun Jiang
Fangjun Jiang 2025 年 2 月 7 日 18:30
編集済み: Fangjun Jiang 2025 年 2 月 7 日 18:33
Use 'color' to specify color in plot()
x = linspace(0,10,10);
y = linspace(0,10,10);
xlabel('X-axis');
ylabel('Y-axis');
axis([0 10 0 10]);
B =[1 0 0
0 1 0
1 0 1
0 1 1
0 0 1
0 0 1];
for k=1:6
VW = B(k,:);
plot((VW),'o','LineWidth',5,'color',VW);
hold on
end
hold off
  3 件のコメント
Fangjun Jiang
Fangjun Jiang 2025 年 2 月 7 日 19:50
Yes. I only provided answer regarding 'color'. Didn't pay attention to what needs to be plotted. Can still use plot() to do it.
axis([0 10 0 10]);
B =[1 0 0
0 1 0
1 0 1
0 1 1
0 0 1
0 0 1];
for k=1:6
VW = B(k,:);
plot(k,0,'o','LineWidth',5,'color',VW);
hold on
end
Stylianos
Stylianos 2025 年 2 月 10 日 15:45
Dear Jiang.
The code works fine.
Thank you very much.
Much obliged.
Stelios Kas

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

カテゴリ

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

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by