How do I highlight a certain area between two lines?
9 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Ben van Zon
am 22 Dez. 2022
Beantwortet: Star Strider
am 22 Dez. 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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1239932/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1239937/image.png)
4 Kommentare
Akzeptierte Antwort
Star Strider
am 22 Dez. 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 Kommentare
Weitere Antworten (2)
Chunru
am 22 Dez. 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 Kommentare
Siehe auch
Kategorien
Mehr zu Polygons finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!