필터 지우기
필터 지우기

plotting points with different colors

조회 수: 29 (최근 30일)
Diego Soler Polo
Diego Soler Polo 2016년 4월 22일
댓글: J. Webster 2016년 4월 23일
Hi,
I have a MxM matrix RegionsMap whose components are integer numbers between 1 and 4. The element RegionsMap(k,j) represents a point in the complex plane, x(k)+i*y(j) (x and y are vectores I previously defined). I want to plot every one of these complex numbers in the complex plane in such a way that: If RegionsMap(k,j) is 1 I color the associated point in blue, if it is 2 I color the associated point in red,...etc (green and yellow for 3 and 4).
How can I achieve this? Any help will be appreciated! Thanks

답변 (2개)

J. Webster
J. Webster 2016년 4월 22일
something like this maybe?
RegionsMap = randi([1 4],10,10);
x = rand(1,10);
y = rand(1,10);
figure(10);
hold on;
for i=1:10
for j=1:10
switch RegionsMap(i,j)
case 1
plot(x(i),y(j), 'ob');
case 2
plot(x(i),y(j), 'or');
case 3
plot(x(i),y(j), 'og');
case 4
plot(x(i),y(j), 'oy');
end
end
end
hold off;
  댓글 수: 2
Diego Soler Polo
Diego Soler Polo 2016년 4월 22일
편집: Diego Soler Polo 2016년 4월 22일
This answer is very nice, thank you very much! I didn't know that switch command. There remains a little trouble: in my case, the number of different cases is an input (which can be as big as 10). I stated the question as I did just to simplify matters a little bit. Perhaps there's some way around this? Less importantly, but also more desirable en my case: can one make a plot like this but with solid circles? This gives a plot consisting of empty circles. Thank you.
J. Webster
J. Webster 2016년 4월 23일
to fill in the circle, you can replace the plot command with
scatter(x(i),y(i), 'og', 'filled')
If you know there will be at most 10 possibilities, you could just define 10 different cases.
Or you could do like Walter Roberson noted and define a colormap.

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2016년 4월 23일
scatter() fourth parameter can be a vector of values. Create a colormap with as many entries as values you have. caxis([1,n]) where n is the number of entries. Now value K will be drawn with color from entry #K

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by