Mercurial > hg > octave-lyh
changeset 17049:0322e057697f
hold.m, grid.m, box.m: Update to use new __plt_get_axis_arg__.
* scripts/plot/box.m: Update to use new __plt_get_axis_arg__.
Use hax instead of ax. Redo docstring.
* scripts/plot/grid.m: Update to use new __plt_get_axis_arg__.
Use hax instead of ax. Redo docstring. Move input validation
to front of function.
* scripts/plot/hold.m: Update to use new __plt_get_axis_arg__.
Use hax instead of ax. Redo docstring. Add 2 new %!demos.
author | Pantxo Diribarne <pantxo.diribarne@gmail.com> |
---|---|
date | Tue, 23 Jul 2013 15:10:57 +0200 |
parents | 3f99d7d22bd0 |
children | 9ff7d4849f03 |
files | scripts/plot/box.m scripts/plot/grid.m scripts/plot/hold.m |
diffstat | 3 files changed, 114 insertions(+), 70 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/plot/box.m +++ b/scripts/plot/box.m @@ -17,11 +17,12 @@ ## <http://www.gnu.org/licenses/>. ## -*- texinfo -*- -## @deftypefn {Function File} {} box -## @deftypefnx {Function File} {} box ("on") -## @deftypefnx {Function File} {} box ("off") +## @deftypefn {Function File} {} box on +## @deftypefnx {Function File} {} box off +## @deftypefnx {Function File} {} box ## @deftypefnx {Function File} {} box (@var{hax}, @dots{}) -## Control the display of a border around the plot. +## Control the display of a border around the current axis. +## ## The argument may be either @code{"on"} or @code{"off"}. If it is ## omitted, the current box state is toggled. ## @@ -34,10 +35,14 @@ function box (varargin) - [ax, varargin, nargs] = __plt_get_axis_arg__ ("box", varargin{:}); + [hax, varargin, nargs] = __plt_get_axis_arg__ ("box", varargin{:}); + if (isempty (hax)) + hax = gca (); + endif + if (nargs == 0) - box_state = get (ax, "box"); + box_state = get (hax, "box"); if (strcmp (box_state, "on")) box_state = "off"; else @@ -60,6 +65,6 @@ print_usage (); endif - set (ax, "box", box_state); + set (hax, "box", box_state); endfunction
--- a/scripts/plot/grid.m +++ b/scripts/plot/grid.m @@ -17,83 +17,98 @@ ## <http://www.gnu.org/licenses/>. ## -*- texinfo -*- -## @deftypefn {Function File} {} grid (@var{arg}) -## @deftypefnx {Function File} {} grid ("minor", @var{arg2}) +## @deftypefn {Function File} {} grid +## @deftypefnx {Function File} {} grid on +## @deftypefnx {Function File} {} grid off +## @deftypefnx {Function File} {} grid minor +## @deftypefnx {Function File} {} grid minor on +## @deftypefnx {Function File} {} grid minor off ## @deftypefnx {Function File} {} grid (@var{hax}, @dots{}) -## Force the display of a grid on the plot. -## The argument may be either @code{"on"}, or @code{"off"}. +## Control the display of grid lines on a plot. +## +## The function input may be either @code{"on"}, or @code{"off"}. ## If it is omitted, the current grid state is toggled. ## -## If @var{arg} is @code{"minor"} then the minor grid is toggled. When -## using a minor grid a second argument @var{arg2} is allowed, which can -## be either @code{"on"} or @code{"off"} to explicitly set the state of -## the minor grid. +## If the first argument is @code{"minor"} then all further commands +## modify the minor grid rather than the major grid. ## ## If the first argument is an axis handle, @var{hax}, operate on the ## specified axis object. -## @seealso{plot} +## +## To control the grid lines for an individual axis use the @code{set} +## function. For example, +## +## @example +## set (gca, "ygrid", "on"); +## @end example +## @seealso{box} ## @end deftypefn ## Author: jwe function grid (varargin) - [ax, varargin, nargs] = __plt_get_axis_arg__ ("grid", varargin{:}); + [hax, varargin, nargs] = __plt_get_axis_arg__ ("grid", varargin{:}); - grid_on = (strcmp (get (ax, "xgrid"), "on") - && strcmp (get (ax, "ygrid"), "on") - && strcmp (get (ax, "zgrid"), "on")); - - minor_on = (strcmp (get (ax, "xminorgrid"), "on") - && strcmp (get (ax, "yminorgrid"), "on") - && strcmp (get (ax, "zminorgrid"), "on")); - + if (isempty (hax)) + hax = gca (); + endif + if (nargs > 2) print_usage (); - elseif (nargs == 0) + endif + + grid_on = ( strcmp (get (hax, "xgrid"), "on") + && strcmp (get (hax, "ygrid"), "on") + && strcmp (get (hax, "zgrid"), "on")); + + minor_on = ( strcmp (get (hax, "xminorgrid"), "on") + && strcmp (get (hax, "yminorgrid"), "on") + && strcmp (get (hax, "zminorgrid"), "on")); + + if (nargs == 0) grid_on = ! grid_on; else x = varargin{1}; - if (ischar (x)) - if (strcmpi (x, "off")) - grid_on = false; - elseif (strcmpi (x, "on")) - grid_on = true; - elseif (strcmpi (x, "minor")) - if (nargs == 2) - x2 = varargin{2}; - if (strcmpi (x2, "on")) - minor_on = true; - grid_on = true; - elseif (strcmpi (x2, "off")) - minor_on = false; - else - print_usage (); - endif + if (! ischar (x)) + error ("grid: argument must be a string"); + endif + if (strcmpi (x, "off")) + grid_on = false; + elseif (strcmpi (x, "on")) + grid_on = true; + elseif (strcmpi (x, "minor")) + if (nargs == 2) + x2 = varargin{2}; + if (strcmpi (x2, "on")) + minor_on = true; + grid_on = true; + elseif (strcmpi (x2, "off")) + minor_on = false; else - minor_on = ! minor_on; - if (minor_on) - grid_on = true; - endif + print_usage (); endif else - print_usage (); + minor_on = ! minor_on; + if (minor_on) + grid_on = true; + endif endif else - error ("grid: argument must be a string"); + print_usage (); endif endif if (grid_on) - set (ax, "xgrid", "on", "ygrid", "on", "zgrid", "on"); + set (hax, "xgrid", "on", "ygrid", "on", "zgrid", "on"); if (minor_on) - set (ax, "xminorgrid", "on", "yminorgrid", "on", "zminorgrid", "on"); + set (hax, "xminorgrid", "on", "yminorgrid", "on", "zminorgrid", "on"); else - set (ax, "xminorgrid", "off", "yminorgrid", "off", "zminorgrid", "off"); + set (hax, "xminorgrid", "off", "yminorgrid", "off", "zminorgrid", "off"); endif else - set (ax, "xgrid", "off", "ygrid", "off", "zgrid", "off"); - set (ax, "xminorgrid", "off", "yminorgrid", "off", "zminorgrid", "off"); + set (hax, "xgrid", "off", "ygrid", "off", "zgrid", "off", + "xminorgrid", "off", "yminorgrid", "off", "zminorgrid", "off"); endif endfunction
--- a/scripts/plot/hold.m +++ b/scripts/plot/hold.m @@ -18,7 +18,9 @@ ## -*- texinfo -*- ## @deftypefn {Command} {} hold -## @deftypefnx {Command} {} hold @var{state} +## @deftypefnx {Command} {} hold on +## @deftypefnx {Command} {} hold off +## @deftypefnx {Command} {} hold all ## @deftypefnx {Function File} {} hold (@var{hax}, @dots{}) ## Toggle or set the "hold" state of the plotting engine which determines ## whether new graphic objects are added to the plot or replace the existing @@ -30,13 +32,13 @@ ## on a single graph. ## ## @item hold all -## Retain plot line color, line style, data and settings so that subsequent +## Retain plot line color, line style, data, and settings so that subsequent ## plot commands are displayed on a single graph with the next line color and ## style. ## ## @item hold off -## Clear plot and restore default graphics settings before each new plot -## command. (default). +## Restore default graphics settings which clear the graph and reset axis +## properties before each new plot command. (default). ## ## @item hold ## Toggle the current hold state. @@ -46,26 +48,30 @@ ## only for the given axis handle. ## ## To query the current hold state use the @code{ishold} function. -## @seealso{ishold, cla, newplot, clf} +## @seealso{ishold, cla, clf, newplot} ## @end deftypefn function hold (varargin) - if (nargin > 0 && numel (varargin{1}) == 1 && ishandle (varargin{1}) + if (nargin > 0 && isscalar (varargin{1}) && ishandle (varargin{1}) && strcmp (get (varargin{1}, "type"), "axes")) - [ax, varargin, nargs] = __plt_get_axis_arg__ ("hold", varargin{:}); - fig = get (ax, "parent"); + [hax, varargin, nargs] = __plt_get_axis_arg__ ("hold", varargin{:}); + if (isempty (hax)) + hax = gca (); + endif + ## FIXME: Should this be ancestor (hax, "parent")? + hfig = get (hax, "parent"); elseif (nargin > 0 && numel (varargin{1}) > 1 && ishandle (varargin{1})) print_usage (); else - ax = gca (); - fig = gcf (); + hax = gca (); + hfig = gcf (); nargs = numel (varargin); endif hold_all = false; if (nargs == 0) - turn_hold_off = ishold (ax); + turn_hold_off = ishold (hax); elseif (nargs == 1) state = varargin{1}; if (ischar (state)) @@ -85,18 +91,36 @@ endif if (turn_hold_off) - set (ax, "nextplot", "replace"); + set (hax, "nextplot", "replace"); else - set (ax, "nextplot", "add"); - set (fig, "nextplot", "add"); + set (hax, "nextplot", "add"); + set (hfig, "nextplot", "add"); endif - set (ax, "__hold_all__", hold_all); + set (hax, "__hold_all__", hold_all); endfunction %!demo %! clf; +%! t = linspace (0, 2*pi, 100); +%! plot (t, sin (t)); +%! hold on; +%! plot (t, cos (t)); +%! title ({'hold on', '2 plots shown on same graph'}); +%! hold off; + +%!demo +%! clf; +%! t = linspace (0, 2*pi, 100); +%! plot (t, sin (t)); +%! hold all; +%! plot (t, cos (t)); +%! title ({'hold all', '2 plots shown on same graph with linestyle also preserved'}); +%! hold off; + +%!demo +%! clf; %! A = rand (100); %! [X, Y] = find (A > 0.9); %! imshow (A); @@ -143,7 +167,7 @@ %! colorbar ('SouthOutside'); %! title ('Test script for some plot functions'); -##hold on +## hold on test %!test %! hf = figure ("visible", "off"); %! unwind_protect @@ -159,7 +183,7 @@ %! close (hf); %! end_unwind_protect -##hold off +## hold off test %!test %! hf = figure ("visible", "off"); %! unwind_protect