help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Again with multiplot scripts


From: Vinayak Dutt
Subject: Again with multiplot scripts
Date: Mon, 3 Jul 95 19:55:21 CDT

Hi Octavers:


Rick made a suggestion to be to make subplot() function of mine
like the subplot() of MATLAB. So here's the modified version of
the subplot(0 along with the renamed subwindow(0 function and
the rest of multiplot functions. I hope this should fix all
the problems.


==Vinayak--

Note: Each of the function has to be saved to its respective *.m file.

%
% ------------ file bottom_title.m ----------------------------
%

function bottom_title (text)

% usage: bottom_title (text)
%
% NOTE: this will work only with gnuplot installed with
%       multiplot patch
%
% makes a title with the given text at the bottom of the plot 
% rather than the top.
%

  if (nargin != 1)
    error ("usage: bottom_title (text)");
  endif

  if (isstr (text))
    set top_title
    set title
    command = sprintf ("set bottom_title \"%s\"", text);
    eval (command);
  else
    error ("error: bottom_title: text must be a string");
  endif

endfunction

%
% ------------ file mplot.m ----------------------------
%

function mplot (...)

# usage: mplot (x, y)
#        mplot (x1, y1, x2, y2, ...)
#        mplot (x, y, fmt)
#
# This is a modified version of plot() command to work with
# multiplot version of gnuplot to plot multiple plots per page.
# This plot version automatically updates the plot position to
# next plot position after making the plot in the given subplot
# position.
#
# See command plot() for the various options to this command
# as this is just mulitplot version of the same command.
#

#
# global variables to keep track of multiplot options
#
  global multiplot_mode 
  global multi_xsize multi_ysize 
  global multi_xn multi_yn
  global multi_xi multi_yi
#
  set nologscale;
  set nopolar;

  plot_int ("plot", all_va_args);
#
# update the plot position
#
  if ( multiplot_mode == 1 )
        if ( multi_xi < multi_xn )
                multi_xi = multi_xi + 1;
        else
                multi_xi = 1;
                if( multi_yi < multi_xn )
                        multi_yi = multi_yi + 1;
                else
                        multi_yi = 1;
                endif;
        endif;
        xo = (multi_xi - 1.0)*multi_xsize;
        yo = (multi_yn - multi_yi)*multi_ysize;
        command = sprintf ("set origin %g,%g", xo,yo);
        eval (command);
  endif;
endfunction

%
% ------------ file multiplot.m ----------------------------
%

function multiplot (xn,yn)

% usage: multiplot (xn,yn)
%
% NOTE: this will work only with gnuplot installed with
%       multiplot patch
%
%    Sets and resets multiplot mode
%
%    if multiplot(0,0) then it will close multiplot mode and
%    and if arguments are non-zero, then it will set up
%    multiplot mode with xn,yn subplots along x and y axes.
%
% See other plotting commands too.

%
% global variables to keep track of multiplot options
%
  global multiplot_mode 
  global multi_xsize multi_ysize 
  global multi_xn multi_yn
  global multi_xi multi_yi
%
% check calling argument count

  if (nargin != 2)
        error "usage: multiplot (xn,yn)"
  endif

% check for scalar inputs

  if (!(is_scalar(xn) && is_scalar(yn)))
        error ("multiplot: xn and yn have to be scalars");
  endif

  xn = fix(xn);
  yn = fix(yn);

  if ( xn == 0 && yn == 0 )
        set nomultiplot;
        set size 1,1
        set origin 0,0
        multiplot_mode = 0;
        multi_xsize = 1;
        multi_ysize = 1;
        multi_xn = 1;
        multi_yn = 1;
        multi_xi = 1;
        multi_yi = 1;
        return;
  else
        if ( ( xn < 1 ) || ( yn < 1 ) )
                error ("multiplot: xn and yn have to be positive int");
        endif;

        set multiplot;
        xsize = 1.0 ./ xn;
        ysize = 1.0 ./ yn;
        command = sprintf ("set size %g,%g", xsize,ysize);
        eval (command);
        xo = 0.0;
        yo = (yn - 1.0)*ysize;
        command = sprintf ("set origin %g,%g", xo,yo);
        eval (command);
        multiplot_mode = 1;
        multi_xsize = xsize;
        multi_ysize = ysize;
        multi_xn = xn;
        multi_yn = yn;
        multi_xi = 1;
        multi_yi = 1;
  endif;

endfunction

%
% ------------ file oneplot.m ----------------------------
%

function oneplot ()

% usage: oneplot 
%
% NOTE: this will work only with gnuplot installed with
%       multiplot patch
%
% Switches from multiplot (if in  multiplot mode) to single plot
% mode
%

global multiplot_mode

set nomultiplot;
set size 1,1
set origin 0,0
multiplot_mode = 0;

endfunction

%
% ------------ file plot_border.m ----------------------------
%

function plot_border (side, ...)

% usage: plot_border (side, ...)
%
% NOTE: this will work only with gnuplot installed with
%       multiplot patch
%
% multiple arguments allowed to specify the sides on which
% the border is shown. allowed strings:
%
% allowed input strings:
%
%  "blank", "BLANK", "b", "B",   --->  No borders displayed
%    "all",   "ALL", "a", "A",   ---> All borders displayed
%  "north", "NORTH", "n", "N",   ---> North Border
%  "south", "SOUTH", "s", "S",   ---> South Border
%   "east",  "EAST", "e", "E",   --->  East Border
%   "west",  "WEST", "w", "W",   --->  West Border
%

  border = 0;
  arg = side;

  if ( ! isstr(arg) )
        error "plot_border: input not a string"
  endif;

% check first argument

  south = strcmp(arg,"south") || strcmp(arg,"SOUTH") ;
  south = south || strcmp(arg,"s") || strcmp(arg,"S") ;
  north = strcmp(arg,"north") || strcmp(arg,"NORTH") ;
  north = north || strcmp(arg,"n") || strcmp(arg,"N") ;
  east  = strcmp(arg,"east") || strcmp(arg,"EAST") ;
  east  =  east || strcmp(arg,"e") || strcmp(arg,"E") ;
  west  = strcmp(arg,"west") || strcmp(arg,"WEST") ;
  west  =  west || strcmp(arg,"w") || strcmp(arg,"W") ;
  all   = strcmp(arg,"all") || strcmp(arg,"ALL") ;
  all   =  all  || strcmp(arg,"a") || strcmp(arg,"A") ;
  blank = strcmp(arg,"blank") || strcmp(arg,"BLANK") ;
  blank = blank || strcmp(arg,"b") || strcmp(arg,"B") ;

  if ( south )
        border = 1;
  elseif ( north )
        border = 4;
  elseif ( east )
        border = 8;
  elseif ( west )
        border = 2;
  elseif ( all )
        border = 15;
  elseif ( blank )
        border = 0;
  endif;

%  check the remaining arguments

  va_start();
  while (--nargin)
          arg = va_arg();
          if ( ! isstr(arg) ) 
                error "plot_border: input not a string"
          endif;

          south = strcmp(arg,"south") || strcmp(arg,"SOUTH") ;
          south = south || strcmp(arg,"s") || strcmp(arg,"S") ;
          north = strcmp(arg,"north") || strcmp(arg,"NORTH") ;
          north = north || strcmp(arg,"n") || strcmp(arg,"N") ;
          east  = strcmp(arg,"east") || strcmp(arg,"EAST") ;
          east  =  east || strcmp(arg,"e") || strcmp(arg,"E") ;
          west  = strcmp(arg,"west") || strcmp(arg,"WEST") ;
          west  =  west || strcmp(arg,"w") || strcmp(arg,"W") ;
          all   = strcmp(arg,"all") || strcmp(arg,"ALL") ;
          all   =  all  || strcmp(arg,"a") || strcmp(arg,"A") ;
          blank = strcmp(arg,"blank") || strcmp(arg,"BLANK") ;
          blank = blank || strcmp(arg,"b") || strcmp(arg,"B") ;

          if ( south )
                border = border + 1;
          elseif ( north )
                border = border + 4;
          elseif ( east )
                border = border + 8;
          elseif ( west )
                border = border + 2;
          elseif ( all )
                border = 15;
          elseif ( blank )
                border = 0;
          endif;
%
  end;

  if ( border == 0 )
        set noborder;
  else
         command = sprintf ("set border %d", border);
        eval (command);
  endif;

endfunction

%
% ------------ file subplot.m ----------------------------
%

function subplot (xn,yn,pl)

% usage: subplot (xn,yn,pl)
%        or 
%        subplot(xyn)
%
% NOTE: this will work only with gnuplot installed with
%       multiplot patch
%
%    Sets gnuplot in multiplot mode and plots in location
%    given by pl (there are xb X yn subwindows)
%
%    if only one arg, then it has to be three digit arg
%    specifying the location in digit 1 and 2 and digit
%     3 is the plot number
%

%
% global variables to keep track of multiplot options
%
  global multiplot_mode 
  global multi_xn multi_yn
  global multi_xsize multi_ysize 

% check calling argument count

  if (nargin != 3 && nargin != 1)
        error "usage: subplot (xn,yn,p) or subplot(xnp)"
  endif

% check for scalar inputs

  if( nargin == 1 )
        if (!is_scalar(xn))
                error ("subplot: xn, yn, and pl  have to be scalars");
        endif
        xnp = xn;
        xn = fix(xnp/100);
        yn = fix((xnp - 100*xn)/10);
        pl = xnp - 100*xn - 10*yn;
  else
        if (!(is_scalar(xn) && is_scalar(yn) && is_scalar(pl)))
                error ("subplot: xn, yn, and pl  have to be scalars");
        endif
  endif
  xn = fix(xn);
  yn = fix(yn);
  pl = fix(pl);

  if (pl > xn*yn)
        error ("subplot: pl must be less than xn*yn");
  endif;

  if( xn < 1 || yn < 1 || pl < 1)
        error ("subplot: xn,yn,pl must be be positive");
  endif;

  if((xn*yn) == 1)
%
% switching to single plot ?
%
        set nomultiplot;
        set size 1,1
        set origin 0,0
        multi_xn = 1;
        multi_yn = 1;
        multiplot_mode = 0;
        return;
  endif;
%
% doing multiplot plots
%
   doagain = 0;
   if ( exist ("multiplot_mode") != 1 )
        doagain = 1;
   else 
        if ( (multiplot_mode != 1) || (multi_xn != xn) || (multi_yn != yn) )
                doagain = 1;
        endif;
   endif;
   if ( doagain )
        set multiplot;
        xsize = 1.0 ./ xn;
        ysize = 1.0 ./ yn;
        command = sprintf ("set size %g,%g", xsize,ysize);
        eval (command);
        multiplot_mode = 1;
        multi_xn = xn;
        multi_yn = yn;
   endif;
%
% get the sub plot location
%
  yp = fix((pl-1)/xn);
  xp = pl - yp*xn - 1;
  xp = xp + 1;
  yp = yp + 1;
%
% set the origin
%
  xsize = 1.0 ./ xn;
  ysize = 1.0 ./ yn;
  multi_xsize = xsize;
  multi_ysize = ysize;
  xo = (xp - 1.0)*xsize;
  yo = (yn - yp)*ysize;
  command = sprintf ("set origin %g,%g", xo,yo);
  eval (command);
%
endfunction

%
% ------------ file subwindow.m ----------------------------
%

function subwindow (xn,yn)

% usage: subwindow (xn,yn)
%
% NOTE: this will work only with gnuplot installed with
%       multiplot patch
%
%    Sets subwindow position in multiplot mode for next
% plot. The multiplot mode has to be previously initialized
% using multiplot() command, else this command just
% becomes an aliad to multiplot()
%

%
% global variables to keep track of multiplot options
%
  global multiplot_mode 
  global multi_xsize multi_ysize 
  global multi_xn multi_yn
%
% check calling argument count

  if (nargin != 2)
        error "usage: subwindow (xn,yn)"
  endif

% check for scalar inputs

  if (!(is_scalar(xn) && is_scalar(yn)))
        error ("subwindow: xn and yn have to be scalars");
  endif

  xn = fix(xn);
  yn = fix(yn);

%
% switch to multiplot mode if not already in, and
% use the args as the args to multiplot()
%
  if ( multiplot_mode ~= 1)
        multiplot(xn,yn)
        return
  endif;

% get the sub plot location

  if ( xn < 1 || xn > multi_xn || yn < 1 || yn > multi_yn )
        error ("subwindow: incorrect xn and yn");
  endif;

  xo = (xn - 1.0)*multi_xsize;
  yo = (multi_yn - yn)*multi_ysize;

  command = sprintf ("set origin %g,%g", xo,yo);
  eval (command);

endfunction

%
% ------------ file top_title.m ----------------------------
%

function top_title (text)

% usage: top_title (text)
%
% NOTE: this will work only with gnuplot installed with
%       multiplot patch
%
% makes a title with text "text" at the top of the plot 
%

  if (nargin != 1)
    error ("usage: top_title (text)");
  endif

  if (isstr (text))
    set bottom_title
    set title
    command = sprintf ("set top_title \"%s\"", text);
    eval (command);
  else
    error ("error: top_title: text must be a string");
  endif

endfunction

%
% ------------ file zlabel.m ----------------------------
%

function zlabel (text)

% usage: zlabel (text)
%
% Defines a label for the z-axis of a plot.  The label will appear the
% next time a plot is displayed.
%
% See other plotting commands also.

  if (nargin != 1)
    error ("usage: zlabel (text)");
  endif

  if (isstr (text))
    command = sprintf ("set zlabel \"%s\"", text);
    eval (command);
  else
    error ("error: zlabel: text must be a string");
  endif

endfunction




reply via email to

[Prev in Thread] Current Thread [Next in Thread]