Plot points in xy-plane with different colours

조회 수: 12 (최근 30일)
Ryan
Ryan 2017년 5월 27일
댓글: Star Strider 2017년 5월 28일
I have a function implemented in MATLAB which for each point in [-2;2] x [-2;2] (i. e. for -2 ≤ x,y ≤ 2) returns exactly one of three possible outputs. I want to plot the function such that every point in this square is coloured in one of three different colours (for example red, blue or green), based on what the output of the function was. Of course, colouring every point is not possible, which is why I am specifying a width of the grid of points beforehand. My code looks like this:
for x = -2:width:2
for y = 2:-width:-2
%function f(x,y) with one of 3 different outputs here
if (f(x,y) = output 1) %colour point (x,y) green
if (f(x,y) = output 2) %colour point (x,y) red
if (f(x,y) = output 3) %colour point (x,y) blue
end
end
How can I implement this colouring? In the end, I want a plot of the xy-plane in the given area (including axes) with points in different colours based on the above procedure.
Additional question: The function is actually a fixed-point iteration which is stopped as soon as the result is closer to either output 1, 2 or 3 than a tolerance specified in advance. Is it possible to alter the "brightness" of the point in my plot based on how many steps the iteration takes to fulfill the break criterion?

채택된 답변

Star Strider
Star Strider 2017년 5월 27일
One option:
figure(1)
hold on
for x = -2:width:2
for y = 2:-width:-2
%function f(x,y) with one of 3 different outputs here
if (f(x,y) = output 1) %colour point (x,y) green
scatter(x,y,'pg', 'MarkerFaceColor','g')
elseif (f(x,y) = output 2) %colour point (x,y) red
scatter(x,y,'pr', 'MarkerFaceColor','r')
else (f(x,y) = output 3) %colour point (x,y) blue
scatter(x,y,'pb', 'MarkerFaceColor','b')
end
end
end
hold off
Some variation of it should give you the result you want. (I like to plot stars. Change the marker to your preference.)
NOTE This is UNTESTED CODE. You will have to experiment with it.
  댓글 수: 2
Ryan
Ryan 2017년 5월 27일
Thank you, this is exactly what I was looking for.
Do you have any idea on how to adjust the brightness of the color based on the number of iterations needed (see question)? For example, for only one required iteration the point should be colored green, for twenty iterations it should be coloured in a very light green.
Star Strider
Star Strider 2017년 5월 28일
My pleasure.
You can alter the colour saturation of the green stars (in my code) by changing the values of the components of the RGB triplets. You will need to experiment to get the result you want.
Example
figure(1)
plot(1,1, 'p', 'MarkerSize',20, 'Color',[0 0.9 0], 'MarkerFaceColor',[0 0.9 0])
hold on
plot(2,2, 'p', 'MarkerSize',20, 'Color',[0 0.8 0], 'MarkerFaceColor',[0 0.8 0])
plot(1,2, 'p', 'MarkerSize',20, 'Color',[0 0.5 0], 'MarkerFaceColor',[0 0.5 0])
plot(2,1, 'p', 'MarkerSize',20, 'Color',[0 0.1 0], 'MarkerFaceColor',[0 0.1 0])
hold off
axis([0 3 0 3])

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by