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

조회 수: 27 (최근 30일)
SSB
SSB 2014년 6월 1일
댓글: A K M Kamrul Hasan 2020년 1월 22일
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에서 Annotations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by