Simple.pm

filter: Statementstmt 31.4% 117 / 372 Branchbran 9.6% 21 / 218 Conditioncond 0.0% 0 / 117 MC/DCmcdc 0.0% 0 / 63 Subroutinesub 70.0% 21 / 30 Podpod 100% 15 / 15 totaltotal 21.3% 174 / 815 SCAR 94.9
CC
158
Cov
20%
plot
9312.0
new
285.1
_regularize_size
132.0
CRAP 13171.5
↑ Summary Simple/PLplot.pm »
1 =head1 NAME
2  
3 PDL::Graphics::Simple - Simple backend-independent plotting for PDL
4  
5 =head1 SYNOPSIS
6
7 # Simple interface - throw plots up on-screen, ASAP
8 use PDL::Graphics::Simple;
9 imag $a; # Display an image PDL
10 imag $a, 0, 300; # Display with color range
11 line $rrr, $fit; # Plot a line
12
13 points $rr, $sec; # Plot points
14 hold; # Hold graphics so subsequent calls overplot
15 line $rrr, $fit; # Overplot a line in a contrasting color
16 release; # Release graphics
17  
18 # Object interface - simple plotting, to file or screen
19 an$w = pgswin( size=>[8,4], multi=>[2,2] ); # 2x2 plot grid on an 8"x4" window
20 $w = pgswin( size=>[1000,1000,'px'], output=>'plot.png' ); # output to a PNG
21  
22 $w->plot( with=>'points', $rr, $sec, with=>'line', $rrr, $fit,
23 {title=>"Points and fit", xlabel=>"Abscissa", ylabel=>"Ordinate"});
24  
25 =head1 DESCRIPTION
26  
27 PDL can plot through a plethora of external plotting modules. Each
28 module tends to be less widely available than Perl itself, and to
29 require an additional step or two to install. For simple applications
30 ("throw up an image on the screen", or "plot a curve") it is useful to
31 have a subset of all plotting capability available in a backend-independent
32 layer. PDL::Graphics::Simple provides that capability.
33  
34 PDL::Graphics::Simple implements all the functionality used in the
35 PDL::Book examples, with identical syntax. It also generalizes that
36 syntax - you can use ::Simple graphics, with slight syntactical
37 differences, in the same manner that you would use any of the engine
38 modules. See the Examples below for details.
39  
40 The plot you get will always be what you asked for, regardless of
41 which plotting engine you have installed on your system.
42  
43 Only a small subset of PDL's complete graphics functionality is
44 supported -- each individual plotting module has unique advantages and
45 functionality that are beyond what PDL::Graphics::Simple can do. Only
46 2-D plotting is supported. For 3-D plotting, use
47 PDL::Graphics::Gnuplot or PDL::Graphics::Trid directly.
48  
49 When plotting to a file, the file output is not guaranteed to be
50 present until the plot object is destroyed (e.g. by being undefed or
51 going out of scope).
52  
53 =head1 STATE OF DEVELOPMENT
54  
55 PDL::Graphics::Simple currently supports most of the
56 planned functionality. It is being released as a beta
57 test to determine if it meets users' needs and gain feedback on
58 the API -- so please give feedback!
59  
60 =head1 SUPPORTED GRAPHICS ENGINES
61  
62 PDL::Graphics::Simple includes support for the following graphics
63 engines. Additional driver modules can be loaded dynamically; see
64 C<register>, below. Each of the engines has unique capabilities and
65 flavor that are not captured in PDL::Graphics::Simple - you are
66 encouraged to look at the individual modules for more capability!
67  
68 =over 3
69  
70 =item * Gnuplot (via PDL::Graphics::Gnuplot)
71  
72 Gnuplot is an extremely richly featured plotting package that offers
73 markup, rich text control, RGB color, and 2-D and 3-D plotting. Its
74 output is publication quality. It is supported on POSIX systems,
75 MacOS, and Microsoft Windows, and is available from most package
76 managers.
77  
78 =item * PGPLOT (via PDL::Graphics::PGPLOT::Window)
79  
80 PGPLOT is venerable and nearly as fully featured as Gnuplot for 2-D
81 plotting. It lacks RGB color output. It does have rich text control,
82 but uses simple plotter fonts that are generated internally. It
83 is supported on MacOS and POSIX, but is not as widely available as
84 Gnuplot.
85  
86 =item * PLplot (via PDL::Graphics::PLplot)
87  
88 PLplot is a moderately full featured plotting package that
89 generates publication quality output with a simple high-level interface.
90 It is supported on MacOS and POSIX.
91  
92 =item * Prima (via PDL::Graphics::Prima)
93  
94 Prima is based around a widget paradigm that enables complex
95 interaction with data in real-time, and it is highly optimized for
96 that application. It is not as mature as the other platforms,
97 particularly for static plot generation to files. This means that
98 PDL::Graphics::Simple does not play to its considerable strengths,
99 although Prima is serviceable and fast in this application. Please
100 run the Prima demo in the perldl shell for a better sample of Prima's
101 capabilities.
102  
103 =back
104  
105 =head1 EXAMPLES
106  
107 PDL::Graphics::Simple can be called using plot-atomic or curve-atomic
108 plotting styles, using a pidgin form of calls to any of the main
109 modules. The examples are divided into Book-like (very simple),
110 PGPLOT-like (curve-atomic), and Gnuplot-like (plot-atomic) cases.
111  
112 There are three main styles of interaction with plot objects that
113 PDL::Graphics::Simple supports, reflective of the pre-existing
114 modules' styles of interaction. You can mix-and-match them to match
115 your particular needs and coding style. Here are examples showing
116 convenient ways to call the code.
117  
118 =head2 First steps (non-object-oriented)
119  
120 For the very simplest actions there are non-object-oriented shortcuts.
121 Here are some examples of simple tasks, including axis labels and plot
122 titles. These non-object-oriented shortcuts are useful for display
123 with the default window size. They make use of a package-global plot
124 object.
125  
126 The non-object interface will keep using the last plot engine you used
127 successfully. On first start, you can specify an engine with the
128 environment variable PDL_SIMPLE_ENGINE. If that one isn't working, or
129 if you didn't specify one, all known engines are tried in alphabetical
130 order until one works.
131  
132 =over 3
133  
134 =item * Load module and create line plots
135  
136 use PDL::Graphics::Simple;
137 $x = xvals(51)/5;
138 $y = $x**3;
139  
140 $y->line;
141 line( $x, $y );
142 line( $x, $y, {title=>"My plot", ylabel=> "Ordinate", xlabel=>"Abscissa"} );
143  
144 =item * Bin plots
145  
146 $y->bins;
147 bins($y, {title=>"Bin plot", xl=>"Bin number", yl=>"Count"} );
148  
149 =item * Point plots
150  
151 $y->points;
152 points($y, {title=>"Points plot"});
153  
154 =item * Logarithmic scaling
155  
156 line( $y, { log=>'y' } ); # semilog
157 line( $y, { log=>'xy' } ); # log-log
158  
159 =item * Image display
160  
161 $im = 10 * sin(rvals(101,101)) / (10 + rvals(101,101));
162 imag $im; # Display image
163 imag $im, 0, 1; # Set lower/upper color range
164  
165 =item * Overlays
166  
167 points($x, $y, {logx=>1});
168 hold;
169 line($x, sqrt($y)*10);
170 release;
171  
172  
173 =item * Justify aspect ratio
174  
175 imag $im, {justify=>1}
176 points($x, $y, {justify=>1});
177  
178 =item * Erase/delete the plot window
179
180 erase();
181  
182 =back
183  
184 =head2 Simple object-oriented plotting
185
186 More functionality is accessible through direct use of the PDL::Graphics::Simple
187 object. You can set plot size, direct plots to files, and set up multi-panel plots.
188  
189 The constructor accepts window configuration options that set the plotting
190 environment, including size, driving plot engine, output, and multiple
191 panels in a single window.
192  
193 For interactive/display plots, the plot is rendered immediately, and lasts until
194 the object is destroyed. For file plots, the file is not guaranteed to exist
195 and be correct until the object is destroyed.
196  
197 The basic plotting method is C<plot>. C<plot> accepts a collection of
198 arguments that describe one or more "curves" (or datasets) to plot,
199 followed by an optional plot option hash that affects the entire plot.
200 Overplotting is implemented via plot option, via a held/released state
201 (as in PGPLOT), and via a convenience method C<oplot> that causes the
202 current plot to be overplotted on the previous one.
203  
204 Plot style (line/points/bins/etc.) is selected via the C<with> curve option.
205 Several convenience methods exist to create plots in the various styles.
206  
207 =over 3
208  
209 =item * Load module and create basic objects
210  
211 use PDL::Graphics::Simple;
212 $x = xvals(51)/5;
213 $y = $x**3;
214
215 $win = pgswin(); # plot to a default-shape window
216 $win = pgswin( size=>[4,3] ); # size is given in inches by default
217 $win = pgswin( size=>[10,5,'cm'] ); # You can feed in other units too
218 $win = pgswin( out=>'plot.ps' ); # Plot to a file (type is via suffix)
219 $win = pgswin( engine=>'gnuplot' ); # Pick a particular plotting engine
220 $win = pgswin( multi=>[2,2] ); # Set up for a 2x2 4-panel plot
221
222 =item * Simple plots with C<plot>
223  
224 $win->plot( with=>'line', $x, $y, {title=>"Simple line plot"} );
225 $win->plot( with=>'errorbars', $x, $y, sqrt($y), {title=>"Error bars"} );
226 $win->plot( with=>'circles', $x, $y, sin($x)**2 );
227  
228 =item * Plot overlays
229  
230 # All at once
231 $win->plot( with=>'line', $x, $y, with=>'circles', $x, $y/2, sqrt($y) );
232  
233 # Using oplot (IDL-style; PLplot-style)
234 $win->plot( with=>'line', $x, $y );
235 $win->oplot( with=>'circles', $x, $y/2, sqrt($y) );
236  
237 # Using object state (PGPLOT-style)
238 $win->line( $x, $y );
239 $win->hold;
240 $win->circles( $x, $y/2, sqrt($y) );
241 $win->release;
242  
243 =back
244  
245  
246 =head1 FUNCTIONS
247  
248 =cut
249  
250 package PDL::Graphics::Simple;
251  
252 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
253 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
254 1 ▶use PDL;
Subroutine BEGIN: called CC=1 SCAR=0.0
255 1 ▶use PDL::Options q/iparse/;
Subroutine BEGIN: called CC=1 SCAR=0.0
256 1 ▶use File::Temp qw/tempfile tempdir/;
Subroutine BEGIN: called CC=1 SCAR=0.0
257 1 ▶use Scalar::Util q/looks_like_number/;
Subroutine BEGIN: called CC=1 SCAR=0.0
258  
259 1 our $VERSION = '1.005';
260 1 $VERSION = eval $VERSION;
261  
262 ##############################
263 # Exporting
264 1 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
265 1 our @EXPORT_OK = qw(pgswin plot line points imag hold release erase image );
266 1 our @EXPORT = qw(pgswin line points imag hold release erase);
267  
268  
269 ##############################
270 # Configuration
271  
272 # Knowledge base containing found info about each possible backend
273 1 our $mods = {};
274 1 our $mod_abbrevs = undef;
275 1 our $last_successful_type = undef;
276 1 our $global_plot = undef;
277  
278 # Attempt to load some default modules
279  
280 1 for my $submod(qw/ PGPLOT Gnuplot PLplot Prima /) {
281 1 ▶eval "use PDL::Graphics::Simple::$submod;";
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
282 }
283  
284 =head2 show
285  
286 =for usage
287  
288 PDL::Graphics::Simple::show
289  
290 =for ref
291  
292 C<show> lists the supported engines and a one-line synopsis of each.
293  
294 =cut
295 sub show {
296 1 ▶ my $format = "%-10s %-30s %-s\n";
Subroutine show: called CC=2 SCAR=6.9
Pod documented
297 1 printf($format, "NAME","Module","(synopsis)");
298 1 printf($format, "----","------","----------");
299 1 for my $engine( sort keys %$mods ) {
300 4 printf($format, $engine, $mods->{$engine}->{engine}, $mods->{$engine}->{synopsis});
301 }
302 1 print "\n";
303 }
304  
305 ##############################
306 # Constructor - scan through registered subclasses and generate the correct one.
307  
308 =head2 pgswin - exported constructor
309  
310 =for usage
311  
312 $w = pgswin( %opts );
313  
314 =for ref
315  
316 C<pgswin> is a constructor that is exported by default into the using package. Calling
317 C<pgswin(%opts)> is exactly the same as calling C<< PDL::Graphics::Simple->new(%opts) >>.
318  
319  
320 =head2 new
321  
322 =for usage
323  
324 $w = new PDL::Graphics::Simple( %opts );
325  
326 =for ref
327  
328 C<new> is the main constructor for PDL::Graphics::Simple. It accepts a list of options
329 about the type of window you want:
330  
331 =over 3
332  
333 =item engine
334  
335 If specified, this must be one of the supported plotting engines. You
336 can use a module name or the shortened name. If you don't give one,
337 the constructor will try the last one you used, or else scan through
338 existing modules and pick one that seems to work. It will first check
339 the environment variable PDL_SIMPLE_ENGINE, then search through all
340 the known engines in alphabetical order until it finds one that seems
341 to work on your system.
342  
343 =item size
344  
345 This is a window size as an ARRAY ref containing [width, height,
346 units]. If no units are specified, the default is "inches". Accepted
347 units are "in","pt","px","mm", and "cm". The conversion used for pixels
348 is 100 px/inch.
349  
350 =item type
351  
352 This describes the kind of plot to create, and should be either "file"
353 or "interactive" - though only the leading character is checked. If
354 you don't specify either C<type> or C<output> (below), the default is
355 "interactive". If you specify only C<output>, the default is "file".
356  
357 =item output
358  
359 This should be a window number or name for interactive plots, or a
360 file name for file plots. The default file name is "plot.png" in the
361 current working directory. Individual plotting modules all support at
362 least '.png', '.pdf', and '.ps' -- via format conversion if necessary.
363 Most other standard file types are supported but are not guaranteed to
364 work.
365  
366 =item multi
367  
368 This enables plotting multiple plots on a single screen. You feed in
369 a single array ref containing (nx, ny). Subsequent calls to plot
370 send graphics to subsequent locations on the window. The ordering
371 is always horizontal first, and left-to-right, top-to-bottom.
372  
373 =back
374  
375 =cut
376  
377 1 our $new_defaults = {
378 engine => '',
379 size => [8,6,'in'],
380 type => '',
381 output => '',
382 multi => undef
383 };
384  
385 4 ▶sub pgswin { new('PDL::Graphics::Simple',@_); }
Subroutine pgswin: called CC=1 SCAR=0.0
Pod documented
386  
387 sub new {
388 4 ▶ my $pkg = shift;
Subroutine new: called CC=26 SCAR=56.5
Pod documented
389 4 my $opt_in = shift;
390 4 ▶ $opt_in = {} unless(defined($opt_in));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opt_in 4 0 4
391 4 ▶ if(!(ref($opt_in))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $opt_in) 0 4 4
392 0 my %opt = ($opt_in, @_);
393 0 $opt_in = \%opt;
394 }
395  
396 4 my $opt = { iparse( $new_defaults, $opt_in ) };
397  
398 ##############################
399 # Pick out a working plot engine...
400  
401 4 ▶ unless($opt->{engine}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt->{"engine"}) 4 0 4
402 # find the first working subclass...
403 4 ▶ unless($last_successful_type) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($PDL::Graphics::Simple::last_successful_type) 4 0 4
404  
405 4 my @try = ();
406  
407 4 ▶ if($ENV{'PDL_SIMPLE_ENGINE'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"PDL_SIMPLE_ENGINE"}) 0 4 4
408 0 push(@try, $ENV{'PDL_SIMPLE_ENGINE'});
409 }
410
411 4 push(@try, sort keys %$mods);
412  
413 4 attempt: for my $engine( @try ) {
414 16 print "Trying $engine ($mods->{$engine}->{engine})...";
415 16 my $a;
416 my $s;
417 16 eval "\$a = $mods->{$engine}->{module}::check()";
418 16 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 16 16
419 0 chomp $@;
420 0 $s = "$@";
421 } else {
422 16 ▶ $s = ($a ? "ok" : "nope");
Branch: 1 branch
BranchTrueFalseTotal
$a ? : 0 16 16
423 }
424 16 print $s."\n";
425 16 ▶ if($a) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a) 0 16 16
426 0 $last_successful_type = $engine;
427 0 last attempt;
428 }
429 }
430 4 ▶ unless( $last_successful_type ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($PDL::Graphics::Simple::last_successful_type) 4 0 4
431 4 die "Sorry, all known plotting engines failed. Install one and try again.\n";
432 }
433 }
434 0 $opt->{engine} = $last_successful_type;
435 }
436
437 ##############################
438 # Deal with abbreviations.
439 # This can't be done at load time since the modules have to self-register then -- so
440 # we do it at run time instead.
441 0 ▶ $mod_abbrevs = _make_abbrevs($mods) unless($mod_abbrevs);
Branch: 1 branch
BranchTrueFalseTotal
unless $PDL::Graphics::Simple::mod_abbrevs 0 0 0
442
443 0 my $engine = $mod_abbrevs->{lc($opt->{engine})};
444 0 ▶ unless(defined($engine) and defined($mods->{$engine})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $engine and defined $PDL::Graphics::Simple::mods->{$engine}) 0 0 0
Condition0%0/3
defined $engine and defined $PDL::Graphics::Simple::mods->{$engine}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $engine and defined $PDL::Graphics::Simple::mods->{$engine}
defined $engine defined $PDL::Graphics::Simple::mods->{$engine}
Truth table0%0/3
A and B
A= defined $engineB= defined $PDL::Graphics::Simple::mods->{$engine}
ABresult
0X0
100
111
445 0 die "$opt->{engine} is not a known plotting engine. Use PDL::Graphics::Simple::show() for a list. ";
446 }
447 0 $last_successful_type = $opt->{engine};
448
449 0 my $size = _regularize_size($opt->{size},'in');
450  
451 0 my $type = $opt->{type};
452 0 my $output = $opt->{output};
453
454 0 ▶ unless($type) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type) 0 0 0
455 # Default to file if output looks like a filename; to interactive otherwise.
456 0 ▶ $type = ( ($output =~ m/\.(\w{2,4})$/) ? 'f' : 'i' );
Branch: 1 branch
BranchTrueFalseTotal
$output =~ /\.(\w{2,4})$/ ? : 0 0 0
457 }
458 0 ▶ unless($type =~ m/^[fi]/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type =~ /^[fi]/i) 0 0 0
459 0 die "$type is not a known output type (must be 'file' or 'interactive')\n";
460 }
461  
462 # Default to 'plot.png' if no output is specified.
463 0 ▶ unless($output) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($output) 0 0 0
464 0 ▶ $output = ($type eq 'f') ? "plot.png" : "";
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'f' ? : 0 0 0
465 }
466
467 # Hammer it into a '.png' if no suffix is specified
468 0 ▶ if( $opt->{type} =~ m/^f/i and $output !~ m/\.(\w{2,4})$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"type"} =~ /^f/i and not $output =~ /\.(\w{2,4})$/) 0 0 0
Condition0%0/3
$opt->{"type"} =~ /^f/i and not $output =~ /\.(\w{2,4})$/
!ll&&!rl&&r
000
MC/DC0%0/2
$opt->{"type"} =~ /^f/i and not $output =~ /\.(\w{2,4})$/
$opt->{"type"} =~ /^f/i not $output =~ /\.(\w{2,4})$/
Truth table0%0/3
A and not(B)
A= $opt->{"type"} =~ /^f/iB= not $output =~ /\.(\w{2,4})$/
ABresult
0X0
100
111
469 0 $output .= ".png";
470 }
471  
472 # Error-check multi
473 0 ▶ if( defined($opt->{multi}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt->{"multi"}) 0 0 0
474 0 ▶ if( ref($opt->{multi}) ne 'ARRAY' or @{$opt->{multi}} != 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opt->{"multi"} ne "ARRAY" or @{$opt->{"multi"};} != 2) 0 0 0
Condition0%0/3
ref $opt->{"multi"} ne "ARRAY" or @{$opt->{"multi"};} != 2
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $opt->{"multi"} ne "ARRAY" or @{$opt->{"multi"};} != 2
ref $opt->{"multi"} ne "ARRAY" @{$opt->{"multi"};} != 2
Truth table0%0/3
A or B
A= ref $opt->{"multi"} ne "ARRAY"B= @{$opt->{"multi"};} != 2
ABresult
1X1
011
000
475 0 die "PDL::Graphics::Simple::new: 'multi' option requires a 2-element ARRAY ref\n";
476 }
477 0 ▶ $opt->{multi}->[0] = 1 unless( $opt->{multi}->[0] );
Branch: 1 branch
BranchTrueFalseTotal
unless $opt->{"multi"}[0] 0 0 0
478 0 ▶ $opt->{multi}->[1] = 1 unless( $opt->{multi}->[1] );
Branch: 1 branch
BranchTrueFalseTotal
unless $opt->{"multi"}[1] 0 0 0
479 }
480  
481 0 my $submod= $mods->{$engine}->{module};
482 0 my $params = { size=>$size, type=>$type, output=>$output, multi=>$opt->{multi} };
483 0 my $obj = eval "new $mods->{$engine}->{module}(\$params)";
484 0 my $me = { engine=>$engine, params=>$params, obj=>$obj };
485 0 return bless($me,$pkg);
486  
487 }
488  
489 =head2 plot
490  
491 =for usage
492  
493 $w = new PDL::Graphics::Simple ( %opts );
494 $w->plot($data);
495  
496 =for ref
497  
498 C<plot> plots zero or more traces of data on a graph. It accepts two kinds of
499 options: plot options that affect the whole plot, and curve options
500 that affect each curve. The arguments are divided into "curve blocks", each
501 of which contains a curve options hash followed by data.
502  
503 If the last argument is a hash ref, it is always treated as plot options.
504 If the first and second arguments are both hash refs, then the first argument
505 is treated as plot options and the second as curve options for the first curve
506 block.
507  
508 =head3 Plot options:
509  
510 =over 3
511  
512 =item oplot
513  
514 If this is set, then the plot overplots a previous plot.
515  
516 =item title
517  
518 If this is set, it is a title for the plot as a whole.
519  
520 =item xlabel
521  
522 If this is set, it is a title for the X axis.
523  
524 =item ylabel
525  
526 If this is set, it is a title for the Y axis.
527  
528 =item xrange
529  
530 If this is set, it is a two-element ARRAY ref containing a range for
531 the X axis. If it is clear, the axis is autoscaled.
532  
533 =item yrange
534  
535 If this is set, it is a two-element ARRAY ref containing a range for
536 the Y axis. If it is clear, the axis is autoscaled.
537  
538 =item logaxis
539  
540 This should be empty, "x", "y", or "xy" (case and order insensitive).
541 Named axes are scaled logarithmically.
542  
543 =item crange
544  
545 If this is set, it is a two-element ARRAY ref containing a range for
546 color values, full black to full white. If it is clear, the engine or
547 plot module is responsible for setting the range.
548  
549 =item wedge
550  
551 If this is set, then image plots get a scientific colorbar on the
552 right side of the plot. (You can also say "colorbar", "colorbox", or "cb" if
553 you're more familiar with Gnuplot).
554  
555 =item justify
556  
557 If this is set to a true value, then the screen aspect ratio is adjusted
558 to keep the Y axis and X axis scales equal -- so circles appear circular, and
559 squares appear square.
560  
561 =item legend (EXPERIMENTAL)
562  
563 The "legend" plot option is intended for full support but it is currently
564 experimental: it is not fully implemented in all the engines, and
565 implementation is more variable than one would like in the engines that
566 do support it.
567  
568 This controls whether and where a plot legend should be placed. If
569 you set it, you supply a combination of 't','b','c','l', and 'r':
570 indicating top, bottom, center, left, right position for the plot
571 legend. For example, 'tl' for top left, 'tc' for center top, 'c' or
572 'cc' for dead center. If left unset, no legend will be plotted. If
573 you set it but don't specify a position (or part of one), it defaults
574 to top and left.
575  
576 If you supply even one 'key' curve option in the curves, legend defaults
577 to the value 'tl' if it isn't specified.
578  
579 =back
580  
581 =head3 Curve options:
582  
583 =over 3
584  
585 =item with
586  
587 This names the type of curve to be plotted. See below for supported curve types.
588  
589 =item key
590  
591 This gives a name for the following curve, to be placed in a master plot legend.
592 If you don't specify a name but do call for a legend, the curve will be named
593 with the plot type and number (e.g. "line 3" or "points 4").
594  
595 =item width
596  
597 This lets you specify the width of the line, as a multiplier on the standard
598 width the engine uses. That lets you pick normal-width or extra-bold lines
599 for any given curve. The option takes a single positive natural number.
600  
601 =item style
602  
603 You can specify the line style in a very limited way -- as a style
604 number supported by the backend. The styles are generally defined by
605 a mix of color and dash pattern, but the particular color and dash
606 pattern depend on the engine in use. The first 30 styles are
607 guaranteed to be distinguishable. This is useful to produce, e.g.,
608 multiple traces with the same style.
609  
610 =back
611  
612 =head3 Curve types supported
613  
614 =over 3
615  
616 =item points
617  
618 This is a simple point plot. It takes 1 or 2 columns of data.
619  
620 =item lines
621  
622 This is a simple line plot. It takes 1 or 2 columns of data.
623  
624 =item bins
625  
626 Stepwise line plot, with the steps centered on each X value. 1 or 2 columns.
627  
628 =item errorbars
629  
630 Simple points-with-errorbar plot, with centered errorbars. It takes 2
631 or 3 columns, and the last column is the absolute size of the errorbar (which
632 is centered on the data point).
633  
634 =item limitbars
635  
636 Simple points-with-errorbar plot, with asymmetric errorbars. It takes
637 3 or 4 columns, and the last two columns are the absolute low and high
638 values of the errorbar around each point (specified relative to the
639 origin, not relative to the data point value).
640  
641 =item circles
642  
643 Plot unfilled circles. Requires 2 or 3 columns of data; the last
644 column is the radius of each circle. The circles are circular in
645 scientific coordinates, not necessarily in screen coordinates (unless
646 you specify the "justify" plot option).
647  
648 =item image
649  
650 This is a monochrome or RGB image. It takes a 2-D or 3-D array of
651 values, as (width x height x color-index). Images are displayed in
652 a sepiatone color scale that enhances contrast and preserves intensity
653 when converted to grayscale. If you use the convenience routines
654 (C<image> or C<imag>), the "justify" plot option defaults to 1 -- so
655 the image will be displayed with square pixel aspect. If you use
656 C<< plot(with=>'image' ...) >>, "justify" defaults to 0 and you will have
657 to set it if you want square pixels.
658  
659 =item labels
660  
661 This places text annotations on the plot. It requires three input
662 arguments: the X and Y location(s) as PDLs, and the label(s) as a list
663 ref. The labels are normally left-justified, but you can explicitly
664 set the alignment for each one by beginning the label with "<" for
665 left "|" for center, and ">" for right justification, or a single " "
666 to denote default justification (left).
667  
668 =back
669  
670 =cut
671  
672 # Plot options have a bunch of names for familiarity to different package users.
673 # They're hammered into a single simplified set for transfer to the engines.
674  
675 1 our $plot_options = new PDL::Options( {
676 oplot=> 0,
677 title => undef,
678 xlabel=> undef,
679 ylabel=> undef,
680 legend => undef,
681 xrange=> undef,
682 yrange=> undef,
683 logaxis=> "",
684 crange=> undef,
685 bounds=> undef,
686 wedge => 0,
687 justify=>undef,
688 });
689  
690 1 $plot_options->synonyms( {
691 cbrange=>'crange',
692 replot=>'oplot',
693 xtitle=>'xlabel',
694 ytitle=>'ylabel',
695 'key'=>'legend',
696 colorbar=>'wedge',
697 colorbox=>'wedge',
698 cb=>'wedge',
699 logscale => 'logaxis'
700 });
701  
702 1 our $plot_types = {
703 points => { args=>[1,2], ndims=>[1] },
704 lines => { args=>[1,2], ndims=>[1] },
705 bins => { args=>[1,2], ndims=>[1] },
706 circles => { args=>[2,3], ndims=>[1] },
707 errorbars => { args=>[2,3], ndims=>[1] },
708 limitbars => { args=>[3,4], ndims=>[1] },
709 image => { args=>[1,3], ndims=>[2,3] },
710 labels => { args=>[3], ndims=>[1] }
711 };
712  
713 1 our $plot_type_abbrevs = _make_abbrevs($plot_types);
714  
715 sub plot {
716 0 ▶ my $obj;
Subroutine plot: not called CC=96 SCAR=91.4
Pod documented
717 0 ▶ if(UNIVERSAL::isa($_[0],"PDL::Graphics::Simple")) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($_[0], 'PDL::Graphics::Simple')) { } 0 0 0
718 0 $obj = shift;
719 } else {
720 0 $obj = $global_plot = new('PDL::Graphics::Simple');
721 }
722  
723 0 my $curve_options = new PDL::Options( {
724 with => 'lines',
725 key => undef,
726 style => undef,
727 width => undef
728 });
729 0 $curve_options->synonyms( {
730 legend =>'key',
731 name=>'key'
732 });
733 0 $curve_options->incremental(0);
734  
735  
736 ##############################
737 # Trap some simple errors
738 0 ▶ if($#_ == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ == 0) 0 0 0
739 0 die "plot: requires at least one argument to plot!\n";
740 }
741 0 ▶ if($#_ == 1 and ref($_[0]) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ == 1 and ref $_[0] eq "HASH") 0 0 0
Condition0%0/3
$#_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
$#_ == 1 and ref $_[0] eq "HASH"
$#_ == 1 ref $_[0] eq "HASH"
Truth table0%0/3
A and B
A= $#_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
742 0 die "plot: requires at least one argument to plot, in addition to plot options\n";
743 }
744
745 ##############################
746 # Collect plot options. These can be in a leading or trailing
747 # hash ref, with the leading overriding the trailing one. If the first
748 # two elements are hash refs, then the first is plot options and
749 # the second is curve options, otherwise we treat the first as curve options.
750 # A curve option hash is required for every curve.
751 0 my $po = {};
752  
753 0 ▶ while(ref($_[$#_]) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[$#_] eq "HASH") 0 0 0
754 0 for my $k(keys %{$_[$#_]}) {
755 0 $po->{$k} = $_[$#_]->{$k};
756 };
757 0 pop;
758 }
759  
760 0 ▶ if(ref($_[0]) eq 'HASH' and ref($_[1]) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq "HASH" and ref $_[1] eq "HASH") 0 0 0
Condition0%0/3
ref $_[0] eq "HASH" and ref $_[1] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] eq "HASH" and ref $_[1] eq "HASH"
ref $_[0] eq "HASH" ref $_[1] eq "HASH"
Truth table0%0/3
A and B
A= ref $_[0] eq "HASH"B= ref $_[1] eq "HASH"
ABresult
0X0
100
111
761 0 for my $k(keys %{$_[0]}) {
762 0 $po->{$k} = $_[0]->{$k};
763 }
764 0 shift;
765 }
766  
767 0 my $called_from_imag = $po->{called_from_imag};
768 0 delete $po->{called_from_imag};
769  
770 0 $po = $plot_options->options($po);
771 0 ▶ $po->{oplot} = 1 if(defined($obj->{held}) and $obj->{held});
Branch: 1 branch
BranchTrueFalseTotal
if defined $obj->{"held"} and $obj->{"held"} 0 0 0
Condition0%0/3
defined $obj->{"held"} and $obj->{"held"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $obj->{"held"} and $obj->{"held"}
defined $obj->{"held"} $obj->{"held"}
Truth table0%0/3
A and B
A= defined $obj->{"held"}B= $obj->{"held"}
ABresult
0X0
100
111
772  
773  
774 ##############################
775 # Check the plot options for correctness.
776  
777 ### bounds is a synonym for xrange/yrange together.
778 ### (dcm likes it)
779 0 ▶ if(defined($po->{bounds})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $po->{"bounds"}) 0 0 0
780 0 ▶ if( !ref($po->{bounds}) or
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $po->{"bounds"} or ref $po->{"bounds"} ne "ARRAY" or @{$po->{"bounds"};} != 2) 0 0 0
Condition0%0/6
not ref $po->{"bounds"} or ref $po->{"bounds"} ne "ARRAY" or @{$po->{"bounds"};} != 2
l!l&&r!l&&!r
000
not ref $po->{"bounds"} or ref $po->{"bounds"} ne "ARRAY"
l!l&&r!l&&!r
000
MC/DC0%0/3
not ref $po->{"bounds"} or ref $po->{"bounds"} ne "ARRAY" or @{$po->{"bounds"};} != 2
not ref $po->{"bounds"} ref $po->{"bounds"} ne "ARRAY" @{$po->{"bounds"};} != 2
Truth table0%0/4
not(A) or B or C
A= not ref $po->{"bounds"}B= ref $po->{"bounds"} ne "ARRAY"C= @{$po->{"bounds"};} != 2
ABCresult
1XX1
01X1
0011
0000
781 ref($po->{bounds}) ne 'ARRAY' or
782 0 @{$po->{bounds}} != 2
783 ) {
784 0 die "Bounds option must be a 2-element ARRAY ref containing (xrange, yrange)\n";
785 }
786  
787 0 ▶ if( defined($po->{bounds}->[0]) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $po->{"bounds"}[0]) 0 0 0
788 0 ▶ print STDERR "WARNING: bounds overriding xrange since both were specified\n" if(defined($po->{xrange}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $po->{"xrange"} 0 0 0
789 0 $po->{xrange} = $po->{bounds}->[0];
790 }
791  
792 0 ▶ if( defined($po->{bounds}->[1]) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $po->{"bounds"}[1]) 0 0 0
793 0 ▶ print STDERR "WARNING: bounds overriding yrange since both were specified\n" if(defined($po->{yrange}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $po->{"yrange"} 0 0 0
794 0 $po->{yrange} = $po->{bounds}->[1];
795 }
796 }
797  
798 0 ▶ if( defined($po->{xrange}) and (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $po->{"xrange"} and !ref($po->{"xrange"}) || ref $po->{"xrange"} ne "ARRAY" || @{$po->{"xrange"};} != 2 || $po->{"xrange"}[0] == $po->{"xrange"}[1]) 0 0 0
Condition0%0/12
defined $po->{"xrange"} and !ref($po->{"xrange"}) || ref $po->{"xrange"} ne "ARRAY" || @{$po->{"xrange"};} != 2 || $po->{"xrange"}[0] == $po->{"xrange"}[1]
!ll&&!rl&&r
000
not ref $po->{"xrange"} or ref $po->{"xrange"} ne "ARRAY" or @{$po->{"xrange"};} != 2 or $po->{"xrange"}[0] == $po->{"xrange"}[1]
l!l&&r!l&&!r
000
not ref $po->{"xrange"} or ref $po->{"xrange"} ne "ARRAY" or @{$po->{"xrange"};} != 2
l!l&&r!l&&!r
000
not ref $po->{"xrange"} or ref $po->{"xrange"} ne "ARRAY"
l!l&&r!l&&!r
000
MC/DC0%0/5
defined $po->{"xrange"} and !ref($po->{"xrange"}) || ref $po->{"xrange"} ne "ARRAY" || @{$po->{"xrange"};} != 2 || $po->{"xrange"}[0] == $po->{"xrange"}[1]
defined $po->{"xrange"} not ref $po->{"xrange"} ref $po->{"xrange"} ne "ARRAY" @{$po->{"xrange"};} != 2 $po->{"xrange"}[0] == $po->{"xrange"}[1]
Truth table0%0/6
A and not(B) or C or D or E
A= defined $po->{"xrange"}B= not ref $po->{"xrange"}C= ref $po->{"xrange"} ne "ARRAY"D= @{$po->{"xrange"};} != 2E= $po->{"xrange"}[0] == $po->{"xrange"}[1]
ABCDEresult
0XXXX0
100000
11XXX1
101XX1
1001X1
100011
799 !ref($po->{xrange}) or
800 ref($po->{xrange}) ne 'ARRAY' or
801 0 @{$po->{xrange}} != 2 or
802 $po->{xrange}->[0] == $po->{xrange}->[1])
803 ) {
804 0 die "Invalid X range (must be a 2-element ARRAY ref with differing values)\n";
805 }
806  
807 0 ▶ if( defined($po->{yrange}) and (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $po->{"yrange"} and !ref($po->{"yrange"}) || ref $po->{"yrange"} ne "ARRAY" || @{$po->{"yrange"};} != 2 || $po->{"yrange"}[0] == $po->{"yrange"}[1]) 0 0 0
Condition0%0/12
defined $po->{"yrange"} and !ref($po->{"yrange"}) || ref $po->{"yrange"} ne "ARRAY" || @{$po->{"yrange"};} != 2 || $po->{"yrange"}[0] == $po->{"yrange"}[1]
!ll&&!rl&&r
000
not ref $po->{"yrange"} or ref $po->{"yrange"} ne "ARRAY" or @{$po->{"yrange"};} != 2 or $po->{"yrange"}[0] == $po->{"yrange"}[1]
l!l&&r!l&&!r
000
not ref $po->{"yrange"} or ref $po->{"yrange"} ne "ARRAY" or @{$po->{"yrange"};} != 2
l!l&&r!l&&!r
000
not ref $po->{"yrange"} or ref $po->{"yrange"} ne "ARRAY"
l!l&&r!l&&!r
000
MC/DC0%0/5
defined $po->{"yrange"} and !ref($po->{"yrange"}) || ref $po->{"yrange"} ne "ARRAY" || @{$po->{"yrange"};} != 2 || $po->{"yrange"}[0] == $po->{"yrange"}[1]
defined $po->{"yrange"} not ref $po->{"yrange"} ref $po->{"yrange"} ne "ARRAY" @{$po->{"yrange"};} != 2 $po->{"yrange"}[0] == $po->{"yrange"}[1]
Truth table0%0/6
A and not(B) or C or D or E
A= defined $po->{"yrange"}B= not ref $po->{"yrange"}C= ref $po->{"yrange"} ne "ARRAY"D= @{$po->{"yrange"};} != 2E= $po->{"yrange"}[0] == $po->{"yrange"}[1]
ABCDEresult
0XXXX0
100000
11XXX1
101XX1
1001X1
100011
808 !ref($po->{yrange}) or
809 ref($po->{yrange}) ne 'ARRAY' or
810 0 @{$po->{yrange}} != 2 or
811 $po->{yrange}->[0] == $po->{yrange}->[1])
812 ) {
813 0 die "Invalid Y range (must be a 2-element ARRAY ref with differing values)\n";
814 }
815  
816 0 ▶ if( defined($po->{wedge}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $po->{"wedge"}) 0 0 0
817 0 $po->{wedge} = !!$po->{wedge};
818 }
819  
820 0 ▶ if( length($po->{logaxis}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $po->{"logaxis"}) 0 0 0
821 0 ▶ if($po->{logaxis} =~ m/[^xyXY]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($po->{"logaxis"} =~ /[^xyXY]/) 0 0 0
822 0 die "logaxis must be X, Y, or XY (case insensitive)\n";
823 }
824 0 $po->{logaxis} =~ tr/XY/xy/;
825 0 $po->{logaxis} =~ s/yx/xy/;
826 }
827  
828 0 ▶ unless($po->{oplot}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($po->{"oplot"}) 0 0 0
829 0 $obj->{keys} = [];
830 }
831  
832 0 ▶ if(!defined($po->{justify})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $po->{"justify"}) 0 0 0
833 0 ▶ $po->{justify} = ($called_from_imag ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$called_from_imag ? : 0 0 0
834 }
835  
836 ##############################
837 # Parse out curve blocks and check each one for existence.
838 0 my @blocks = ();
839 0 my $xminmax = [undef,undef];
840 0 my $yminmax = [undef,undef];
841  
842 0 ▶ while( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
843 0 my $co = {};
844 0 my @args = ();
845  
846 0 ▶ if (ref $_[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 0 0 0
847 0 $co = shift;
848 } else {
849 # Attempt to parse out curve option hash entries from an inline hash.
850 # Keys must exists and not be refs and contain at least one letter.
851 0 ▶ while( @_ and !ref($_[0]) and $_[0] =~ m/[a-zA-Z]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and not ref $_[0] and $_[0] =~ /[a-zA-Z]/) 0 0 0
Condition0%0/6
@_ and not ref $_[0] and $_[0] =~ /[a-zA-Z]/
!ll&&!rl&&r
000
@_ and not ref $_[0]
!ll&&!rl&&r
000
MC/DC0%0/3
@_ and not ref $_[0] and $_[0] =~ /[a-zA-Z]/
@_ not ref $_[0] $_[0] =~ /[a-zA-Z]/
Truth table0%0/4
A and not(B) and C
A= @_B= not ref $_[0]C= $_[0] =~ /[a-zA-Z]/
ABCresult
0XX0
10X0
1100
1111
852 0 my $a = shift;
853 0 my $b = shift;
854 0 $co->{$a} = $b;
855 }
856 }
857
858 ##############################
859 # Parse curve options and expand into standard form so we can find "with".
860 0 $curve_options->options({key=>undef});
861 0 my %co2 = %{$curve_options->options( $co )};
862 0 my $co2 = \%co2;
863  
864 0 my $ptn = $plot_type_abbrevs->{ $co2->{with} };
865 0 ▶ unless( defined($ptn) and defined($plot_types->{$ptn}) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ptn and defined $PDL::Graphics::Simple::plot_types->{$ptn}) 0 0 0
Condition0%0/3
defined $ptn and defined $PDL::Graphics::Simple::plot_types->{$ptn}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ptn and defined $PDL::Graphics::Simple::plot_types->{$ptn}
defined $ptn defined $PDL::Graphics::Simple::plot_types->{$ptn}
Truth table0%0/3
A and B
A= defined $ptnB= defined $PDL::Graphics::Simple::plot_types->{$ptn}
ABresult
0X0
100
111
866 0 die "Unknown plot type $ptn\n";
867 }
868  
869 0 ▶ if($co2->{key} and !defined($po->{legend})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($co2->{"key"} and not defined $po->{"legend"}) 0 0 0
Condition0%0/3
$co2->{"key"} and not defined $po->{"legend"}
!ll&&!rl&&r
000
MC/DC0%0/2
$co2->{"key"} and not defined $po->{"legend"}
$co2->{"key"} not defined $po->{"legend"}
Truth table0%0/3
A and not(B)
A= $co2->{"key"}B= not defined $po->{"legend"}
ABresult
0X0
100
111
870 0 $po->{legend} = 'tl';
871 }
872  
873 0 ▶ unless( $ptn eq 'labels' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ptn eq "labels") 0 0 0
874 0 my $ptns = $ptn;
875 0 $ptns=~s/s$//;
876 0 ▶ push( @{$obj->{keys}}, ( defined($co2->{key}) ? $co2->{key} : sprintf("%s %d",$ptns,1+@{$obj->{keys}})));
Branch: 1 branch
BranchTrueFalseTotal
defined $co2->{'key'} ? : 0 0 0
877 }
878  
879 0 my $pt = $plot_types->{$ptn};
880 0 $co2->{with} = $ptn;
881  
882 ##############################
883 # Snarf up the other arguments.
884  
885 0 ▶ while( @_ and ( UNIVERSAL::isa($_[0], 'PDL') or
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and UNIVERSAL::isa($_[0], "PDL") || &looks_like_number($_[0]) || ref $_[0] eq "ARRAY") 0 0 0
Condition0%0/9
@_ and UNIVERSAL::isa($_[0], "PDL") || &looks_like_number($_[0]) || ref $_[0] eq "ARRAY"
!ll&&!rl&&r
000
UNIVERSAL::isa($_[0], "PDL") or &looks_like_number($_[0]) or ref $_[0] eq "ARRAY"
l!l&&r!l&&!r
000
UNIVERSAL::isa($_[0], "PDL") or &looks_like_number($_[0])
l!l&&r!l&&!r
000
MC/DC0%0/4
@_ and UNIVERSAL::isa($_[0], "PDL") || &looks_like_number($_[0]) || ref $_[0] eq "ARRAY"
@_ UNIVERSAL::isa($_[0], "PDL") &looks_like_number($_[0]) ref $_[0] eq "ARRAY"
Truth table0%0/5
A and B or C or D
A= @_B= UNIVERSAL::isa($_[0], "PDL")C= &looks_like_number($_[0])D= ref $_[0] eq "ARRAY"
ABCDresult
0XXX0
10000
11XX1
101X1
10011
886 looks_like_number($_[0]) or
887 ref $_[0] eq 'ARRAY'
888 )
889 ) {
890 0 push(@args, shift );
891 }
892  
893  
894 ##############################
895 # Most array refs get immediately converted to
896 # PDLs. But the last argument to a "with=labels" curve
897 # needs to be left as an array ref. If it's a PDL we throw
898 # an error, since that's a common mistake case.
899 0 ▶ if( $ptn eq 'labels' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ptn eq 'labels') { } 0 0 0
900 0 for my $i(0..$#args-1) {
901 0 ▶ $args[$i] = pdl($args[$i]) unless(UNIVERSAL::isa($args[$i],'PDL'));
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($args[$i], "PDL") 0 0 0
902 }
903 0 ▶ if( ref($args[$#args]) ne 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args[$#args] ne "ARRAY") 0 0 0
904 0 die "Last argument to 'labels' plot type must be an array ref!";
905 }
906 } else {
907 0 for my $i(0..$#args) {
908 0 ▶ $args[$i] = pdl($args[$i]) unless(UNIVERSAL::isa($args[$i],'PDL'));
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($args[$i], "PDL") 0 0 0
909 }
910 }
911
912 ##############################
913 # Now check options
914 0 ▶ unless(@args == $pt->{args}->[0] or @args == $pt->{args}->[1]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@args == $pt->{"args"}[0] or @args == $pt->{"args"}[1]) 0 0 0
Condition0%0/3
@args == $pt->{"args"}[0] or @args == $pt->{"args"}[1]
l!l&&r!l&&!r
000
MC/DC0%0/2
@args == $pt->{"args"}[0] or @args == $pt->{"args"}[1]
@args == $pt->{"args"}[0] @args == $pt->{"args"}[1]
Truth table0%0/3
A or B
A= @args == $pt->{"args"}[0]B= @args == $pt->{"args"}[1]
ABresult
1X1
011
000
915 0 die sprintf("plot style %s requires %d or %d columns; you gave %d\n",$ptn,$pt->{args}->[0],$pt->{args}->[1],0+@args);
916 }
917
918 # Add an index variable if needed
919 0 ▶ if(defined($pt->{args}->[1])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pt->{"args"}[1]) 0 0 0
920 0 ▶ if( $pt->{args}->[1] - @args == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pt->{"args"}[1] - @args == 2) 0 0 0
921 0 my @dims = ($args[0]->slice(":,:")->dims)[0,1];
922 0 unshift(@args, xvals(@dims), yvals(@dims));
923 }
924 0 ▶ if( $pt->{args}->[1] - @args == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pt->{"args"}[1] - @args == 1) 0 0 0
925 0 unshift(@args, xvals($args[0]) );
926 }
927 }
928  
929 # Check that the PDL arguments all agree in a threading sense.
930 # Since at least one type of args has an array ref in there, we have to
931 # consider that case as a pseudo-PDL.
932 0 ▶ my @dims = map { ref($_) eq 'ARRAY' ? [ 0+@{$_} ] : [$_->dims] } @args;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'ARRAY' ? : 0 0 0
933 0 my $dims;
934 {
935 0 local $PDL::undefval = 1;
936 0 $dims = pdl(@dims);
937 }
938 0 my $dmax = $dims->mv(1,0)->maximum;
939 0 ▶ unless( ( ($dims==1) | ($dims==$dmax) )->all ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (($dims == 1 | $dims == $dmax)->all) 0 0 0
940 0 die "Data dimensions do not agree in plot.\n";
941 }
942  
943 # Check that the number of dimensions is correct...
944 0 ▶ if($dims->dim(0) != $pt->{ndims}->[0] and
Branch: 1 branch
BranchTrueFalseTotal
if ($dims->dim(0) != $pt->{"ndims"}[0] and !defined($pt->{"ndims"}[1]) || $dims->dim(0) != $pt->{"ndims"}[1]) 0 0 0
Condition0%0/6
$dims->dim(0) != $pt->{"ndims"}[0] and !defined($pt->{"ndims"}[1]) || $dims->dim(0) != $pt->{"ndims"}[1]
!ll&&!rl&&r
000
not defined $pt->{"ndims"}[1] or $dims->dim(0) != $pt->{"ndims"}[1]
l!l&&r!l&&!r
000
MC/DC0%0/3
$dims->dim(0) != $pt->{"ndims"}[0] and !defined($pt->{"ndims"}[1]) || $dims->dim(0) != $pt->{"ndims"}[1]
$dims->dim(0) != $pt->{"ndims"}[0] not defined $pt->{"ndims"}[1] $dims->dim(0) != $pt->{"ndims"}[1]
Truth table0%0/4
A and not(B) or C
A= $dims->dim(0) != $pt->{"ndims"}[0]B= not defined $pt->{"ndims"}[1]C= $dims->dim(0) != $pt->{"ndims"}[1]
ABCresult
0XX0
1000
11X1
1011
945 ((!defined($pt->{ndims}->[1])) or ($dims->dim(0) != $pt->{ndims}->[1]))) {
946 0 die "Data dimension (".$dims->dim(0)."-D PDLs) is not correct for plot type $ptn";
947 }
948  
949 # Accumulate x and y ranges...
950 0 my @minmax;
951 0 my $dcorner = pdl(0,0);
952  
953 # Deal with half-pixel offset at edges of images
954 0 ▶ if($args[0]->dims > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args[0]->dims > 1) 0 0 0
955 0 my $xymat = pdl( [ ($args[0]->slice("(1),(0)")-$args[0]->slice("(0),(0)")),
956 ($args[0]->slice("(0),(1)")-$args[0]->slice("(0),(0)")) ],
957 [ ($args[1]->slice("(1),(0)")-$args[1]->slice("(0),(0)")),
958 ($args[1]->slice("(0),(1)")-$args[1]->slice("(0),(0)")) ]
959 );
960 0 $dcorner = ($xymat x pdl(0.5,0.5)->slice("*1"))->slice("(0)")->abs;
961 }
962  
963 0 @minmax = $args[0]->minmax;
964 0 $minmax[0] -= $dcorner->at(0);
965 0 $minmax[1] += $dcorner->at(0);
966  
967 0 ▶ if($po->{logaxis} =~ m/x/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($po->{"logaxis"} =~ /x/) 0 0 0
968 0 ▶ if($minmax[1] > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($minmax[1] > 0) { } 0 0 0
969 0 ▶ if($minmax[0] <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($minmax[0] <= 0) 0 0 0
970 0 $minmax[0] = $args[0]->where( ($args[0]>0) )->min;
971 }
972 } else {
973 0 $minmax[0] = $minmax[1] = undef;
974 }
975 }
976  
977 0 ▶ $xminmax->[0] = $minmax[0] if( defined($minmax[0]) and ( !defined($xminmax->[0]) or $minmax[0] < $xminmax->[0] ));
Branch: 1 branch
BranchTrueFalseTotal
if defined $minmax[0] and !defined($xminmax->[0]) || $minmax[0] < $xminmax->[0] 0 0 0
Condition0%0/6
defined $minmax[0] and !defined($xminmax->[0]) || $minmax[0] < $xminmax->[0]
!ll&&!rl&&r
000
not defined $xminmax->[0] or $minmax[0] < $xminmax->[0]
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $minmax[0] and !defined($xminmax->[0]) || $minmax[0] < $xminmax->[0]
defined $minmax[0] not defined $xminmax->[0] $minmax[0] < $xminmax->[0]
Truth table0%0/4
A and not(B) or C
A= defined $minmax[0]B= not defined $xminmax->[0]C= $minmax[0] < $xminmax->[0]
ABCresult
0XX0
1000
11X1
1011
978 0 ▶ $xminmax->[1] = $minmax[1] if( defined($minmax[1]) and ( !defined($xminmax->[1]) or $minmax[1] > $xminmax->[1] ));
Branch: 1 branch
BranchTrueFalseTotal
if defined $minmax[1] and !defined($xminmax->[1]) || $minmax[1] > $xminmax->[1] 0 0 0
Condition0%0/6
defined $minmax[1] and !defined($xminmax->[1]) || $minmax[1] > $xminmax->[1]
!ll&&!rl&&r
000
not defined $xminmax->[1] or $minmax[1] > $xminmax->[1]
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $minmax[1] and !defined($xminmax->[1]) || $minmax[1] > $xminmax->[1]
defined $minmax[1] not defined $xminmax->[1] $minmax[1] > $xminmax->[1]
Truth table0%0/4
A and not(B) or C
A= defined $minmax[1]B= not defined $xminmax->[1]C= $minmax[1] > $xminmax->[1]
ABCresult
0XX0
1000
11X1
1011
979  
980  
981
982 0 @minmax = $args[1]->minmax;
983 0 $minmax[0] -= $dcorner->at(1);
984 0 $minmax[1] += $dcorner->at(1);
985  
986 0 ▶ if($po->{logaxis} =~ m/y/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($po->{"logaxis"} =~ /y/) 0 0 0
987 0 ▶ if($minmax[1] > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($minmax[1] > 0) { } 0 0 0
988 0 ▶ if($minmax[0] <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($minmax[0] <= 0) 0 0 0
989 0 $minmax[0] = $args[0]->where( ($args[0]>0) )->min;
990 }
991 } else {
992 0 $minmax[0] = $minmax[1] = undef;
993 }
994 }
995  
996 0 ▶ $yminmax->[0] = $minmax[0] if( !defined($yminmax->[0]) or $minmax[0] < $yminmax->[0] );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $yminmax->[0] or $minmax[0] < $yminmax->[0] 0 0 0
Condition0%0/3
not defined $yminmax->[0] or $minmax[0] < $yminmax->[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $yminmax->[0] or $minmax[0] < $yminmax->[0]
not defined $yminmax->[0] $minmax[0] < $yminmax->[0]
Truth table0%0/3
not(A) or B
A= not defined $yminmax->[0]B= $minmax[0] < $yminmax->[0]
ABresult
1X1
011
000
997 0 ▶ $yminmax->[1] = $minmax[1] if( !defined($yminmax->[1]) or $minmax[1] > $yminmax->[1] );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $yminmax->[1] or $minmax[1] > $yminmax->[1] 0 0 0
Condition0%0/3
not defined $yminmax->[1] or $minmax[1] > $yminmax->[1]
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $yminmax->[1] or $minmax[1] > $yminmax->[1]
not defined $yminmax->[1] $minmax[1] > $yminmax->[1]
Truth table0%0/3
not(A) or B
A= not defined $yminmax->[1]B= $minmax[1] > $yminmax->[1]
ABresult
1X1
011
000
998  
999 # Push the curve block to the list.
1000 0 push(@blocks, [$co2, @args] );
1001 }
1002  
1003 ##############################
1004 # Deal with context-dependent defaults.
1005  
1006  
1007 0 ▶ $po->{xrange}->[0] = $xminmax->[0] unless(defined($po->{xrange}->[0]));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $po->{"xrange"}[0] 0 0 0
1008 0 ▶ $po->{xrange}->[1] = $xminmax->[1] unless(defined($po->{xrange}->[1]));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $po->{"xrange"}[1] 0 0 0
1009 0 ▶ $po->{yrange}->[0] = $yminmax->[0] unless(defined($po->{yrange}->[0]));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $po->{"yrange"}[0] 0 0 0
1010 0 ▶ $po->{yrange}->[1] = $yminmax->[1] unless(defined($po->{yrange}->[1]));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $po->{"yrange"}[1] 0 0 0
1011  
1012 0 ▶ if($po->{xrange}->[0] == $po->{xrange}->[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($po->{"xrange"}[0] == $po->{"xrange"}[1]) 0 0 0
1013 0 $po->{xrange}->[0] -= 0.5;
1014 0 $po->{xrange}->[1] += 0.5;
1015 }
1016  
1017 0 ▶ if($po->{yrange}->[0] == $po->{yrange}->[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($po->{"yrange"}[0] == $po->{"yrange"}[1]) 0 0 0
1018 0 $po->{yrange}->[0] -= 0.5;
1019 0 $po->{yrange}->[1] += 0.5;
1020 }
1021  
1022 0 ▶ if($po->{logaxis} =~ m/x/ and ($po->{xrange}->[0] <= 0 or $po->{xrange}->[1] <= 0) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($po->{"logaxis"} =~ /x/ and $po->{"xrange"}[0] <= 0 || $po->{"xrange"}[1] <= 0) 0 0 0
Condition0%0/6
$po->{"logaxis"} =~ /x/ and $po->{"xrange"}[0] <= 0 || $po->{"xrange"}[1] <= 0
!ll&&!rl&&r
000
$po->{"xrange"}[0] <= 0 or $po->{"xrange"}[1] <= 0
l!l&&r!l&&!r
000
MC/DC0%0/3
$po->{"logaxis"} =~ /x/ and $po->{"xrange"}[0] <= 0 || $po->{"xrange"}[1] <= 0
$po->{"logaxis"} =~ /x/ $po->{"xrange"}[0] <= 0 $po->{"xrange"}[1] <= 0
Truth table0%0/4
A and B or C
A= $po->{"logaxis"} =~ /x/B= $po->{"xrange"}[0] <= 0C= $po->{"xrange"}[1] <= 0
ABCresult
0XX0
1000
11X1
1011
1023 0 die "logarithmic X axis requires positive limits (xrange is [$po->{xrange}->[0],$po->{xrange}->[1]])";
1024 }
1025 0 ▶ if($po->{logaxis} =~ m/y/ and ($po->{yrange}->[0] <= 0 or $po->{yrange}->[1] <= 0) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($po->{"logaxis"} =~ /y/ and $po->{"yrange"}[0] <= 0 || $po->{"yrange"}[1] <= 0) 0 0 0
Condition0%0/6
$po->{"logaxis"} =~ /y/ and $po->{"yrange"}[0] <= 0 || $po->{"yrange"}[1] <= 0
!ll&&!rl&&r
000
$po->{"yrange"}[0] <= 0 or $po->{"yrange"}[1] <= 0
l!l&&r!l&&!r
000
MC/DC0%0/3
$po->{"logaxis"} =~ /y/ and $po->{"yrange"}[0] <= 0 || $po->{"yrange"}[1] <= 0
$po->{"logaxis"} =~ /y/ $po->{"yrange"}[0] <= 0 $po->{"yrange"}[1] <= 0
Truth table0%0/4
A and B or C
A= $po->{"logaxis"} =~ /y/B= $po->{"yrange"}[0] <= 0C= $po->{"yrange"}[1] <= 0
ABCresult
0XX0
1000
11X1
1011
1026 0 die "logarithmic Y axis requires positive limits";
1027 }
1028  
1029 ##############################
1030 # At long last, the parsing is over. Dispatch the call.
1031 0 $obj->{obj}->{keys} = $obj->{keys};
1032 0 $obj->{obj}->plot( $po, @blocks );
1033 }
1034  
1035 =head2 oplot
1036  
1037 =for usage
1038  
1039 $w = new PDL::Graphics::Simple ( %opts );
1040 $w->plot($data);
1041 $w->oplot($more_data);
1042  
1043 =for ref
1044  
1045 C<oplot> is a convenience interface. It is exactly
1046 equivalent to C<plot> except it sets the plot option C<oplot>,
1047 so that the plot will be overlain on the previous one.
1048  
1049 =cut
1050  
1051 sub oplot {
1052 0 ▶ my $h;
Subroutine oplot: not called CC=2 SCAR=17.9
Pod documented
1053  
1054 0 ▶ if(ref($_[$#_]) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[$#_] eq 'HASH') { } 0 0 0
1055 0 $h = $_[$#_];
1056 } else {
1057 0 $h = {};
1058 0 push(@_, $h);
1059 }
1060 0 $h->{replot} = 1;
1061
1062 0 plot(@_);
1063 }
1064  
1065 =head2 line, points, image, imag
1066  
1067 =for usage
1068  
1069 # Object-oriented convenience
1070 $w = new PDL::Graphics::Simple ( % opts );
1071 $w->line($data);
1072  
1073 # Very Lazy Convenience
1074 $a = xvals(50);
1075 lines $a;
1076 $im = sin(rvals(100,100)/3);
1077 imag $im;
1078 imag $im, 0, 1, {title=>"Bullseye?", j=>1};
1079  
1080 =for ref
1081  
1082 C<line>, C<points>, and C<image> are convenience
1083 interfaces. They are exactly equivalent to C<plot> except that
1084 they set the default "with" curve option to the appropriate
1085 plot type.
1086  
1087 C<imag> is even more DWIMMy for PGPLOT users or PDL Book readers:
1088 it accepts up to three non-hash arguments at the start of the
1089 argument list. The second and third are taken to be values for
1090 the C<crange> plot option.
1091  
1092 =cut
1093  
1094 sub _convenience_plot{
1095 1 ▶ my $type = shift;
Subroutine _convenience_plot: called CC=5 SCAR=27.8
1096 1 my $me;
1097 1 ▶ if( UNIVERSAL::isa($_[0], 'PDL::Graphics::Simple') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($_[0], 'PDL::Graphics::Simple')) { } 0 1 1
1098 0 $me = shift;
1099 } else {
1100 1 $me = _global_or_new();
1101 }
1102  
1103 0 my @args = @_;
1104  
1105 0 ▶ die "Not enough args to PDL::Graphics::Simple::$type()\n" if( @args < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if @args < 1 0 0 0
1106 0 ▶ if( ref($args[0]) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args[0] eq 'HASH') { } 0 0 0
1107 0 ▶ if( ref($args[1]) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args[1] eq 'HASH') { } 0 0 0
1108 0 $args[1]->{with} = $type;
1109 } else {
1110 0 $args[0]->{with} = $type;
1111 }
1112 } else {
1113 0 unshift(@args, 'with', $type);
1114 }
1115 0 plot( $me, @args );
1116 }
1117  
1118 0 ▶sub line { _convenience_plot( 'line', @_ ); }
Subroutine line: not called CC=1 SCAR=6.9
Pod documented
1119 1 *PDL::line = \&line;
1120  
1121 1 ▶sub lines { _convenience_plot( 'line', @_ ); }
Subroutine lines: called CC=1 SCAR=0.0
Pod documented
1122 1 *PDL::lines = \&lines;
1123  
1124 0 ▶sub bins { _convenience_plot( 'bins', @_ ); }
Subroutine bins: not called CC=1 SCAR=6.9
Pod documented
1125 1 *PDL::bins = \&bins;
1126  
1127 0 ▶sub points { _convenience_plot( 'points', @_ ); }
Subroutine points: not called CC=1 SCAR=6.9
Pod documented
1128 1 *PDL::points = \&points;
1129  
1130 sub image {
1131 0 ▶ _convenience_plot( 'image', @_ ,{called_from_imag=>1});
Subroutine image: not called CC=1 SCAR=6.9
Pod documented
1132 } # Don't PDL-class image since it's so different from imag.
1133  
1134 sub imag {
1135 3 ▶ my $me;
Subroutine imag: called CC=5 SCAR=29.3
Pod documented
1136 3 ▶ if( UNIVERSAL::isa($_[0], 'PDL::Graphics::Simple') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($_[0], 'PDL::Graphics::Simple')) { } 0 3 3
1137 0 $me = shift;
1138 } else {
1139 3 $me = _global_or_new();
1140 }
1141 0 my $data = shift;
1142 0 my $crange = [];
1143 0 ▶ unless(ref($_[0]) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $_[0] eq "HASH") 0 0 0
1144 0 $crange->[0] = shift;
1145
1146 0 ▶ unless(ref($_[0]) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $_[0] eq "HASH") 0 0 0
1147 0 $crange->[1] = shift;
1148 }
1149 }
1150
1151 # Try to put the crange into the plot options, if they are present
1152 0 ▶ unless( ref($_[$#_]) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $_[$#_] eq "HASH") 0 0 0
1153 0 push(@_, {} );
1154 }
1155 0 $_[$#_]->{crange} = $crange;
1156  
1157 0 _convenience_plot( 'image', $data, @_, {called_from_imag=>1} );
1158 }
1159 1 *PDL::imag = \&imag;
1160  
1161 =head2 erase
1162  
1163 =for usage
1164  
1165 use PDL::Graphics::Simple qw/erase hold release/;
1166 line xvals(10), xvals(10)**2 ;
1167 sleep 5;
1168 erase;
1169  
1170 =for ref
1171  
1172 C<erase> removes a global plot window. It should not be called as a method.
1173 To remove a plot window contained in a variable, undefine it.
1174  
1175 =cut
1176  
1177 1 our $global_object;
1178  
1179 sub erase {
1180 2 ▶ my $me = shift;
Subroutine erase: called CC=3 SCAR=13.3
Pod documented
1181 2 ▶ if(defined($me)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $me) 0 2 2
1182 0 die "PDL::Graphics::Simple::erase: no arguments, please.";
1183 }
1184 2 ▶ if(defined($global_object)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $PDL::Graphics::Simple::global_object) 0 2 2
1185 0 undef $global_object;
1186 }
1187 }
1188  
1189 =head2 hold
1190  
1191 =for usage
1192  
1193 use PDL::Graphics::Simple;
1194 line xvals(10);
1195 hold;
1196 line xvals(10)**0.5;
1197  
1198 =for ref
1199  
1200 Causes subsequent plots to be overplotted on any existing one. Called
1201 as a function with no arguments, C<hold> applies to the global object.
1202 Called as an object method, it applies to the object.
1203  
1204 =cut
1205  
1206 sub hold {
1207 0 ▶ my $me = shift;
Subroutine hold: not called CC=4 SCAR=30.0
Pod documented
1208 0 ▶ if(defined($me) and UNIVERSAL::isa($me,"PDL::Graphics::Simple")) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $me and UNIVERSAL::isa($me, 'PDL::Graphics::Simple')) { } 0 0 0
elsif (defined $PDL::Graphics::Simple::global_object) { } 0 0 0
Condition0%0/3
defined $me and UNIVERSAL::isa($me, "PDL::Graphics::Simple")
!ll&&!rl&&r
000
MC/DC0%0/2
defined $me and UNIVERSAL::isa($me, "PDL::Graphics::Simple")
defined $me UNIVERSAL::isa($me, "PDL::Graphics::Simple")
Truth table0%0/3
A and B
A= defined $meB= UNIVERSAL::isa($me, "PDL::Graphics::Simple")
ABresult
0X0
100
111
1209 0 $me->{held} =1;
1210 } elsif(defined($global_object)) {
1211 0 $global_object->{held}=1;
1212 } else {
1213 0 die "Can't hold a nonexistent window!\n";
1214 }
1215 }
1216  
1217 =head2 release
1218  
1219 =for usage
1220  
1221 use PDL::Graphics::Simple;
1222 line xvals(10);
1223 hold;
1224 line xvals(10)**0.5;
1225 release;
1226 line xvals(10)**0.5;
1227  
1228 =for ref
1229  
1230 Releases a hold placed by C<hold>.
1231  
1232 =cut
1233  
1234 sub release {
1235 0 ▶ my $me = shift;
Subroutine release: not called CC=4 SCAR=30.0
Pod documented
1236 0 ▶ if(defined($me) and UNIVERSAL::isa($me,"PDL::Graphics::Simple")) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $me and UNIVERSAL::isa($me, 'PDL::Graphics::Simple')) { } 0 0 0
elsif (defined $PDL::Graphics::Simple::global_object) { } 0 0 0
Condition0%0/3
defined $me and UNIVERSAL::isa($me, "PDL::Graphics::Simple")
!ll&&!rl&&r
000
MC/DC0%0/2
defined $me and UNIVERSAL::isa($me, "PDL::Graphics::Simple")
defined $me UNIVERSAL::isa($me, "PDL::Graphics::Simple")
Truth table0%0/3
A and B
A= defined $meB= UNIVERSAL::isa($me, "PDL::Graphics::Simple")
ABresult
0X0
100
111
1237 0 $me->{held} = 0;
1238 } elsif(defined($global_object)) {
1239 0 $global_object->{held} = 0;
1240 } else {
1241 0 die "Can't release a nonexistent window!\n";
1242 }
1243 }
1244  
1245 ##############################
1246 # Utilities.
1247  
1248  
1249 sub _global_or_new {
1250 4 ▶ unless(defined($global_object)) {
Subroutine _global_or_new: called CC=2 SCAR=8.1
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $PDL::Graphics::Simple::global_object) 4 0 4
1251 4 $global_object = pgswin();
1252 }
1253 0 return $global_object;
1254 }
1255  
1256  
1257 ### Units table - cheesy but also horrible.
1258 1 our $units = {
1259 'inch'=>1,
1260 'inc'=>1,
1261 'in' =>1,
1262 'i' => 1,
1263 'char'=>16,
1264 'cha'=>16,
1265 'ch'=>16,
1266 'c'=>16,
1267 'points'=>72,
1268 'point'=>72,
1269 'poin'=>72,
1270 'poi'=>72,
1271 'po'=>72,
1272 'pt'=>72,
1273 'px'=>100,
1274 'pixels'=>100,
1275 'pixel'=>100,
1276 'pixe'=>100,
1277 'pix'=>100,
1278 'pi'=>100,
1279 'p'=>100,
1280 'mm' => 25.4,
1281 'cm' => 2.54
1282 };
1283  
1284 ### regularize_size -- handle the various cases for the size option to new.
1285 sub _regularize_size {
1286 0 ▶ my $size = shift;
Subroutine _regularize_size: not called CC=11 SCAR=48.8
1287 0 my $unit = shift;
1288
1289 0 $unit =~ tr/A-Z/a-z/;
1290 0 ▶ die "size specifier unit '$unit' is unrecognized\n" unless($units->{$unit});
Branch: 1 branch
BranchTrueFalseTotal
unless $PDL::Graphics::Simple::units->{$unit} 0 0 0
1291  
1292 0 ▶ unless(ref($size)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $size) { } 0 0 0
(do { ref $size ne "ARRAY" }) 0 0 0
1293 0 $size = [ $size, $size, 'in' ];
1294 0 } elsif(ref($size) ne 'ARRAY') {
1295 0 die "size option requires an ARRAY ref or scalar\n";
1296 }
1297 0 ▶ die "size array must have at least one element\n" unless(@{$size});
Branch: 1 branch
BranchTrueFalseTotal
unless @{$size;} 0 0 0
1298 0 ▶ $size->[1] = $size->[0] if(@{$size}==1);
Branch: 1 branch
BranchTrueFalseTotal
if @{$size;} == 1 0 0 0
1299 0 ▶ $size->[2] = 'in' if(@{$size}==2);
Branch: 1 branch
BranchTrueFalseTotal
if @{$size;} == 2 0 0 0
1300 0 ▶ die "size array can have at most three elements\n" if(@{$size}>3);
Branch: 1 branch
BranchTrueFalseTotal
if @{$size;} > 3 0 0 0
1301 0 ▶ die "size array unit '$unit' is unrecognized\n" unless($units->{$unit});
Branch: 1 branch
BranchTrueFalseTotal
unless $PDL::Graphics::Simple::units->{$unit} 0 0 0
1302 0 ▶ die "new: size must be nonnegative\n" unless( $size->[0] > 0 and $size->[1] > 0 );
Branch: 1 branch
BranchTrueFalseTotal
unless $size->[0] > 0 and $size->[1] > 0 0 0 0
Condition0%0/3
$size->[0] > 0 and $size->[1] > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$size->[0] > 0 and $size->[1] > 0
$size->[0] > 0 $size->[1] > 0
Truth table0%0/3
A and B
A= $size->[0] > 0B= $size->[1] > 0
ABresult
0X0
100
111
1303  
1304 0 my $ret = [];
1305 0 $ret->[0] = $size->[0] / $units->{$size->[2]} * $units->{$unit};
1306 0 $ret->[1] = $size->[1] / $units->{$size->[2]} * $units->{$unit};
1307 0 $ret->[2] = $unit;
1308 0 return $ret;
1309 }
1310  
1311 ##########
1312 # make_abbrevs - generate abbrev hash for module list. Cheesy but fast to code.
1313 sub _make_abbrevs {
1314 1 ▶ my $hash = shift;
Subroutine _make_abbrevs: called CC=5 SCAR=16.1
1315 1 my $abbrevs = {};
1316 1 my %ab = ();
1317 1 for my $k(keys %$hash) {
1318 8 my $s = $k;
1319 8 ▶ while(length($s)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $s) 51 8 59
1320 51 push(@{$ab{$s}},$k);
1321 51 chop $s;
1322 }
1323 }
1324 1 for my $k(keys %ab) {
1325 48 ▶ $abbrevs->{$k} = $ab{$k}->[0] if( @{$ab{$k}} == 1);
Branch: 1 branch
BranchTrueFalseTotal
if @{$ab{$k};} == 1 46 2 48
1326 }
1327 1 return $abbrevs;
1328 }
1329  
1330 =head2 register
1331  
1332 =for usage
1333  
1334 PDL::Graphics::Simple::register( $module_name );
1335  
1336 =for ref
1337  
1338 This is the registration mechanism for new driver methods for
1339 C<PDL::Graphics::Simple>. Compliant drivers should announce
1340 themselves at compile time by calling C<register>. When they do that,
1341 they should have already defined a package global hash ref, C<$mod>,
1342 containing the following keys:
1343  
1344 =over
1345  
1346 =item shortname
1347  
1348 This is the short name of the engine, by which users refer to it colloquially.
1349  
1350 =item module
1351  
1352 This is the fully qualified package name of the module itself.
1353  
1354 =item engine
1355  
1356 This is the fully qualified package name of the Perl API for the graphics engine.
1357  
1358 =item synopsis
1359  
1360 This is a brief string describing the backend
1361  
1362 =item pgs_version
1363  
1364 This is a one-period version number of PDL::Graphics::Simple against which
1365 the module has been tested. A warning will be thrown if the version isn't the
1366 same as C<$PDL::Graphics::Simple::VERSION>.
1367  
1368 =back
1369  
1370 =cut
1371 sub register {
1372 4 ▶ my $module = shift;
Subroutine register: called CC=5 SCAR=16.6
Pod documented
1373
1374 4 my $modname = "\$${module}::mod";
1375 4 ▶ die "PDL::Graphics::Simple::register: tried to register $module \n\t...but $modname wasn't defined.\n"
Branch: 1 branch
BranchTrueFalseTotal
unless eval "defined($modname) and ref($modname) eq 'HASH';" 0 4 4
1376 unless (eval qq{defined($modname) and ref($modname) eq 'HASH';});
1377  
1378 4 my $mod = eval $modname;
1379  
1380 4 for(qw/shortname module engine synopsis pgs_version/) {
1381 die "PDL::Graphics::Simple::register: $modname looks fishy; I give up\n"
1382 20 ▶ unless( defined($mod->{$_}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mod->{$_} 0 20 20
1383 }
1384  
1385 warn "PDL::Graphics::Simple::register: $module is out of date - winging it"
1386 4 ▶ unless($mod->{pgs_version} eq $VERSION);
Branch: 1 branch
BranchTrueFalseTotal
unless $mod->{"pgs_version"} eq $PDL::Graphics::Simple::VERSION 0 4 4
1387  
1388 4 $mods->{$mod->{shortname}} = $mod;
1389 }
1390  
1391  
1392 =head1 IMPLEMENTATION
1393  
1394 PDL::Graphics::Simple defines an object that represents a plotting
1395 window/interface. When you construct the object, you can either
1396 specify a backend or allow PDL::Graphics::Simple to find a backend
1397 that seems to work on your system. Subsequent plotting commands are
1398 translated and passed through to that working plotting module.
1399  
1400 PDL::Graphics::Simple calls are dispatched in a two-step process. The
1401 main module curries the arguments, parsing them into a regularized
1402 form and carrying out DWIM optimizations. The regularized arguments
1403 are passed to subclasses that translate them into the APIs of their
1404 respective plot engines. The subclasses are very simple and implement
1405 only a few methods, outlined below. They are intended only to be
1406 called by the PDL::Graphics::Simple driver, which limits the need for
1407 argument processing, currying, and parsing. The subclasses are thus
1408 responsible only for converting the regularized parameters to plot
1409 calls in the form expected by their corresponding plot modules.
1410  
1411 PDL::Graphics::Simple works through a call-and-dispatch system rather
1412 than taking full advantage of inheritance. That is for two reasons:
1413 (1) it makes central control mildly easier going forward, since calls
1414 are dispatched through the main module; and (2) it makes the
1415 non-object-oriented interface easier to implement since the main
1416 interface modules are in one place and can access the global object
1417 easily.
1418  
1419 =head2 Interface subclass methods
1420  
1421 Each interface module supports the following methods:
1422  
1423 =cut
1424  
1425 # Note that these are =head3; that means they won't be indexed by PDL::Doc,
1426 # which is a Good Thing as they are internal routines.
1427  
1428 =head3 check
1429  
1430 C<check> attempts to load the relevant engine module and test that it
1431 is working. In addition to returning a boolean value indicating
1432 success if true, it registers its success or failure in
1433 the main $mods hash, under the "ok" flag. If there is a failure that
1434 generates an error message, the error is logged under the "msg" flag.
1435  
1436 C<check> accepts one parameter, "force". If it is missing or false,
1437 and "ok" is defined, check just echoes the prior result. If it is
1438 true, then check actually checks the status regardless of the "ok"
1439 flag.
1440  
1441 =head3 new
1442  
1443 C<new> creates and returns an appropriate plot object, or dies on
1444 failure.
1445  
1446 Each C<new> method should accept the following options, defined as in
1447 the description for PDL::Graphics::Simple::new (above). There is
1448 no need to set default values as all arguments should be set to
1449 reasonable values by the superclass.
1450  
1451 For file output, the method should autodetect file type by dot-suffix.
1452 At least ".png" and ".ps" should be supported.
1453  
1454 Required options: C<size>, C<type>, C<output>, C<multi>.
1455  
1456 =head3 plot
1457  
1458 C<plot> generates a plot. It should accept a standardized collection
1459 of options as generated by the PDL::Graphics::Simple plot method:
1460 standard plot options as a hash ref, followed by a list of curve
1461 blocks. It should render either a full-sized plot that fills the plot
1462 window or, if the object C<multi> option was set on construction, the
1463 current subwindow. For interactive plot types it should act as an
1464 atomic plot operation, displaying the complete plot. For file plot
1465 types the atomicity is not well defined, since multiplot grids may
1466 be problematic, but the plot should be closed as soon as practical.
1467  
1468 The plot options hash contains the plot options listed under C<plot>,
1469 above, plus one additional flag - C<oplot> - that indicates the new
1470 data is to be overplotted on top of whatever is already present in the
1471 plotting window. All options are present in the hash. The C<title>,
1472 C<xlabel>, C<ylabel>, and C<legend> options default to undef, which
1473 indicates the corresponding plot feature should not be rendered. The
1474 C<oplot>, C<xrange>, C<yrange>, C<crange>, C<wedge>, and C<justify>
1475 parameters are always both present and defined.
1476  
1477 If the C<oplot> plot option is set, then the plot should be overlain on
1478 a previous plot - otherwise the module should display a fresh plot.
1479  
1480 Each curve block consists of an ARRAY ref with a hash in the 0 element
1481 and all required data in the following elements, one PDL per
1482 (ordinate/abscissa). For 1-D plot types (like points and lines) the
1483 PDLs must be 1D. For image plot types the lone PDL must be 2D
1484 (monochrome) or 3D(RGB).
1485  
1486 The hash in the curve block contains the curve options for that
1487 particular curve. They are all set to have reasonable default values.
1488 The values passed in are C<with> and C<key>. If the C<legend>
1489 option is undefined, then the curve should not be placed into a plot
1490 legend (if present). The C<with> option will be one of C<points>,
1491 C<lines>, C<bins>, C<errorbars>, C<limitbars>, C<circles>
1492 C<image>, or C<labels>.
1493  
1494 =cut
1495
1496  
1497 1 1;
1498  
1499  
1500 =head1 TO-DO
1501  
1502 Deal with legend generation. In particular: adding legends with multi-call
1503 protocols is awkward and leads to many edge cases in the internal protocol.
1504 This needs more thought.
1505  
1506 =head1 RELEASE NOTES
1507  
1508 =head3 v1.003
1509  
1510 Fix tests for smoker compatibility
1511  
1512 =head3 v1.002
1513  
1514 Include Prima support
1515  
1516  
1517 =head1 REPOSITORY
1518  
1519 L<https:/github.com/drzowie/PDL-Graphics-Simple>
1520  
1521 =head1 AUTHOR
1522  
1523 Craig DeForest, C<< <craig@deforest.org> >>
1524  
1525  
1526 =head1 LICENSE AND COPYRIGHT
1527  
1528 Copyright 2013 Craig DeForest
1529  
1530 This program is free software; you can redistribute it and/or modify
1531 it under the terms of either: the Gnu General Public License v2 as
1532 published by the Free Software Foundation; or the Perl Artistic
1533 License included with the Perl language.
1534  
1535 see http://dev.perl.org/licenses/ for more information.
1536  
1537 =cut
↑ Summary Simple/PLplot.pm »