[scilab-Users] Reg :: Area Between two curves

Antoine Monmayrant antoine.monmayrant at laas.fr
Wed Oct 5 08:54:00 CEST 2011


Le 04/10/2011 22:41, Samuel Gougeon a écrit :
> Le 04/10/2011 15:47, Sumit Adhikari a écrit :
>> Hello All,
>>
>> If I have two curves then how do I shade the area between two curves 
>> in scilab.
>>
>> I am plotting data files and using scilab plot function.
> There are at least 4 ways for doing that. The best one depends on if 
> your curves
> share the same x or not, and/or if they are crossing each others or 
> not...
>
> After a plot, you may use  e = gce(); e=e.children(1);  and then
> either e.polyline_style=5;  e.foreground=<index of the color you want>
> or e.fill_mode="on"; e.background=<index of the color you want>
> xfpoly(...) could also be used.
> Anyway, you will likely have to complete your data by adding a heading
> and a trailing well-chosen point to each curve.
>
> Have a try and optimize according to your data.
>
> The area between both curves may also be considered as a polygone to 
> be filled.
>
> HTH
> Samuel

For two curves y1 and y2 that share the same x axis, I have made this 
quick and dirty function:

BetweenCurves(x,y1,y2);

Here is an example:

x=[-10:10];y1=x+10;y2=x.*x;
BetweenCurves(x,y1,y2);


Here is the source code below:


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Plot area between two curves
function [h,epoly,ey1,ey2]=BetweenCurves(x,y1,y2,varargin)
//
//      Plots two curves and fill the area in between
//
//      INPUTS:
//              x       vector (1,n) of horizontal coordinates
//              y1      vector (1,n) value of 1st curve y1(x)
//              y2      vector (1,n) value of 2nd curve y2(x)
//       -- optional inputs: pairs "keyword","value" --
//              "handle",h      handle to the graphic window to use
//              "axis",a      handle to the graphic axis to use
//              "foreground", colorid   id of the color to use for 
painting the area
//              "background", colorid   id of the color to use for 
curves stroke
//
//      OUTPUTS:
//              h       handle to the graphic window used
//              epoly   handle to the polygone that fill the area in between
//              ey1     handle to first curve
//              ey2     handle to second curve

//default values for optional argument
       hfig=-1;
       background=%nan;
       foreground=%nan;
// scan varargin for optional parameter pairs (they can appear in any order)
    for i=1:2:length(varargin)
       keyword=varargin(i);
       value=varargin(i+1);
       select keyword
          case "handle" then
             hfig=value;
             scf(hfig);
          case "axis" then
             axis=value;
             sca(axis);
             hfig=axis.parent;
          case "background" then
             background=value;
          case "foreground" then
             background=value;
       end
    end
// special treatment for handle (aka hack alert)
    if typeof(hfig) ~= "handle" then
       hfig=scf();
    end
    h=hfig;
    scf(hfig);
    xfpoly([x,x($:-1:1)],[y1,y2($:-1:1)]);
    epoly=gce();
    plot(x,y1);
    ey1=gce();
    plot(x,y2);
    ey2=gce();
// background setting
    if (~isnan(background)) then
       // optional background specified
       epoly.background=background;
    else
       // default background
       epoly.background=color("gray87");
    end
// foreground setting (as for background)
    if (~isnan(foreground)) then
       epoly.foreground=foreground;
       ey1.children.foreground=foreground;
       ey2.children.foreground=foreground;
    else
       epoly.foreground=color("gray65");
       ey1.children.foreground=color("gray65");
       ey2.children.foreground=color("gray65");
    end
endfunction
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

It's far from perfect but it does what I need.
Hope it helps,

Antoine



More information about the users mailing list