MATLAB Answers

How to color some region of plot?

184 views (last 30 days)
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?

  0 Comments

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 25 Aug 2019
Edited: Image Analyst on 25 Aug 2019
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;

  3 Comments

Enrica Brunetti
Enrica Brunetti on 26 Aug 2019
Thank you, but if I want to underline ( to color) the value of 'x', that corrisponds to the value of y(i)>70?
Image Analyst
Image Analyst on 27 Aug 2019
Do you mean like the x values (tick labels) under the x axis??? Please post an image of what you'd like.
Enrica Brunetti
Enrica Brunetti on 21 Sep 2019
But if I want to color with the use of the same code, the region for y(i)< 60?

Sign in to comment.

More Answers (0)

Sign in to answer this question.