How do I highlight a certain area between two lines?

조회 수: 5 (최근 30일)
Ben van Zon
Ben van Zon 2022년 12월 22일
답변: Star Strider 2022년 12월 22일
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
KSSV
KSSV 2022년 12월 22일
Attach your data and show us what region you want to highlight.
Ben van Zon
Ben van Zon 2022년 12월 22일
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

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2022년 12월 22일
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')
.

추가 답변 (2개)

Chunru
Chunru 2022년 12월 22일
% 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 2022년 12월 22일

카테고리

Help CenterFile Exchange에서 Polygons에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by