How do I assign colours to every plotted dot if I have the colours as cell arrays

조회 수: 1 (최근 30일)
kaiwen wang
kaiwen wang 2022년 4월 29일
답변: DGM 2022년 4월 29일
I have an x-position matrix and a y-position matrix eg. x=[1,2,3,4] y=[1,2,3,4] nad I also have a cell array for the colours of each position eg, c = [r g b r]. How can I plot the data so that each dot on the plot corresponds to the colour, eg, positon 1,1 is red position 2,2 is green

답변 (1개)

DGM
DGM 2022년 4월 29일
Normally, you'd do this by using a colortable (or a list of indices into a color table) with scatter().
x = 1:4;
y = 1:4;
c = [1 0 0; 0 1 0; 0 0 1; 1 0 0];
scatter(x,y,40,c,'filled');
If you really want to do it using a cellchar of color names, I'm pretty sure you're going to be stuck using a loop.
x = 1:4;
y = 1:4;
c = {'r','g','b','r'};
for k = 1:numel(x)
plot(x(k),y(k),'.','color',c{k},'markersize',20);
hold on
end

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by