1
votes

Is there exist a way to color a plot in matlab? For example my x and y coordinate are between 0 and 1. I want to give a red zone to the area which x times y is less than 0.5 and yellow for the rest.

Thank you.

1
See fill or area.sco1
You mean an area plot?Dan
Yes, I mean an are in the plot. I want to divide the plot into 2 areas, red and yellow.Gregorius Edwadr

1 Answers

1
votes

You can use pcolor to generate a pseudocolor-plot. To get the values of x*y just do an appropriate matrix-multiplication to get M. M can be compared with 0.5 with M<0.5. This returns a logical-matrix that is converted to a double with the double-function and is then passed to pcolor. Then we set a colormap containing red and yellow. Finally we can apply shading flat or shading interp (additionally interpolates), so the lines between the patches disappear.

x = linspace(0,1,1000);
y = x;

M = x'*y;

pcolor(x,y,double(M<0.5));
colormap([1,1,0;1,0,0]);
shading interp

This is the result: result1

Edit: If you want several areas with different colors, just add a new color to the colormap and edit the color-argument of pcolor accordingly. The following code generates a three-zone plot:

x = linspace(0,1,1000);
y = x;

M = x'*y;
C = double(M<0.5)+double(M<0.75);

pcolor(x,y,C);
colormap([0,1,0;1,1,0;1,0,0]);
shading flat

The result looks like this: result2