version 1.1.0.0 (1.75 KB) by
John Bockstege

Function shades the area between two vectors.

This routine will shade the area of a 2-D plot between two user defined vectors. Simply pass the two vectors, and their corresponding horizontal coordinates, select your fill color and transparency level and viola! You have a figure that is colored in between the two input vectors.

John Bockstege (2020). Shade area between two curves (https://www.mathworks.com/matlabcentral/fileexchange/13188-shade-area-between-two-curves), MATLAB Central File Exchange. Retrieved .

Created with
R2006b

Compatible with any release

**Inspired:**
anomaly, Simpsons Rule Demonstration, gtfill(x,y1,y2,n,c1,c2), fill_between, SMA, MACD, RSI (SMR) Trading Algorithm, h-coefficient

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

Start Hunting!Create scripts with code, output, and formatted text in a single executable document.

Akilesh GWorks well , very simple

Thank you very much

Jonathan DavisFabio BonassiGreat tool, should be included in Matlab by default

Sophie Mutelclementina del preteI did not understand how to use it :(

Anna PelosiJustin PangJustin PangIs there any way of modifying this such that the shaded colour changes whenever the 'Upper' vector falls below the 'Lower' vector? At the moment it's difficult to tell if the vectors cross or diverge after contact. Otherwise, very helpful!

xinglong fangUllekh GambhiraThanks a lot! This solved the issue of not being able to assign multiple basevalues for multiple areas in a plot for me.

matthew riedybravo!

David CarvalhoNicolette SchwarzRoss Mattheisabderrazak chahidThank you very much. It's very helpful.

Dyna Hafyanereally helpful tool

Milad MehrHelene Le MevelShaghayegh JavadiThanks a lot. very helpful :) I had been looking for a proper method to shade the area between two curves. Matlab help is useless in this matter. without your help wouldn't be possible. thanks.

Gia Minh Thao NGUYENThank you so much for your great program and share. This program is helpful for my current research.

maria estebanAdminFelipe UribeKerwinThanks for the code! I adapted it to add regions of interest so you can shade just particular parts of the two curves (such as statistically different sections), using a logical vector. I also fixed the orientation thing.

function[fillhandle,msg]=jbfill(xpoints,upper,lower,roi,color,edge,add,transparency)

%% ADAPTED BY K. OLFERS 28-08-2016

%USAGE: [fillhandle,msg]=jbfill(xpoints,upper,lower,color,edge,add,transparency)

%This function will fill a region with a color between the two vectors provided

%using the Matlab fill command.

%

%fillhandle is the returned handle to the filled region in the plot.

%xpoints= The horizontal data points (ie frequencies). Note length(Upper)

% must equal Length(lower)and must equal length(xpoints)!

%upper = the upper curve values (data can be less than lower)

%lower = the lower curve values (data can be more than upper)

%roi = a *(logical) vector specifying the areas to be shaded

%color = the color of the filled area

%edge = the color around the edge of the filled area

%add = a flag to add to the current plot or make a new one.

%transparency is a value ranging from 1 for opaque to 0 for invisible for

%the filled color only.

%

%John A. Bockstege November 2006;

%Example:

% a=rand(1,20);%Vector of random data

% b=a+2*rand(1,20);%2nd vector of data points;

% x=1:20;%horizontal vector

% [ph,msg]=jbfill(x,a,b,rand(1,3),rand(1,3),0,rand(1,1))

% grid on

% legend('Datr')

if nargin<8;transparency=.5;end %default is to have a transparency of .5

if nargin<7;add=0;end %default is to add to current plot

if nargin<6;edge='k';end %dfault edge color is black

if nargin<5;color='b';end %default color is blue

if nargin<4; roi = ones(length(xpoints),1); end

if ~isrow(lower); lower = lower'; end

if ~isrow(upper); upper = upper'; end

if ~isrow(xpoints); xpoints = xpoints'; end

[clusters cnum] = bwlabeln(roi);

if ~add

figure;

plot([1:length(upper)],upper,[1:length(upper)],lower);

add = 1;

end

for iClust = 1:cnum

iStart = find(clusters==iClust,1);

clength = length(find(clusters == iClust))-1;

tmpUpper = upper(iStart:iStart+clength);

tmpLower = lower(iStart:iStart+clength);

tmpXpoints = xpoints (iStart:iStart+clength);

if length(tmpUpper)==length(tmpLower) && length(tmpLower)==length(tmpXpoints)

msg='';

filled=[tmpUpper,fliplr(tmpLower)];

tmpXpoints=[tmpXpoints,fliplr(tmpXpoints)];

if add

hold on

end

fillhandle=fill(tmpXpoints,filled,color);%plot the data

set(fillhandle,'EdgeColor',edge,'FaceAlpha',transparency,'EdgeAlpha',transparency);%set edge color

if add

hold off

end

else

msg='Error: Must use the same number of points in each vector';

end

end

Stephen SpearsEle DemariaExcellent tool! Thank you for sharing it.

AndreThat's correct. As Paul pointed out, the input vectors need to be rows. Thanks for the function and thanks Paul for the comment.

Andrepaul harderwould be nice if it didn't freak out with column vectors. works well if i transpose my data vectors, but it took me a bit to figure that out. thanks!

Francisco Hernandeznsc.cloneI applied spline interpolation to fit the curve. In this case, how can I use 'jbfill' function to shade area between two curves.

For example:

x=[0 100 200 300 400 500 600 700 800 900 1000 1100 1200 1300];

y=[0 28.23 27.3 20.94 13.89 6.268 2.468 0.1701 0.3704 0.1852 0.1852 0 0 0];

errBar=[0 3.366 4.936 4.969 2.294 2.353 1.568 0.1701 0.3704 0.1852 0.1852 0 0 0];

topcurve = y+errBar;

bottomcurve = y-errBar;

cs1=spapi(5,x,y);

fnplt(cs1);

hold on;

cs2=spapi(5,x,topcurve);

fnplt(cs2);

hold on;

cs3=spapi(5,x,bottomcurve);

fnplt(cs3);

hold on

Do you have a way to figure this out?

Alex DytsoPhilippeGreat!

LoganThis only works with data input as rows. To allow for column or row vector input, add the following:

if iscolumn(xpoints)

xpoints = xpoints';

upper = upper';

lower = lower';

end

Philip OhneweinMarkus Nennigerlooks excellent, but somehow it doesnt save as pdf properly...

SiddharthI was using this program but unable to save my figure as jpeg/tif/bmp or copy this figure. This just give a black picture. I do not understand this have anyone came across this before?

Stefano Gabrielethanks, very very useful

EnricoVery useful function. That really helped!

Thanks a lot!

AllenGreat.

Francesco MontorsiI use this function for plotting confidence intervals... it's very useful, thanks!

LindseyStephanVincentWorks great, thanks!

Timothy KinyanjuiGreat function

JavadTERRIFIC! Thanks.

ClaraEasy to use, and very useful... at least for those of us working in geophysics. Thanks

cehc84Braisnice function, nice help, straightforward use

Juangreat

JakobSimple and straight forward! Just what I needed.

Ulrich SachsVery nice. But because of the transparancy, Matlab switches to OpenGL, which leads to vanishing axis. To avoid that I just make the axis lines thicker set(gca,'LineWidth',1.0).

Because logarithmic plots do not support OpenGL, you can not draw transparant areas in logarithmic plots.

Grzegorz KnorVery smart solution.

RedmondThis function is great, thanks very much. I was hoping to use it to highlight the time-periods for which there was a statistical difference between the two lines. After creating the basic plot, I created a new matrix which specifies the time-points of interest (statistcially significant) in the first column, and the relevant data points for the two lines in the 2nd and 3rd column. I then ran jbfill on this. Unfortunately because the periods of interest are non-contiguous the function fills in the area in between. Is there a way to make it so that it wil only shade in areas at the specific time-points I have specified?

Robert Cummingvery good, and commented well (does what it says on the tin)

SRGMatlaboscopegood but row vectors VS column vectors as input does matter.

Andranik ValediBeautiful.

Ueli Rutishauserworks great,thanks!

the "transparancy" option comes with a caveat: it will switch the renderer to opengl. After that, a vector export is not possible anymore (matlab limitation). set to 1.0 to avoid this problem.

Ueli Rutishauserursula larkethank you!!!! this came in REALLY handy! easy to use, well documented! :)

Luis ConchaVery, very helpful and extremely easy to use. Great for confidence intervals!

Yasser SolimanGreat. This was very helpful and saved me lots of time to figure out the exact work that you've done. Thanks!

On another note, I need a few vector-couples the area between which need to be shadded, on the same plot. How can I incorporate that?

W. JohnsGood file, but the colour disappears when I switch to a vertical log scale. How can I fix this?

Ta MThanks, exactly what I needed!

Also thanks to Chris: put data in row vectors!

maka mohuif you make I run the following example, matlab logs me off my user account on SuSe 10.2

for i=1:25

figure

% copy the jbfill.m example from the comments here.

close

end

W. H. BraveVery good file, thank you.

M Hlovely function but it is removing box around figure as consequence of transparency.

Housam binousGreat job. Thanks for all.

Luke TaylorJust what I needed. Thanks!

H WExactly what I needed. Thanks for posting!

Marc FerrerWonderful function.

Matthias BrunnerVery nice. Thanks!

Chris KlinkWorks great, but apparently only when datapoint are horizontally organized (one row). If it's a column it doesn't work. You could mention this in the file.

Francis HossmeyerGreat! Solves a common problem.