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

31 次查看(过去 30 天)
I would like the plot area (under the plot lines) to be shaded grey to the right of x = 2.

回答(2 个)

Star Strider
Star Strider 2014-6-2
编辑:Star Strider 2014-6-2
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])

Geoff Hayes
Geoff Hayes 2014-6-2
编辑:Geoff Hayes 2014-6-2
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 个评论
A K M Kamrul Hasan
A K M Kamrul Hasan 2020-1-22
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Polygons 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by