MATLAB Answers

0

How to color some region of plot?

Asked by Enrica Brunetti on 25 Aug 2019 at 17:18
Latest activity Commented on by Image Analyst
on 27 Aug 2019 at 2:40
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.

1 Answer

Answer by Image Analyst
on 25 Aug 2019 at 18:06
Edited by Image Analyst
on 25 Aug 2019 at 18:07
 Accepted Answer

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;

  2 Comments

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
on 27 Aug 2019 at 2:40
Do you mean like the x values (tick labels) under the x axis??? Please post an image of what you'd like.

Sign in to comment.