필터 지우기
필터 지우기

How to color some region of plot?

조회 수: 259 (최근 30일)
Enrica Brunetti
Enrica Brunetti 2019년 8월 25일
편집: Afiq Azaibi 2023년 3월 17일
If I have this plot
y = [70, 72, 60, 66, 60, 57, 59, 58, 60, 73, 72, 73, 73, 72, 52, 72, 77, 59, 49, 66];
x = [1: 20];
what function can I use to color some region of plot, when for example y(i) > 70?

채택된 답변

Image Analyst
Image Analyst 2019년 8월 25일
편집: Image Analyst 2019년 8월 25일
Try this,using patch():
% Plot original data.
y = [70, 72, 60, 66, 60, 57, 59, 58, 60, 73, 72, 73, 73, 72, 52, 72, 77, 59, 49, 66];
x = [1: 20];
plot(x, y, 'b*-');
grid on;
hold on;
% Make patch of transparent color.
yl = ylim
xl = xlim
xBox = [xl(1), xl(1), xl(2), xl(2), xl(1)]
yBox = [70, yl(2), yl(2), 70, 70]
patch(xBox, yBox, 'black', 'FaceColor', 'green', 'FaceAlpha', 0.1);
hold off;
  댓글 수: 4
Enrica Brunetti
Enrica Brunetti 2019년 9월 21일
But if I want to color with the use of the same code, the region for y(i)< 60?
Afiq Azaibi
Afiq Azaibi 2023년 3월 17일
편집: Afiq Azaibi 2023년 3월 17일
In addition to Image Analyst's solution, starting in R2023a you can use the xregion and yregion functions.
y = [70, 72, 60, 66, 60, 57, 59, 58, 60, 73, 72, 73, 73, 72, 52, 72, 77, 59, 49, 66];
x = [1: 20];
plot(x, y, 'b*-');
yregion(70,80,"FaceColor", 'g'); % Note, 80 is the end point so if you pan past 80, the region will stop.
yregion(45, 70, "FaceColor", 'r');
You can also set the EdgeColor properties to emphasize the edges but the default EdgeColor is 'none'.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Lighting, Transparency, and Shading에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by