Plot points in xy-plane with different colours
12 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Ryan
el 27 de Mayo de 2017
Comentada: Star Strider
el 28 de Mayo de 2017
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?
0 comentarios
Respuesta aceptada
Star Strider
el 27 de Mayo de 2017
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 comentarios
Star Strider
el 28 de Mayo de 2017
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])
Más respuestas (0)
Ver también
Categorías
Más información sobre Surface and Mesh Plots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!