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):


perldl> # ensure the module is loaded (required for PDL versions>= 2.004)
perldl> use PDL::Graphics::PGPLOT;
perldl> # The size of the window can be specified
perldl> $ENV{PGPLOT_XW_WIDTH}=0.3;
perldl> # You can set your device explicitly
perldl> dev('/XSERVE');

perldl> # First we define some variables to use for the rest of the demo.
perldl> $x=sequence(10);
perldl> $y=2*$x**2;
perldl> # Now a simple plot with points
perldl> points $x, $y;
[Thumbnail] Output

perldl> # Here is the same with lines
perldl> line $x, $y;
[Thumbnail] Output

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

perldl> bin $x, $y;
perldl> # This plots a binned histogram of the data and as you can
perldl> # see it made a new plot.
[Thumbnail] Output

perldl> # 2D data can also easily be accomodated:
perldl> # First make a simple image
perldl> $gradient=sequence(40,40);
perldl> # Then display it.
perldl> imag $gradient;
perldl> # And overlay a contour plot over it:
perldl> hold;
perldl> cont $gradient;
perldl> release;

perldl> # PDL::Graphics::PGPLOT contains several colour tables,
perldl> # a more extensive collection can be found in 
perldl> # PDL::Graphics::LUT
perldl> #
perldl> # (note: the call to lut_names() can take a few seconds to execute)
perldl> #
perldl> use PDL::Graphics::LUT;
perldl> @names = lut_names();
perldl> print "Available tables: [ ", @names, " ]\n";
perldl> # use the first table
perldl> ctab( lut_data($names[0]) );
perldl> use PGPLOT;
perldl> 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 ]
[Thumbnail] Output

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

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

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

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

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

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

perldl> #
perldl> # more complex numbers
perldl> #
perldl> use PDL::Complex;
perldl> $c =  zeroes(300)->xlinvals(0,12)+i*zeroes(300)->xlinvals(2,10);
perldl> $sin = sin $c;
perldl> line $sin->im, $sin->re;   # look at the result in the complex plane
[Thumbnail] Output
This page was written by Robert Schwebel and various other pdl-porters. Please send bugs and ideas for this web page to pdl-porters@jach.hawaii.edu.
Last modified: Tuesday, 08-Apr-2008 10:31:03 PDT

Valid HTML 4.01 Transitional Valid CSS!