How do I highlight a certain area between two lines?

5 views (last 30 days)
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 Comments
KSSV
KSSV on 22 Dec 2022
Attach your data and show us what region you want to highlight.
Ben van Zon
Ben van Zon on 22 Dec 2022
I attached the xlsx file, only need the data with the CO2 values, so row 11.
And I want to highlight the circled area
So the part where the concentration goes above the specified limit of 1000 ppm

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 22 Dec 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')
T1 = 346×13 table
PM1.0 PM2.5 PM10 NO2 C2H5CH VOC CO Temp Press Humi CO2 Time i _____ _____ ____ ___ ______ ___ __ _____ _____ ____ ______ _____________________________ __ 0 0 0 391 83 243 58 22.27 73145 70 912.31 {'2022:41:12/20/22:16:41:15'} 0 0 0 0 392 83 243 59 22.27 73145 70 911.64 {'2022:41:12/20/22:16:41:26'} 1 0 0 0 391 83 243 59 22.27 73145 70 911.57 {'2022:41:12/20/22:16:41:37'} 2 0 0 0 391 83 243 59 22.27 73145 70 911.15 {'2022:41:12/20/22:16:41:48'} 3 0 0 0 392 83 244 60 22.27 73145 70 910.73 {'2022:41:12/20/22:16:41:59'} 4 0 0 0 392 83 244 59 22.27 73145 70 908.55 {'2022:42:12/20/22:16:42:10'} 5 0 0 0 392 82 244 60 22.27 73145 70 906.42 {'2022:42:12/20/22:16:42:21'} 6 0 0 0 392 83 244 60 22.27 73145 70 905.56 {'2022:42:12/20/22:16:42:32'} 7 0 0 0 392 83 244 60 22.27 73145 70 904.37 {'2022:42:12/20/22:16:42:43'} 8 0 0 0 392 83 244 59 22.27 73145 70 907.3 {'2022:42:12/20/22:16:42:54'} 9 0 0 0 391 82 243 60 22.27 73145 70 960.94 {'2022:43:12/20/22:16:43:05'} 10 0 0 0 392 83 243 60 22.27 73145 70 976.15 {'2022:43:12/20/22:16:43:16'} 11 0 0 0 391 82 244 60 22.27 73145 70 978.85 {'2022:43:12/20/22:16:43:27'} 12 0 0 0 392 82 244 60 22.27 73145 70 971.3 {'2022:43:12/20/22:16:43:38'} 13 0 0 0 392 83 244 60 22.27 73145 70 965.23 {'2022:43:12/20/22:16:43:49'} 14 0 0 0 392 82 244 60 22.27 73145 70 956.8 {'2022:44:12/20/22:16:44:00'} 15
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')
.

More Answers (2)

Chunru
Chunru on 22 Dec 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))

Image Analyst
Image Analyst on 22 Dec 2022

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!