Problem with fill()/patch() between two lines in R2023a
7 views (last 30 days)
Show older comments
Lorenzo Tortora
on 2 Nov 2023
Commented: Star Strider
on 2 Nov 2023
I have a problem with the fill()/patch() command when plotting experimental data.
I want to plot a sine wave with an atan background, and i want to make a picture in which the filled region is the one between the two waves. However, when i attempt to fill() (or patch()) i get a straigth line going from the first to the last point in the functions i create, and i don't know how to avoid this.
What am i doing wrong?
Here is the code for the plot:
%define the variable
x = 0:0.05:6.28
x = transpose(x)
bg = atan(-x)
linetofill = sin(x)
bg
%plot raw spectrum
plot(x, linetofill)
hold on
%fill the lines up to the background contribution
fill([x fliplr(x)], [linetofill fliplr(bg)], 'cyan', "FaceAlpha", 0.5)
hold off
1 Comment
Accepted Answer
Star Strider
on 2 Nov 2023
I am not certain what result you want.
For column vectors, use flip (or flipud) and concatenate the vector arguments vertically —
%define the variable
x = 0:0.05:6.28;
x = transpose(x);
bg = atan(-x);
linetofill = sin(x);
% bg
%plot raw spectrum
plot(x, linetofill)
hold on
%fill the lines up to the background contribution
patch([x; flip(x)], [linetofill; flip(bg)], 'cyan', "FaceAlpha", 0.5)
hold off
Is this what you want? (The fill function produces the same result. Use the function you are most comfortable wiith.)
.
2 Comments
More Answers (0)
See Also
Categories
Find more on Graphics Performance 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!