Azzera filtri
Azzera filtri

How to color some region of plot?

258 visualizzazioni (ultimi 30 giorni)
Enrica Brunetti
Enrica Brunetti il 25 Ago 2019
Modificato: Afiq Azaibi il 17 Mar 2023
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?

Risposta accettata

Image Analyst
Image Analyst il 25 Ago 2019
Modificato: Image Analyst il 25 Ago 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;
  4 Commenti
Enrica Brunetti
Enrica Brunetti il 21 Set 2019
But if I want to color with the use of the same code, the region for y(i)< 60?
Afiq Azaibi
Afiq Azaibi il 17 Mar 2023
Modificato: Afiq Azaibi il 17 Mar 2023
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'.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Lighting, Transparency, and Shading in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by