Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MATLAB, Filling in the area between two sets of data, lines in one figure

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:

http://img818.imageshack.us/img818/9519/iwantthisareafilledin.jpg

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.

like image 867
Josh Avatar asked Jun 05 '11 20:06

Josh


People also ask

How do you fill an area in Matlab?

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.

How do you plot area between two curves in Matlab?

plot(x, curve2, 'b', 'LineWidth', 2); x2 = [x, fliplr(x)]; inBetween = [curve1, fliplr(curve2)]; fill(x2, inBetween, 'g');


2 Answers

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 

enter image description here

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.

like image 164
Doresoom Avatar answered Sep 19 '22 15:09

Doresoom


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 ); 
like image 44
MrIO Avatar answered Sep 18 '22 15:09

MrIO