How to add a patch to a plot of data?

23 views (last 30 days)
Sophia van Mourik
Sophia van Mourik on 26 Feb 2022
Answered: Star Strider on 26 Feb 2022
Hi,
I have a plot of Data (wavelength vs transmission) and I was to highlight sections on the plot independent of y axis. Imagine highlighting certain wavelengths along the x axis. I think I need to use a patch function but not sure how to define the rectangle shape ontop of the existing data plot and get it to match.

Answers (1)

Star Strider
Star Strider on 26 Feb 2022
I am not certain what your function or data are or what you want to plot.
See if this helps to cliarify the general approach —
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
.

Community Treasure Hunt

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

Start Hunting!