Highlighting sections of a plot

조회 수: 20 (최근 30일)
Sophia van Mourik
Sophia van Mourik 2022년 2월 26일
댓글: Star Strider 2022년 8월 17일
Hi,
I have this plot below.
%% Plotting PU2
plot(wavelength,Day0_PU2,"-")
hold on
plot(wavelength,Day5_PU2,"-")
plot(wavelength,Day14_PU2p,"-")
% plot(wavelength,Day14_PU1b,"-")
% plot(wavelength,PU3,"-")
% plot(wavelength,PU4,"-")
xlim([400 4000])
hold off
title('Degradation of PU2 by Psuedomonas Esterase @37 Degree C')
ylabel('Transmittance %')
xlabel('Wavelength cm-1')
legend('Day0','Day5','Day14 ','Location','southwest');
xticklabels()
set ( gca, 'xdir', 'reverse' )
In the figure winow you can use the brush tool to highlight sections of the plot like so.
I was wondering if there is a way to do this through the code and highlight the whole vertical section of the plot to look something like this...
This graph is taken from using a patch function. I can use a patch on a defined curve but struggling to figure out how to patch a plot with a date plot. Would I have to extract the lines curve equation somehow? and then use the patch function.
My aim is to be able to load my IR spectra data into MATLAB, plot it and use a code to automatically pick out desirable wavelengths to highlight.
Is this possible?
Thanks,
S

채택된 답변

Star Strider
Star Strider 2022년 2월 26일
The patch function did not always work with datetime values.
It does in recent releases. However it does not appear that the variables in the posted plot are datetime variables. It wouold hellp to have the data.
x = linspace(0, 10, 500);
y = sin(2*pi*x/5);
idx = find(diff(sign(y-0.5))) % Regions Greater Than 0.5
idx = 1×4
21 104 271 354
idxm = reshape(idx,2,[])
idxm = 2×2
21 271 104 354
figure
plot(x, y)
yl = ylim;
hold on
for k = 1:size(idxm,2)
xrng = idxm(1,k) : idxm(2,k);
patch([x(xrng) flip(x(xrng))], [min(ylim)*ones(size(y(xrng))) flip(y(xrng))], 'r', 'FaceAlpha',0.5)
end
hold off
grid
.
  댓글 수: 4
Miguel De Leon
Miguel De Leon 2022년 8월 17일
Hello, I am new to matlab and trying to do the same thing. Could you explain this part of the code please (y = sin(2*pi*x/5)?
Star Strider
Star Strider 2022년 8월 17일
@Miguel De Leon — That creates a vector of dependent variable values necessary to illustrate how the code works and the result. It could be any appropriate function.

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by