How can I divide the y-axis of a plot into two different color regions?
5 次查看(过去 30 天)
显示 更早的评论
I am trying to divide a plot into green and red regions orientated horizallay across the y-axis and at a specific point on an eponential curve. For example, I would like the graph background to be green below a value of 5 on the y-axis of a curve and red above that value. Here is what I have tried so far,
clc
clc
clear
x=1:10;
y=[1 3 5 7 7.2 7.3 7.5 8 8.5 9];
plot(y,x)
%cftool(y,x)
x=0:0.01:10;
a=0.5552;
b=0.3237;
f_x=a*exp(b*x);
plot(x,f_x)
low=0:5; %I would like the plot background to be green in this range
high=5:10; %I would like the plot background to be red in this range
ciplot(low,high,'b');
%function ciplot(lower,upper,x,colour);
% ciplot(lower,upper)
% ciplot(lower,upper,x)
% ciplot(lower,upper,x,colour) % % Plots a shaded region on a graph between specified lower and upper confidence intervals (L and U).
% l and u must be vectors of the same length.
% Uses the 'fill' function, not 'area'. Therefore multiple shaded plots
% can be overlayed without a problem. Make them transparent for total visibility.
% x data can be specified, otherwise plots against index values.
% colour can be specified (eg 'k'). Defaults to blue.
% Raymond Reynolds 24/11/06
%if length(lower)~=length(upper) %error('lower and upper vectors must be same length')
%end
%if nargin<4
%colour='b';
%end
%if nargin<3
% x=1:length(lower);
%end
% convert to row vectors so fliplr can work
%if find(size(x)==(max(size(x))))<2
%x=x'; end
%if find(size(lower)==(max(size(lower))))<2
%lower=lower'; end
%if find(size(upper)==(max(size(upper))))<2
%upper=upper'; end
%fill([x fliplr(x)],[upper fliplr(lower)],colour)
%--Error Message--%
%Error using fill
%String argument is an unknown option.
%Error in ciplot (line 36)
%fill([x fliplr(x)],[upper fliplr(lower)],colour)
%Error in example_yaxis_divide (line 21)
%ciplot(low,high,'b');
0 个评论
采纳的回答
Geoff Hayes
2014-5-27
编辑:Geoff Hayes
2014-5-27
You could try to use the area command to fill in the regions beneath the curve in multiple colours. For example, if your data is
x = -5:0.01:5;
y = exp(x);
you can plot the curve as
plot(x,y);
Use the find command to determine the last index of y that has a value less than or equal to 5. (In this case, that index is 661.) Then, colour in the area beneath the curve in green for all those y values less than or equal to 5
hold on;
area(x(1:661),y(1:661),'basevalue',0,'FaceColor','g');
Now colour in the area beneath the curve in red for all those y values greater than 5
area(x(662:end),y(662:end),'basevalue',0,'FaceColor','r');
In the command window, type help area for more details on this command (and the use of basevalue).
3 个评论
Geoff Hayes
2014-5-27
Almost. In the above example, I would use y and do something like
yIdcs = find(y<=5);
to get all indices in y whose value is less than or equal to 5. Since the exponential function (in the above example) is increasing, yIdcs(end) is the last index (661) that has a value less than or equal to 5.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Annotations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!