How to add a patch to a plot of data?
23 views (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
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
idxm = reshape(idx,2,[])
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
.
0 Comments
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!