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 Center および File ExchangeSurface and Mesh Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by