How do I highlight a certain area between two lines?
조회 수: 5 (최근 30일)
이전 댓글 표시
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
채택된 답변
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')
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
추가 답변 (2개)
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))
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Polygons에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!