How can I shade my plot to the right of a given x value?

29 views (last 30 days)
I would like the plot area (under the plot lines) to be shaded grey to the right of x = 2.

Answers (3)

Star Strider
Star Strider on 2 Jun 2014
Edited: Star Strider on 2 Jun 2014
Use the patch function. It takes a bit of experimenting to get it to do what you want, but it’s relatively easy.
To draw a gray box in specific axes, for example:
cm = [0 0 0 ; 0.5 0.5 0.5; 1 1 1];
figure(1)
patch([1 1 2 2 1]', [1 2 2 1 1]', cm(2,:))
axis([0 3 0 3])
  1 Comment
Star Strider
Star Strider on 11 Apr 2025
What you want to do is relatively straightforward —
x = linspace(0, pi, 250).'; % Assume Column Vectors
y = sin(x);
[ymx,ymxidx] = max(y);
Pm = 0.5;
deltac = pi/2;
delta0 = pi/4;
yidx = find(diff(sign(y - Pm)));
for k = 1:numel(yidx)
idxrng = max(1,yidx(k)-1) : min(numel(x), yidx(k)+1);
xval(k) = interp1(y(idxrng), x(idxrng), Pm);
yval(k) = interp1(x(idxrng), y(idxrng), xval(k));
end
Lv1 = x >= xval(1) & x <= deltac; % Logical Vector (Red Area)
% Q1 = nnz(Lv1)
Lv2 = x >= deltac & x <= xval(2); % Logical Vector (Green Area)
figure
plot(x, y)
hold on
Pmv = ones(size(x))*Pm;
patch([x(Lv1); flip(x(Lv1))], [zeros(size(x(Lv1))); flip(Pmv(Lv1))], 'r')
patch([x(Lv2); flip(x(Lv2))], [zeros(size(y(Lv2)))+Pm; flip(y(Lv2))], 'g')
plot([0 x(ymxidx)], [1 1]*ymx, '--c', LineWidth=2)
plot([1 1]*xval(2), [0 yval(2)], '--m', LineWidth=2)
hold off
% axis('padded')
ylim([min(ylim) max(y)*1.1])
text(mean([pi/2 xval(1)]), 0.25, 'A_1')
text(0.6*pi, 0.75, 'A_2')
I leave the annotations and precise colours to you. (I colored the patches red and green and the dashed lines cyan and magenta to make them easier to distinguish in the code.)
The purpose for the interpolation is to get relatively precise values for the (x,y) coordinates of the curve intercepts with ‘Pm’. They are not absolutely necessary if simply uising the appropriate index coordinates will work.
.

Sign in to comment.


Geoff Hayes
Geoff Hayes on 2 Jun 2014
Edited: Geoff Hayes on 2 Jun 2014
The area function (type help area in the Command Window for details) can be used to shade in a portion of your plot to the right of a given x value. Consider the following example:
x = -4:0.1:4;
y = x.^2; % usual parabola
figure;
plot(x,y,'b');
% find the index of the element in x that is equal to two (works well for this
% example, but in other cases you may have to choose the index that you want to
% start the shading from)
idx = find(x==2);
% now colour the area in grey to the right of x==2
grey = [127 127 127]./255;
hold on;
area(x(idx:end),y(idx:end),'basevalue',0,'FaceColor',grey);
In the above, we use a subset of x and y to define the region that we want to shade in grey, starting at a base value of 0 (i.e. y==0).
  1 Comment
A K M Kamrul Hasan
A K M Kamrul Hasan on 22 Jan 2020
Hi,
This discussion is about the srea bounded by straight lines. However, is it possible to shade an area in Matlab plot bounded by straight lines and curved lines? (i.e like the attached image) ?
Regards,1486289316.png

Sign in to comment.


Thorsten
Thorsten on 11 Apr 2025
Edited: Thorsten on 11 Apr 2025
x = -10:0.1:10;
f = @(x) -x.^2 + 100; % sample function
finv = @(y) sqrt(-(y - 100)); % inverse of sample function
y = f(x);
y1 = 50; % sample Pm
x1 = finv(y1);
plot(x, y, '-k')
hold on
xline(0)
yline(y1)
% construct the polyshape from its x and y values
ps = polyshape([0 x1 floor(x1):-0.1:0], [50 50 f(floor(x1):-0.1:0)]);
plot(ps, 'FaceColor', [228 133 173]/255, 'FaceAlpha', 1)

Categories

Find more on Line Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!