How do I highlight a certain area between two lines?
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Ben van Zon
le 22 Déc 2022
Réponse apportée : Star Strider
le 22 Déc 2022
I'm trying to graph measured CO2 levels compared to the safe range according to the WHO.
But when I use the patch command, but that highlights the ENTIRE area, and not the the area that crosses the high concentration.
4 commentaires
Réponse acceptée
Star Strider
le 22 Déc 2022
The ‘Time’ format makes absolutely no sense to me, and readmatrix returns NaN for that column, so the posted code image will not work to convert it.
The ‘t’ and ‘CO2’ vectors need to be interpolated to a finer resolution than in the initial file for this to work correctly. Then, just use logical indexing to define the appropriate regions —
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1239957/DataTest2.xlsx', 'VariableNamingRule','preserve')
L = size(T1,1);
t = linspace(0, L-1, L).';
tv = linspace(min(t), max(t), 10*L); % Increase Resolution
CO2 = T1.CO2;
CO2v = interp1(t, CO2, tv); % Increase Resolution
SafeRange = [400 1000];
LvLo = CO2v >= SafeRange(1); % Logical Vector
LvHi = CO2v <= SafeRange(2); % Logical Vector
v1 = ones(size(t));
v1v = ones(size(tv));
figure
plot(tv, CO2v)
hold on
plot(tv(~LvHi), CO2v(~LvHi), 'r')
patch([tv(~LvHi) flip(tv(~LvHi))], [CO2v(~LvHi) SafeRange(2)*flip(v1v(~LvHi))], 'r')
hold off
ylim([0 1200])
yline(SafeRange, '-k', 'DisplayName','Safe Range')
.
0 commentaires
Plus de réponses (2)
Chunru
le 22 Déc 2022
% Generate some data
t = 0:.1:10;
y = sin(t);
plot(t, y);
hold on
% highlight a portion of data
idx = t>6 & t<9;
area(t(idx), y(idx))
0 commentaires
Voir également
Catégories
En savoir plus sur Polygons dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!