pcolor: how to change colors for values that satisfy a condition rather than having a gradient of colors?
2 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
Sonam Gupta
2017-3-7
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calculus 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!