How can I shade my plot to the right of a given x value?
31 views (last 30 days)
Show older comments
I would like the plot area (under the plot lines) to be shaded grey to the right of x = 2.
0 Comments
Answers (2)
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])
0 Comments
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
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](https://www.mathworks.com/matlabcentral/answers/uploaded_files/264849/1486289316.png)
![1486289316.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/264849/1486289316.png)
See Also
Categories
Find more on Polygons 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!