I have a question about using the area
function; or perhaps another function is in order... I created this plot from a large text file:
The green and the blue represent two different files. What I want to do is fill in the area between the red line and each run, respectively. I can create an area plot with a similar idea, but when I plot them on the same figure, they do not overlap correctly. Essentially, 4 plots would be on one figure.
I hope this makes sense.
fill( X , Y , C ) plots filled polygonal regions as patches with vertices at the (x,y) locations specified by X and Y . To plot one region, specify X and Y as vectors. To plot multiple regions, specify X and Y as matrices where each column corresponds to a polygon.
plot(x, curve2, 'b', 'LineWidth', 2); x2 = [x, fliplr(x)]; inBetween = [curve1, fliplr(curve2)]; fill(x2, inBetween, 'g');
Building off of @gnovice's answer, you can actually create filled plots with shading only in the area between the two curves. Just use fill
in conjunction with fliplr
.
Example:
x=0:0.01:2*pi; %#initialize x array y1=sin(x); %#create first curve y2=sin(x)+.5; %#create second curve X=[x,fliplr(x)]; %#create continuous x value array for plotting Y=[y1,fliplr(y2)]; %#create y values for out and then back fill(X,Y,'b'); %#plot filled area
By flipping the x array and concatenating it with the original, you're going out, down, back, and then up to close both arrays in a complete, many-many-many-sided polygon.
Personally, I find it both elegant and convenient to wrap the fill function. To fill between two equally sized row vectors Y1
and Y2
that share the support X
(and color C):
fill_between_lines = @(X,Y1,Y2,C) fill( [X fliplr(X)], [Y1 fliplr(Y2)], C );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With