필터 지우기
필터 지우기

Fill light gray color between two horizontal lines in a plot?

조회 수: 73 (최근 30일)
Abhik Saha
Abhik Saha 2023년 2월 7일
댓글: Star Strider 2024년 2월 14일
I want to fill light gray color between two horizontal lines such that I can plot the horizontal lines as dashed dotted ? Please help me regarding this issue. I am writing below the code
x=[1:10];
y=[1:10];
figure(1);clf;
plot(x,y,'b')
hold on
plot([1,10],2*[1,1],'--.k')
hold on
plot([1,10],4*[1,1],'--.k')
  댓글 수: 2
Levente Gellért
Levente Gellért 2024년 2월 14일
Dear Community members! How can one solve the same problem in 2017a?
Thanks
lg
Star Strider
Star Strider 2024년 2월 14일
My code should work equally well in R2017a.

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2023년 2월 7일
To plot a ‘dash-dot¹ line requires a slightly different LineStyle definition —
x=[1:10];
y=[1:10];
figure(1)
plot(x,y,'b')
hold on
patch([x, flip(x)], [ones(size(x))*2, ones(size(x))*4], [1 1 1]*0.05, 'Edgecolor','none', 'FaceAlpha',0.25)
plot([1,10],2*[1,1],'-.k')
plot([1,10],4*[1,1],'-.k')
hold off
Plotting the patch as dash-dotted is difficult, however it is possible.
.
  댓글 수: 3
Star Strider
Star Strider 2023년 2월 9일
As always, my pleasure!
Afiq Azaibi
Afiq Azaibi 2023년 3월 17일
In addition to Star Strider's solution, starting in R2023a you can use the xregion and yregion functions:
plot(1:10);
yregion(2, 5, 'EdgeColor', 'black');
You can update the LineStyle, FaceAlpha, and other visuals about the object. However, the default EdgeColor is 'none'.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Arif Hoq
Arif Hoq 2023년 2월 7일
x=[1:10];
y=[1:10];
figure(1);clf;
plot(x,y,'b')
hold on
plot([1,10],2*[1,1],'--.k')
hold on
plot([1,10],4*[1,1],'--.k')
x = [1 10 10 1];
y = [2 2 4 4];
patch(x,y,[.5 .5 .5])
  댓글 수: 1
Benjamin Kraus
Benjamin Kraus 2023년 2월 7일
Using patch as @Arif Hoq suggested is best, but you may also want to set the FaceAlpha and EdgeColor like this:
x=[1:10];
y=[1:10];
figure(1);clf;
plot(x,y,'b')
hold on
plot([1,10],2*[1,1],'--.k')
hold on
plot([1,10],4*[1,1],'--.k')
x = [1 10 10 1];
y = [2 2 4 4];
patch(x,y,'k','FaceAlpha',0.5,'EdgeColor','none')
xlim([1 10])

댓글을 달려면 로그인하십시오.


Sarvesh Kale
Sarvesh Kale 2023년 2월 7일
Hi ,
You can add the following extra line to your code
rectangle("Position",[1 2 10 2],"FaceColor",[150/255 150/255 150/255],"LineStyle","none");
This will shade the required portion with gray color, However I was not able to toggle its visibility so that the underlying slanted straight line is also visible.

카테고리

Help CenterFile Exchange에서 Labels and Annotations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by