Clear Filters
Clear Filters

pcolor: how to change colors for values that satisfy a condition rather than having a gradient of colors?

2 views (last 30 days)
Hi, I want to change colors that appear in my figure based on the conditions that the underlying values satisfy. Example: if value>0.5, the color that is displayed should be red if 0<value<0.3, the color displayed should be green and, for 0.3<value<0.5, the color displayed should be yellow

Answers (1)

Sonam Gupta
Sonam Gupta on 7 Mar 2017
one way to do this is using fplot command. It lets you to plot a function of the form y = f(x) in intervals. Below is a sample code to use it for the given situation.
suppose you have a function f(x) = x^2. you want the values greater than 0.5 to be plotted in green color and less then 0.5 in red.
At first, find the x values for which y will be less than 0.5. If y = x^2, we know x = sqrt(y). Let x goes from 0 to 1 in intervals of 0.1.
x = 0 : .1 : 1;
x0 = 0;
x1 = sqrt(.5); % x value corresponding to which y will be 0.5
x2 = 1;
figure;
syms f(x);
f(x) = x^2;
fplot(f, [x0,x1],'r');
hold on
fplot(f, [x1,x2],'g');
Note that fplot lets you plot in intervals based on x value. Although for simple functions you can find x intervals corresponding to required y values. For more information in fplot, you can check the documentation here

Community Treasure Hunt

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

Start Hunting!