# 2D Plotting with PGPLOT

Welcome to this tour of the PDL's PGPLOT interface.

This tour will introduce the PDL's PGPLOT plotting
module and show what this powerful package can provide in terms of
plotting. It is not designed to give a full tour of PGPLOT, you are
advised to see the routines provided with `pgperl` for
that.

The PDL::Graphics::PGPLOT module provides a high-level interface to PGPLOT. However if you want even better control of your plots you might want to include the PGPLOT module specifically:

```
use PGPLOT;
```

One aspect of PGPLOT that requires mention is the use of devices: Normally PGPLOT will inquire you about what device you want to use, with the prompt:

```
Graphics device/type (? to see list, default /NULL):
```

```
```**pdl>** # ensure the module is loaded (required for PDL versions>= 2.004)
**pdl>** use PDL::Graphics::PGPLOT;
**pdl>** # The size of the window can be specified
**pdl>** $ENV{PGPLOT_XW_WIDTH}=0.3;
**pdl>** # You can set your device explicitly
**pdl>** dev('/XSERVE');
**pdl>** # First we define some variables to use for the rest of the demo.
**pdl>** $x=sequence(10);
**pdl>** $y=2*$x**2;
**pdl>** # Now a simple plot with points
**pdl>** points $x, $y;

Output
```
```**pdl>** # Here is the same with lines
**pdl>** line $x, $y;

Output
```
```**pdl>** # If you want to overlay one plot you can use the command
**pdl>** # 'hold' to put the graphics on hold and 'release' to
**pdl>** # revert the effect
**pdl>** points $x, $y, {SYMBOL=>4}; # The last argument sets symboltype
**pdl>** hold;
**pdl>** # Now draw lines between the points
**pdl>** line $x, $y;
**pdl>** # Plot errorbars over the points
**pdl>** $yerr=sqrt($y);
**pdl>** errb $x, $y, $yerr;
**pdl>** # To revert to old behaviour, use release
**pdl>** release;

Output
```
```**pdl>** bin $x, $y;
**pdl>** # This plots a binned histogram of the data and as you can
**pdl>** # see it made a new plot.

Output
```
```**pdl>** # 2D data can also easily be accomodated:
**pdl>** # First make a simple image
**pdl>** $gradient=sequence(40,40);
**pdl>** # Then display it.
**pdl>** imag $gradient;
**pdl>** # And overlay a contour plot over it:
**pdl>** hold;
**pdl>** cont $gradient;
**pdl>** release;
**pdl>** # PDL::Graphics::PGPLOT contains several colour tables,
**pdl>** # a more extensive collection can be found in
**pdl>** # PDL::Graphics::LUT
**pdl>** #
**pdl>** # (note: the call to lut_names() can take a few seconds to execute)
**pdl>** #
**pdl>** use PDL::Graphics::LUT;
**pdl>** @names = lut_names();
**pdl>** print "Available tables: [ ", @names, " ]\n";
**pdl>** # use the first table
**pdl>** ctab( lut_data($names[0]) );
**pdl>** use PGPLOT;
**pdl>** pglabel "", "", "Colour table: $names[0]";
Available tables: [ aips0 backgr bgyrw blue blulut color green heat
idl11 idl12 idl14 idl15 idl2 idl4 idl5 idl6 isophot light manycol pastel
rainbow rainbow1 rainbow2 rainbow3 rainbow4 ramp random random1 random2
random3 random4 random5 random6 real red smooth smooth1 smooth2 smooth3
staircase stairs8 stairs9 standard ]

Output
```
```**pdl>** # To change plot specifics you can either use the specific PGPLOT
**pdl>** # commands - recommended if you need lots of control over your
**pdl>** # plot.
**pdl>** #
**pdl>** # Or you can use the new option specifications:
**pdl>** # To plot our first graph again with blue color, dashed line
**pdl>** # and a thickness of 10 we can do:
**pdl>** line $x, $y, {COLOR=>5, LINESTYLE=>'dashed', LINEWIDTH=>10};

Output
```
```**pdl>** # Now for a more complicated example.
**pdl>** # First create some data
**pdl>** $a=sequence(360)*3.1415/180.;
**pdl>** $b=sin($a)*transpose(cos($a));
**pdl>** # Make a piddle with the wanted contours
**pdl>** $contours=pdl [0.1,0.5,1.0];
**pdl>** # And an array (reference to an array) with labels
**pdl>** $labels=['A', 'B', 'C'];
**pdl>** # Create a contour map of the data - note that we can set the colour of
**pdl>** # the labels.
**pdl>** cont($b, {CONTOURS=>$contours, linest=>'DASHED',
**pdl>** LINEWIDTH=>3, COLOR=>2, LABELCOL=>4});
**pdl>** hold;
**pdl>** pgqlw($linewidth);
**pdl>** points $a->slice('0:-1:4')*180./3.1415;
**pdl>** release;

Output
```
```**pdl>** #
**pdl>** # More examples of changing the plot defaults
**pdl>** #
**pdl>** $a = 1+sequence(10);
**pdl>** $b = $a*2;
**pdl>** $bord_opt = { TYPE => 'RELATIVE', VALUE => 0.1 };
**pdl>** line log10($a), $b, { AXIS => 'LOGX', BORDER => $bord_opt };

Output
```
```**pdl>** #
**pdl>** # We can also create vector maps of data
**pdl>** # This requires one array for the horizontal component and
**pdl>** # one for the vertical component
**pdl>** #
**pdl>** $horizontal=sequence(10,10);
**pdl>** $vertical=transpose($horizontal)+random(10,10)*$horizontal/10.;
**pdl>** $arrow={ARROW=> {FS=>1, ANGLE=>25, VENT=>0.7, SIZE=>3}};
**pdl>** vect $horizontal, $vertical, {ARROW=>$arrow, COLOR=>RED};
**pdl>** hold;
**pdl>** cont $vertical-$horizontal, {COLOR=>YELLOW};
**pdl>** release;

Output
```
```**pdl>** #
**pdl>** # To draw [filled] polygons, the command poly is handy:
**pdl>** #
**pdl>** $x=sequence(10)/5;
**pdl>** poly $x, $x**2, {FILL=>HATCHED, COLOR=>BLUE};

Output
```
```**pdl>** #
**pdl>** # the latest feature of PDL are complex numbers
**pdl>** # so let's play with a simple example
**pdl>** #
**pdl>** use PDL::Complex;
**pdl>** $z50 = zeroes(50);
**pdl>** $c = $z50->xlinvals(0,7)+i*$z50->xlinvals(2,4);
**pdl>** line im sin $c; hold; # the imaginary part
**pdl>** line re sin $c; # real
**pdl>** line abs sin $c; release; # and the modulus

Output
```
```**pdl>** #
**pdl>** # more complex numbers
**pdl>** #
**pdl>** use PDL::Complex;
**pdl>** $c = zeroes(300)->xlinvals(0,12)+i*zeroes(300)->xlinvals(2,10);
**pdl>** $sin = sin $c;
**pdl>** line $sin->im, $sin->re; # look at the result in the complex plane

Output