1########################################################################
2##
3## Copyright (C) 1993-2021 The Octave Project Developers
4##
5## See the file COPYRIGHT.md in the top-level directory of this
6## distribution or <https://octave.org/copyright/>.
7##
8## This file is part of Octave.
9##
10## Octave is free software: you can redistribute it and/or modify it
11## under the terms of the GNU General Public License as published by
12## the Free Software Foundation, either version 3 of the License, or
13## (at your option) any later version.
14##
15## Octave is distributed in the hope that it will be useful, but
16## WITHOUT ANY WARRANTY; without even the implied warranty of
17## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18## GNU General Public License for more details.
19##
20## You should have received a copy of the GNU General Public License
21## along with Octave; see the file COPYING.  If not, see
22## <https://www.gnu.org/licenses/>.
23##
24########################################################################
25
26## -*- texinfo -*-
27## @deftypefn  {} {} contour (@var{z})
28## @deftypefnx {} {} contour (@var{z}, @var{vn})
29## @deftypefnx {} {} contour (@var{x}, @var{y}, @var{z})
30## @deftypefnx {} {} contour (@var{x}, @var{y}, @var{z}, @var{vn})
31## @deftypefnx {} {} contour (@dots{}, @var{style})
32## @deftypefnx {} {} contour (@var{hax}, @dots{})
33## @deftypefnx {} {[@var{c}, @var{h}] =} contour (@dots{})
34## Create a 2-D contour plot.
35##
36## Plot level curves (contour lines) of the matrix @var{z}, using the
37## contour matrix @var{c} computed by @code{contourc} from the same
38## arguments; see the latter for their interpretation.
39##
40## The appearance of contour lines can be defined with a line style @var{style}
41## in the same manner as @code{plot}.  Only line style and color are used;
42## Any markers defined by @var{style} are ignored.
43##
44## If the first argument @var{hax} is an axes handle, then plot into this axes,
45## rather than the current axes returned by @code{gca}.
46##
47## The optional output @var{c} contains the contour levels in @code{contourc}
48## format.
49##
50## The optional return value @var{h} is a graphics handle to the hggroup
51## comprising the contour lines.
52##
53## Example:
54##
55## @example
56## @group
57## x = 0:3;
58## y = 0:2;
59## z = y' * x;
60## contour (x, y, z, 2:3)
61## @end group
62## @end example
63##
64## @seealso{ezcontour, contourc, contourf, contour3, clabel, meshc, surfc, caxis, colormap, plot}
65##
66## @end deftypefn
67
68function [c, h] = contour (varargin)
69
70  [hax, varargin] = __plt_get_axis_arg__ ("contour", varargin{:});
71
72  oldfig = [];
73  if (! isempty (hax))
74    oldfig = get (0, "currentfigure");
75  endif
76  unwind_protect
77    hax = newplot (hax);
78
79    [ctmp, htmp] = __contour__ (hax, "none", varargin{:});
80
81    if (! ishold ())
82      set (hax, "box", "on");
83    endif
84
85  unwind_protect_cleanup
86    if (! isempty (oldfig))
87      set (0, "currentfigure", oldfig);
88    endif
89  end_unwind_protect
90
91  if (nargout > 0)
92    c = ctmp;
93    h = htmp;
94  endif
95
96endfunction
97
98
99%!demo
100%! clf;
101%! colormap ("default");
102%! [x, y, z] = peaks ();
103%! contour (x, y, z);
104%! title ({"contour() plot (isolines of constant Z)"; "Z = peaks()"});
105
106%!demo
107%! clf;
108%! colormap ("default");
109%! [theta, r] = meshgrid (linspace (0,2*pi,64), linspace (0,1,64));
110%! [X, Y] = pol2cart (theta, r);
111%! Z = sin (2*theta) .* (1-r);
112%! contour (X, Y, abs (Z), 10);
113%! title ({"contour() plot"; "polar fcn: Z = sin (2*theta) * (1-r)"});
114
115%!demo
116%! clf;
117%! colormap ("default");
118%! z = peaks ();
119%! contour (z, [0 0]);
120%! title ({"contour() plot with single isoline at Z == 0"; "Z = peaks()"});
121
122%!test
123%! hf = figure ("visible", "off");
124%! clf (hf);
125%! unwind_protect
126%!   [x, y, z] = peaks ();
127%!   [c, h] = contour (x, y, z);
128%!   levellist = -6:6;
129%!   set (h, "levellist", levellist);
130%!   assert (get (h, "levellist"), levellist);
131%!   assert (get (h, "levellistmode"), "manual");
132%! unwind_protect_cleanup
133%!   close (hf);
134%! end_unwind_protect
135
136%!test
137%! hf = figure ("visible", "off");
138%! clf (hf);
139%! unwind_protect
140%!   [x, y, z] = peaks ();
141%!   [c, h] = contour (x, y, z);
142%!   levelstep = 3;
143%!   set (h, "levelstep", levelstep);
144%!   assert (get (h, "levelstep"), levelstep);
145%!   assert (get (h, "levelstepmode"), "manual");
146%!   assert (get (h, "levellist"), -6:levelstep:6);
147%!   assert (get (h, "levellistmode"), "auto");
148%! unwind_protect_cleanup
149%!   close (hf);
150%! end_unwind_protect
151