an introduction to plotting in perl using pdl::graphics::plplot

92
An Introduction to PDL::Graphics::PLplot David Mertens July 7, 2010

Upload: dcmertens

Post on 18-Dec-2014

6.963 views

Category:

Education


1 download

DESCRIPTION

This is an introduction to the Perl (PDL) bindings for the PLplot plotting library.

TRANSCRIPT

Page 1: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

An Introduction to PDL::Graphics::PLplot

David Mertens

July 7, 2010

Page 2: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Introduction

Introduction

PDL and PLplot

PLplot

PDL Bindings

Alternatives

First Steps

Multiple Curves

Boxes and Viewports

Other Methods

Text

Using the mem

Device

Miscellaneous

Conclusions

Page 3: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

PDL and PLplot

Introduction

PDL and PLplot

PLplot

PDL Bindings

Alternatives

First Steps

Multiple Curves

Boxes and Viewports

Other Methods

Text

Using the mem

Device

Miscellaneous

Conclusions

The Perl Data Language,making number-crunching aseasy as writing a Perl script.

pdl.perl.org

PLplot, a modern open-sourceplotting library written in C.

plplot.sourceforge.net

Page 4: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

PLplot

Introduction

PDL and PLplot

PLplot

PDL Bindings

Alternatives

First Steps

Multiple Curves

Boxes and Viewports

Other Methods

Text

Using the mem

Device

Miscellaneous

Conclusions

Written in C

Bindings for Lisp, Perl (PDL), Python, OCaml, etc

Strong separation between plotting commands and outputdevices

All plotting commands can be used on all output devices

New devices are relatively easy to write - only require ahandful of commands

Well documented with many examples on their website

Downside: worked very hard on cross-platformcompatibility, but installation is still tricky sometimes

Page 5: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

PDL::Graphics::PLplot

Introduction

PDL and PLplot

PLplot

PDL Bindings

Alternatives

First Steps

Multiple Curves

Boxes and Viewports

Other Methods

Text

Using the mem

Device

Miscellaneous

Conclusions

High-level object-oriented in-terface

Easier

Perlish feel

Only a handful of func-tions

Tweak plots by specifyingoptions

Incomplete

Low-level wrappers of (mostof) the C interface

More powerful

Feels like C

Many functions

Tweak plots by callingtweaking functions

Page 6: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Alternatives

Introduction

PDL and PLplot

PLplot

PDL Bindings

Alternatives

First Steps

Multiple Curves

Boxes and Viewports

Other Methods

Text

Using the mem

Device

Miscellaneous

Conclusions

Alternatives to using PLplot:

PGPLOT (requires Fortran to compile)

Asymptote, with Perl and PDL bindings (slow)

gnuplot (no PDL interface)

PDL::Graphics::TriD (not publication quality)

Karma ??

... and many others

Page 7: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

First Steps

Introduction

First Steps

Loading PLplot

Hello PLplot

Labels and Title

Setting the Device

Plotting Points

Multiple Curves

Boxes and Viewports

Other Methods

Text

Using the mem

Device

Miscellaneous

Conclusions

Page 8: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Loading PLplot

Introduction

First Steps

Loading PLplot

Hello PLplot

Labels and Title

Setting the Device

Plotting Points

Multiple Curves

Boxes and Viewports

Other Methods

Text

Using the mem

Device

Miscellaneous

Conclusions

To load PLplot, just use the bindings module:

1 use strict;

2 use warnings;

3 use PDL;

4 use PDL::Graphics::PLplot;

5

6 # ...

To save myself keystrokes, I will make use of the aliasedmodule, available from CPAN, making these two pairs ofstatements equivalent:

1 use aliased ’PDL::Graphics::PLplot’;

2 my $pl = PLplot->new( ’args...’ );

3

4 # works the same as:5 use PDL::Graphics::PLplot;

6 my $pl = PDL::Graphics::PLplot->new( ’args...’ );

Page 9: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Hello PLplot

new([OPTIONS]) creates a new PLplot objectxyplot($x, $y, [OPTIONS]) plots x vs y dataclose() closes the PLplot object and finalizes the plot

1 use strict; use warnings; use PDL;

2 use PDL::Graphics::PLplot;

3

4 # Generate a time series5 my $time = sequence(100)/10;

6 my $sinewave = 5 * sin($time);

7

8 # Prompts the user for the device9 # and file name:

10 my $pl = PDL::Graphics::PLplot->new;11

12 # Plot the time series13 $pl->xyplot($time, $sinewave);

14

15 # Close the PLplot object to finalize16 $pl->close; 0 2 4 6 8

-4-2

02

4

Page 10: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Labels and Title

How do we add axis labels and a title? Options XLAB, YLAB specify the axislabels and TITLE specifies the plot title

1 use strict; use warnings; use PDL;

2 use aliased ’PDL::Graphics::PLplot’;

3

4 # Generate a time series5 my $time = sequence(100)/10;

6 my $sinewave = 5 * sin($time);

7

8 # Create the PLplot object:9 my $pl = PLplot->new;

10

11 # Plot the time series12 $pl->xyplot($time, $sinewave13 , XLAB => ’time [s]’14 , YLAB => ’position [cm]’

15 , TITLE => ’Mass on Spring’

16 );

17

18 # Close the PLplot object to finalize19 $pl->close;

Mass on Spring

time [s]

posit

ion [

cm

]

0 2 4 6 8

-4-2

02

4

Page 11: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Setting the Device

Introduction

First Steps

Loading PLplot

Hello PLplot

Labels and Title

Setting the Device

Plotting Points

Multiple Curves

Boxes and Viewports

Other Methods

Text

Using the mem

Device

Miscellaneous

Conclusions

PLplot supports multiple devices. Specify them in your call tonew. For output to a window:

option DEV must be set to xwin, wxwidgets, or similar

For output to a file:

option DEV must be set to xfig, svg, pscairo, or similar

option FILE must give the output file’s name

For output to a memory buffer:

option DEV must be set to mem or memcairo

option MEM must be passed a piddle where the results willbe plot

Page 12: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Setting the Device

Introduction

First Steps

Loading PLplot

Hello PLplot

Labels and Title

Setting the Device

Plotting Points

Multiple Curves

Boxes and Viewports

Other Methods

Text

Using the mem

Device

Miscellaneous

Conclusions

1 use strict; use warnings; use PDL;

2 use PDL::Graphics::PLplot;

3

4 # Generate a time series5 my $time = sequence(100)/10;

6 my $sinewave = 5 * sin($time);

7

8 # Create a PLplot wxwidgets object:9 my $pl = PDL::Graphics::PLplot->new(

10 DEV => ’wxwidgets’

11 );

12

13 # Plot the time series14 $pl->xyplot($time, $sinewave

15 , XLAB => ’time [s]’, YLAB => ’position [cm]’

16 , TITLE => ’Mass on Spring’

17 );

18

19 # Close the PLplot object to finalize20 $pl->close;

Page 13: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Setting the Device

Introduction

First Steps

Loading PLplot

Hello PLplot

Labels and Title

Setting the Device

Plotting Points

Multiple Curves

Boxes and Viewports

Other Methods

Text

Using the mem

Device

Miscellaneous

Conclusions

1 use strict; use warnings; use PDL;

2 use PDL::Graphics::PLplot;

3

4 # Generate a time series5 my $time = sequence(100)/10;

6 my $sinewave = 5 * sin($time);

7

8 # Save the image to a postscript file9 my $pl = PDL::Graphics::PLplot->new(

10 DEV => ’ps’11 , FILE => ’myfile.eps’

12 );

13

14 # Plot the time series15 $pl->xyplot($time, $sinewave

16 , XLAB => ’time [s]’, YLAB => ’position [cm]’

17 , TITLE => ’Mass on Spring’

18 );

19

20 # Close the PLplot object to finalize21 $pl->close;

Page 14: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Plotting Points

Introduction

First Steps

Loading PLplot

Hello PLplot

Labels and Title

Setting the Device

Plotting Points

Multiple Curves

Boxes and Viewports

Other Methods

Text

Using the mem

Device

Miscellaneous

Conclusions

You can plot lines, symbols, or both by using the PLOTTYPEoption. You specify error bars in x and y by passing a scalar or apiddle with those errors to XERRORBAR and YERRORBAR.

PLOTTYPE => LINE plots data as lines (default)

PLOTTYPE => POINTS plots data as points

PLOTTYPE => LINEPOINTS plots data as lines and points

PLplot’s built in error-bars can plot asymmetric error bars,but the high-level PDL bindings do not support this.

Page 15: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Plotting Points

Introduction

First Steps

Loading PLplot

Hello PLplot

Labels and Title

Setting the Device

Plotting Points

Multiple Curves

Boxes and Viewports

Other Methods

Text

Using the mem

Device

Miscellaneous

Conclusions

To set the symbol type and size, use the SYMBOL andSYMBOLSIZE options.

Symbol sizes are measured as multiples of the default size

Symbol sizes can be fractional, such as 0.7 or 4.5

Symbols are identified by their number

Symbol numbers are shown on the PLplot demo page 7 athttp://plplot.sourceforge.net/examples.php?demo=071

1Warning: I had to choose a symbol numbered 200 or higher. YMMV.

Page 16: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Symbols Example

1 use strict; use warnings; use PDL;

2 use aliased ’PDL::Graphics::PLplot’;

3

4 # Generate a time series5 my $time = sequence(100)/10;

6 my $sinewave = 5 * sin($time);

7

8 # Save the image to a postscript file9 my $pl = PLplot->new(

10 DEV => ’pscairo’

11 , FILE => ’Symbols.eps’

12 );

13

14 # Plot the time series as points15 $pl->xyplot($time, $sinewave16 , PLOTTYPE => ’POINTS’

17 , SYMBOL => 84318 , YERRORBAR => grandom($time)/2

19 );

20

21 $pl->close;

0 2 4 6 8

-4-2

02

4

Page 17: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Multiple Curves

Introduction

First Steps

Multiple Curves

TIMTOWTDI

Multidim Piddles

Multiple xyplots

Problems

Solutions

stripplots

stripplots and

rcols

SUBPAGES

Insets

Boxes and Viewports

Other Methods

Text

Using the mem

Device

Miscellaneous

Conclusions

Page 18: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

TIMTOWTDI

Introduction

First Steps

Multiple Curves

TIMTOWTDI

Multidim Piddles

Multiple xyplots

Problems

Solutions

stripplots

stripplots and

rcols

SUBPAGES

Insets

Boxes and Viewports

Other Methods

Text

Using the mem

Device

Miscellaneous

Conclusions

Depending on what you want, there are at least five ways to plotmultiple curves on a plot.

plot a multidimensional piddle

call xyplot multiple times

use stripplots

specify SUBPAGES in the constructor

create insets using the VIEWPORT option

Page 19: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Multidimensional Piddles

The easiest way to plot multiple curves is to create a multi-dimensional piddlethat you plot with xyplot:1 use strict; use warnings; use PDL;

2 use aliased ’PDL::Graphics::PLplot’;

3

4 # Generate a time series5 my $time = sequence(100)/10;

6 my $sinewave = 5 * sin($time);

7 my $cosinewave = 4 * cos($time);8 my $toplot = cat($sinewave, $cosinewave);

9

10 # Save the image to a postscript file11 my $pl = PLplot->new(12 DEV => ’pscairo’

13 , FILE => ’Multidimensional.eps’

14 );

15

16 # Plot the time series17 $pl->xyplot($time, $toplot);

18

19 $pl->close;

0 2 4 6 8-4

-20

24

Page 20: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Multidimensional Piddles

Introduction

First Steps

Multiple Curves

TIMTOWTDI

Multidim Piddles

Multiple xyplots

Problems

Solutions

stripplots

stripplots and

rcols

SUBPAGES

Insets

Boxes and Viewports

Other Methods

Text

Using the mem

Device

Miscellaneous

Conclusions

Use color to differentiate different data sets:

For multidimensional piddles, plot as POINTS and use theCOLORMAP and PALETTE options.

For multiple calls to xyplot, use POINTS, COLORMAP, andPALETTE, or use COLOR option.

The COLORMAP option lets you specify a third value for each(x, y) pair, making it (x, y, colorval).Which color is associated with the minimum colorval? Whichcolor is associated with the maximum value? All of these are setwith the PALETTE.

Page 21: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Multidimensional Piddles

Introduction

First Steps

Multiple Curves

TIMTOWTDI

Multidim Piddles

Multiple xyplots

Problems

Solutions

stripplots

stripplots and

rcols

SUBPAGES

Insets

Boxes and Viewports

Other Methods

Text

Using the mem

Device

Miscellaneous

Conclusions

Valid PALETTEs include:

RAINBOW - from red to violet through the spectrumREVERSERAINBOW - violet through red

GREYSCALE - from black to white via greyREVERSEGREYSCALE - from white to black via grey

GREENRED - from green to redREDGREEN - from red to green

Note:

the default palette is not named

this only works when plotting points, not lines or error bars

Page 22: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Multidimensional Piddles

1 use strict; use warnings; use PDL;

2 use aliased ’PDL::Graphics::PLplot’;

3 my $pl = PLplot->new(4 DEV => ’pscairo’

5 ,FILE => ’Multidimensional2.eps’);

6

7 # Generate a time series and phase offset8 my $time = sequence(100)/10;

9 my $phi = zeroes(4)->xlinvals(0, 3)

10 ->transpose;11 my $sinewaves = 5*sin($time + $phi);

12 # Plot the time series and phi color key13 $pl->xyplot($time, $sinewaves14 , PLOTTYPE => ’POINTS’15 , COLORMAP => $phi

16 , TITLE => ’sin(x + #gf)’);

17 $pl->colorkey($phi, ’v’

18 , TITLE => ’#gf’

19 , VIEWPORT

20 => [0.93, 0.96, 0.15, 0.85]);

21 $pl->close;

sin(x + φ)

0 2 4 6 8

-4-2

02

4

φ

01

23

Page 23: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Call xyplot multiple times

Introduction

First Steps

Multiple Curves

TIMTOWTDI

Multidim Piddles

Multiple xyplots

Problems

Solutions

stripplots

stripplots and

rcols

SUBPAGES

Insets

Boxes and Viewports

Other Methods

Text

Using the mem

Device

Miscellaneous

Conclusions

Another way to plot multiple curves on the same plot is to callxyplot multiple times, specifying the COLOR option.

BLACK GREEN WHEAT

BLUE RED AQUAMARINE

GREY BLUEVIOLET YELLOW

PINK BROWN CYAN

TURQUOISE MAGENTA SALMON

WHITE ROYALBLUE DEEPSKYBLUE

VIOLET STEELBLUE1 DEEPPINK

MAGENTA DARKORCHID1 PALEVIOLETRED2

TURQUOISE1 LIGHTSEAGREEN SKYBLUE

FORESTGREEN CHARTREUSE3 GOLD2

SIENNA1 CORAL HOTPINK

LIGHTCORAL LIGHTPINK1 LIGHTGOLDENROD

Page 24: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Call xyplot multiple times

1 use strict; use warnings; use PDL;

2 use aliased ’PDL::Graphics::PLplot’;

3

4 # Generate a time series5 my $time = sequence(100)/10;

6 my $sinewave = 5 * sin($time);

7 my $cosinewave = 4 * cos($time);

8

9 # Save the image to a postscript file10 my $pl = PLplot->new(11 DEV => ’pscairo’

12 , FILE => ’Multiple curves.eps’

13 );

14

15 # Plot the sine in black, cosine in red16 $pl->xyplot($time, $sinewave);

17 $pl->xyplot($time, $cosinewave18 , COLOR => ’RED’);

19

20 $pl->close;

0 2 4 6 8

-4-2

02

4

0 2 4 6 8

-4-2

02

4

Page 25: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Problems with multiple xyplot calls

Introduction

First Steps

Multiple Curves

TIMTOWTDI

Multidim Piddles

Multiple xyplots

Problems

Solutions

stripplots

stripplots and

rcols

SUBPAGES

Insets

Boxes and Viewports

Other Methods

Text

Using the mem

Device

Miscellaneous

Conclusions

Things can easily go awry and not Do What You Mean:

Curve clipping - the first plot sets the plotting boundariesand later plots fall outside of those boundaries

Changing ‘current’ color - the first plot sets the ‘current’color and the second does not specify a color

Also, PLplots has a discrete color limit of 16, includingforeground and background color.

Page 26: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Curve Clipping

1 use strict; use warnings; use PDL;

2 use aliased ’PDL::Graphics::PLplot’;

3

4 # Generate a time series5 my $time = sequence(100)/10;

6 my $sinewave = 5 * sin($time);7 my $cosinewave = 6 * cos($time);

8

9 # Save the image to a postscript file10 my $pl = PLplot->new(11 DEV => ’pscairo’

12 , FILE => ’Multiple curves2.eps’

13 );

14

15 # Plot the sine in black, cosine in red16 $pl->xyplot($time, $sinewave);

17 $pl->xyplot($time, $cosinewave

18 , COLOR => ’RED’);

19

20 $pl->close;

0 2 4 6 8

-4-2

02

4

0 2 4 6 8

-4-2

02

4

Page 27: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Changing Current Colors

1 use strict; use warnings; use PDL;

2 use aliased ’PDL::Graphics::PLplot’;

3

4 # Generate a time series5 my $time = sequence(100)/10;

6 my $sinewave = 5 * sin($time);

7 my $cosinewave = 6 * cos($time);

8

9 # Save the image to a postscript file10 my $pl = PLplot->new(11 DEV => ’pscairo’

12 , FILE => ’Color wart.eps’

13 );

14

15 # Plot the cosine in red16 $pl->xyplot($time, $cosinewave

17 , COLOR => ’RED’);18 # Plot the sine in black19 # ERROR: current color is red!

20 $pl->xyplot($time, $sinewave);

21

22 $pl->close;

0 2 4 6 8

-4-2

02

46

0 2 4 6 8

-4-2

02

46

Page 28: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Solution to Changing Current Color

1 use strict; use warnings; use PDL;

2 use aliased ’PDL::Graphics::PLplot’;

3

4 # Generate a time series5 my $time = sequence(100)/10;

6 my $sinewave = 5 * sin($time);

7 my $cosinewave = 6 * cos($time);

8

9 # Save the image to a postscript file10 my $pl = PLplot->new(11 DEV => ’pscairo’

12 , FILE => ’Color solution.eps’

13 );

14

15 # Plot the cosine in red16 $pl->xyplot($time, $cosinewave

17 , COLOR => ’RED’);

18 # Plot the sine in black19 $pl->xyplot($time, $sinewave20 , COLOR => ’BLACK’);

21

22 $pl->close;

0 2 4 6 8

-4-2

02

46

0 2 4 6 8

-4-2

02

46

Page 29: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Solution to Curve Clipping

1 use strict; use warnings; use PDL;

2 use aliased ’PDL::Graphics::PLplot’;

3

4 # Generate a time series5 my $time = sequence(100)/10;

6 my $sinewave = 5 * sin($time);

7 my $cosinewave = 6 * cos($time);

8

9 # Save the image to a postscript file10 my $pl = PLplot->new(11 DEV => ’pscairo’

12 , FILE => ’Multiple curves3.eps’

13 );

14

15 # Plot the sine with full bounds16 $pl->xyplot($time, $sinewave

17 , BOX => [$time->minmax18 , $cosinewave->minmax]);19 # Plot the cosine in red20 $pl->xyplot($time, $cosinewave

21 , COLOR => ’RED’);

22

23 $pl->close;

0 2 4 6 8

-4-2

02

46

0 2 4 6 8

-4-2

02

46

Page 30: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

stripplots

1 use strict; use warnings; use PDL;

2 use aliased ’PDL::Graphics::PLplot’;

3

4 # Save the image to a postscript file5 my $pl = PLplot->new(6 DEV => ’pscairo’

7 , FILE => ’stripplots.eps’

8 );

9

10 # Generate a time series11 my $time = sequence(100)/10;

12

13 # Make stripplots with the14 # different time series15 $pl->stripplots($time16 , [sin($time), cos($time)]

17 , XLAB => ’x’

18 , YLAB => [’sine’, ’cosine’]

19 , COLOR => [’BLUE’, ’RED’]

20 , TITLE => ’Sine and Cosine’

21 );

22

23 $pl->close;

0 2 4 6 8

-1.0

0.0

1.0

sin

e

x

-1.0

0.0

1.0

cosin

e

Sine and Cosine

Page 31: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

stripplots

1 use strict; use warnings; use PDL;

2 use aliased ’PDL::Graphics::PLplot’;

3

4 # Save the image to a postscript file5 my $pl = PLplot->new(6 DEV => ’pscairo’

7 , FILE => ’stripplots.eps’

8 );

9

10 # Generate a time series11 my $time = sequence(100)/10;

12 my $data13 = cat(sin($time), cos($time));

14 # Make stripplots with the15 # different time series16 $pl->stripplots($time, $data

17 , XLAB => ’x’

18 , YLAB => [’sine’, ’cosine’]

19 , COLOR => [’BLUE’, ’RED’]

20 , TITLE => ’Sine and Cosine’

21 );

22

23 $pl->close;

0 2 4 6 8

-1.0

0.0

1.0

sin

e

x

-1.0

0.0

1.0

cosin

e

Sine and Cosine

Page 32: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

stripplots and rcols

Introduction

First Steps

Multiple Curves

TIMTOWTDI

Multidim Piddles

Multiple xyplots

Problems

Solutions

stripplots

stripplots and

rcols

SUBPAGES

Insets

Boxes and Viewports

Other Methods

Text

Using the mem

Device

Miscellaneous

Conclusions

1 use strict; use warnings; use PDL;

2 use PDL::Graphics::PLplot;

3

4 my ($t, $data) = rcols(*DATA, 0, []);

5

6 my $pl = PDL::Graphics::PLplot->new;7

8 # Make stripplots with the different time series9 $pl->stripplots($t, $data->transpose);

10

11 $pl->close;12

13 DATA

14 # t x1 x2 x315 1 4 6 -1

16 2 3 9 3

17 3 2 8 7

18 3 -1 4 10

19 5 1 2 6

20 6 5 -1 5

Page 33: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

SUBPAGES

Introduction

First Steps

Multiple Curves

TIMTOWTDI

Multidim Piddles

Multiple xyplots

Problems

Solutions

stripplots

stripplots and

rcols

SUBPAGES

Insets

Boxes and Viewports

Other Methods

Text

Using the mem

Device

Miscellaneous

Conclusions

When you create your PLplot object, you can carve the canvasinto immutable subpages.

1 my $pl = PDL::Graphics::PLplot->new(2 # ...3 , SUBPAGES => [$nx, $ny]

4 );

To advance to a new subpage, specify the SUBPAGE option.

1 # Advance to next subpage2 $pl->xyplot($x, $y

3 # other options...4 , SUBPAGE => 0

5 # other options...6 );

7

8 # Advance to fourth subpage9 $pl->xyplot($x, $y

10 # other options...11 , SUBPAGE => 4

12 # other options...13 );

Page 34: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

SUBPAGES

1 use strict; use warnings; use PDL;

2 use aliased ’PDL::Graphics::PLplot’;

3

4 # Generate a time series5 my $time = sequence(100)/10;

6

7 # Save the image to a postscript file8 my $pl = PLplot->new(9 DEV => ’pscairo’

10 , FILE => ’subpages.eps’11 , SUBPAGES => [2,2]);

12

13 # Plot the time series14 $pl->xyplot($time, sin($time)

15 , TITLE => ’Sine’);

16 $pl->xyplot($time, cos($time)17 , TITLE => ’Cosine’, SUBPAGE => 0);

18 $pl->xyplot($time, tan($time)19 , TITLE => ’Tangent’, SUBPAGE => 4);

20 $pl->xyplot($time, $time**221 , TITLE => ’Squared’, SUBPAGE => 3);

22

23 $pl->close;

Sine

0 2 4 6 8

-0.5

0.0

0.5

Cosine

0 2 4 6 8

-0.5

0.0

0.5

Tangent

0 2 4 6 8

-0.5

0.0

0.5

Squared

0 2 4 6 8

-0.5

0.0

0.5

Page 35: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Insets

Sometimes you want a small inset in one of the corners of your plot. If youwant to do this you should:

Specify the VIEWPORT

Specify the BOX

Use a smaller CHARSIZE

If the underlying plot has a title, you should probably undefine it

undefine or change the XLAB and YLAB unless you want to use thevalues from the underlying plot

Page 36: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Insets

1 use strict; use warnings;

2 use PDL::Graphics::PLplot;

3 use PDL; use PDL::NiceSlice;

4

5 # Generate a noisy time series6 my $time = sequence(1000) /10;

7 my $sinewave = 1 * sin($time) + grandom($time) / 3;

8

9 # Save the image to a postscript file10 my $pl = PDL::Graphics::PLplot->new(DEV => ’pscairo’, FILE => ’inset.eps’);

11

12 # Plot subset as the main plot13 $pl->xyplot($time(0:65), $sinewave(0:65), TITLE => ’Noisy Pendulum’

14 , YLAB => ’Displacement d [m]’, XLAB => ’Time t [s]’);

15

16 # Plot full data set as inset17 $pl->xyplot($time, $sinewave

18 , TITLE => undef

19 , VIEWPORT => [0.525, 0.825, 0.525, 0.775]

20 , BOX => [$time->minmax, $sinewave->minmax]21 , CHARSIZE => 0.6

22 );

23 $pl->close;

Page 37: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Insets

Noisy Pendulum

Time t [s]

Dis

pla

cem

ent

d [

m]

0 2 4 6

-10

1

Time t [s]

Dis

pla

cem

ent

d [

m]

0 20 40 60 80

-10

12

Page 38: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Boxes and Viewports

Introduction

First Steps

Multiple Curves

Boxes and Viewports

Three Layers

Surface Dimensions

Viewport Positioning

Clipping Box

Examples

Summary

Other Methods

Text

Using the mem

Device

Miscellaneous

Conclusions

Page 39: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Three Layers of Coordinates

Introduction

First Steps

Multiple Curves

Boxes and Viewports

Three Layers

Surface Dimensions

Viewport Positioning

Clipping Box

Examples

Summary

Other Methods

Text

Using the mem

Device

Miscellaneous

Conclusions

PLplot has three distinct measurements for your plot at anypoint:

the plotting surface’s dimensions

the viewport’s relative extent

the ‘natural’ coordinates within the viewport

Page 40: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Surface Dimensions

Introduction

First Steps

Multiple Curves

Boxes and Viewports

Three Layers

Surface Dimensions

Viewport Positioning

Clipping Box

Examples

Summary

Other Methods

Text

Using the mem

Device

Miscellaneous

Conclusions

The dimensions of the canvas or surface that you are using canbe specified in the constructor (and cannot be changed later):

1 my $pl = PDL::Graphics::PLplot->new(2 # other options...3 PAGESIZE => [$width, $height]

4 # other options...5 );

These are measured either in pixels or milimeters depending onwhether the underlying format is a raster or vector format.

Page 41: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Viewport Positioning

Introduction

First Steps

Multiple Curves

Boxes and Viewports

Three Layers

Surface Dimensions

Viewport Positioning

Clipping Box

Examples

Summary

Other Methods

Text

Using the mem

Device

Miscellaneous

Conclusions

The viewport carves out a chunk of space on the canvas forplotting and can be changed with each plotting function.

1 $pl->xyplot($x, $y

2 # other options3 , VIEWPORT => [$xmin, $xmax, $ymin, $ymax]

4 # other options5 );

6

7 # Plot on right half of the page8 VIEWPORT => [0.5, 1, 0, 1]

9 # Plot in upper half of the page10 VIEWPORT => [0, 1, 0, 0.5]

11 # Vertically centered, horizontally offset12 VIEWPORT => [0.5, 0.7, 0.4, 0.6]

Viewport values are fractions of the full page (or sub-page) width– all four values should be a number between 0 and 1.

Page 42: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Clipping Box

Introduction

First Steps

Multiple Curves

Boxes and Viewports

Three Layers

Surface Dimensions

Viewport Positioning

Clipping Box

Examples

Summary

Other Methods

Text

Using the mem

Device

Miscellaneous

Conclusions

If the viewport indicates the chunk of space you will be graphingon, the clipping box indicates the coordinates within that chunkof space.1 $pl->xyplot($x, $y

2 # other options...3 , BOX => [$xmin, $xmax, $ymin, $ymax]

4 # other options...5 );

6

7 # x runs from 0 to 10, y from -8 to 8:8 BOX => [0, 10, -8, 8]

9 # piddles have the minmax method:10 BOX => [$x pdl->minmax, $y pdl->minmax]

When plotting using the specified box, a data point near (0,−8)will be plotted near the lower left corner and a data point near(5, 0) will be plotted at the center.Viewports define where plots are drawn. Tick labels, axis labels,and plot titles are drawn outside the viewport.

Page 43: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Examples

Introduction

First Steps

Multiple Curves

Boxes and Viewports

Three Layers

Surface Dimensions

Viewport Positioning

Clipping Box

Examples

Summary

Other Methods

Text

Using the mem

Device

Miscellaneous

Conclusions

Here are some examples to show how each of these work.

Page 44: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Starting Example

1 use strict; use warnings; use PDL;

2 use aliased ’PDL::Graphics::PLplot’;

3

4 my $x = zeroes(20)->xlinvals(-3, 3);

5 my $y = $x**2;

6

7 # Set the backgound to blue:8 my $pl = PLplot->new(9 DEV => ’pscairo’

10 , FILE => ’box example 1.eps’11 , BACKGROUND => ’SKYBLUE’

12 );

13

14 # Plot a quadratic function:15 $pl->xyplot($x, $y

16 , YLAB => ’y’, XLAB => ’x’);

17

18 $pl->close;

x

y

-2 0 2

24

68

I set the background to blue so you can see the extent of the canvas.

Page 45: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Page Size Example

1 use strict; use warnings; use PDL;

2 use aliased ’PDL::Graphics::PLplot’;

3

4 my $x = zeroes(20)->xlinvals(-3, 3);

5 my $y = $x**2;

6

7 # Set a custom page size8 my $pl = PLplot->new(9 DEV => ’pscairo’

10 , FILE => ’box example 2.eps’

11 , BACKGROUND => ’SKYBLUE’12 , PAGESIZE => [360, 240]

13 );

14

15 # Plot a quadratic function:16 $pl->xyplot($x, $y

17 , YLAB => ’y’, XLAB => ’x’);

18

19 $pl->close;

x

y

-2 0 2

24

68

Page 46: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Viewport Example - Upper Right

1 use strict; use warnings; use PDL;

2 use aliased ’PDL::Graphics::PLplot’;

3

4 my $x = zeroes(20)->xlinvals(-3, 3);

5 my $y = $x**2;

6 my $pl = PLplot->new(7 DEV => ’pscairo’

8 , FILE => ’box example 3.eps’

9 , BACKGROUND => ’SKYBLUE’

10 );

11

12 # Put the plot in the upper right:13 $pl->xyplot($x, $y

14 , YLAB => ’y’, XLAB => ’x’

15 , VIEWPORT16 => [0.5, 0.9, 0.6, 0.8]);

17

18 $pl->close;

x

y

-2 0 2

24

68

Page 47: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Viewport Example - Centered

1 use strict; use warnings; use PDL;

2 use aliased ’PDL::Graphics::PLplot’;

3

4 my $x = zeroes(20)->xlinvals(-3, 3);

5 my $y = $x**2;

6 my $pl = PLplot->new(7 DEV => ’pscairo’

8 , FILE => ’box example 4.eps’

9 , BACKGROUND => ’SKYBLUE’

10 );

11

12 # Center the plot13 $pl->xyplot($x, $y

14 , YLAB => ’y’, XLAB => ’x’

15 , VIEWPORT16 => [0.3, 0.7, 0.3, 0.7]);

17

18 $pl->close;

x

y

-2 0 2

24

68

Page 48: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Viewport Example - Extreme Bounds

1 use strict; use warnings; use PDL;

2 use aliased ’PDL::Graphics::PLplot’;

3

4 my $x = zeroes(20)->xlinvals(-3, 3);

5 my $y = $x**2;

6 my $pl = PLplot->new(7 DEV => ’pscairo’

8 , FILE => ’box example 5.eps’

9 , BACKGROUND => ’SKYBLUE’

10 );

11

12 # Try extreme bounds for the viewport13 $pl->xyplot($x, $y

14 , YLAB => ’y’, XLAB => ’x’

15 , VIEWPORT16 => [0, 1, 0.3, 1]);

17

18 $pl->close;

x-2 0 2

Page 49: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Viewport Example - Multiple Plots

1 use strict; use warnings; use PDL;

2 use aliased ’PDL::Graphics::PLplot’;

3

4 my $x = zeroes(20)->xlinvals(-3, 3);

5 my $y = $x**2;

6 my $pl = PLplot->new(7 DEV => ’pscairo’

8 , FILE => ’box example 6.eps’

9 , BACKGROUND => ’SKYBLUE’);

10

11 # Big plot on left12 $pl->xyplot($x, $y, VIEWPORT13 => [0.1, 0.6, 0.1, 0.8]);

14 # Medium plot on upper right15 $pl->xyplot($x, $y, VIEWPORT16 => [0.5, 0.9, 0.6, 0.9]);

17 # Small plot on lower right18 $pl->xyplot($x, $y, VIEWPORT19 => [0.7, 0.9, 0.1, 0.4]);

20

21 $pl->close;

-2 0 2

24

68

-2 0 2

24

68

-2 0 2

24

68

Page 50: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Box Example - Default Box

1 use strict; use warnings; use PDL;

2 use aliased ’PDL::Graphics::PLplot’;

3

4 my $x = zeroes(20)->xlinvals(-3, 3);

5 my $pl = PLplot->new(6 DEV => ’pscairo’

7 , FILE => ’box example 7.eps’

8 , BACKGROUND => ’SKYBLUE’);

9

10 # Sine wave on top11 $pl->xyplot($x, sin($x), VIEWPORT

12 => [0.1, 0.9, 0.55, 0.9]);

13 # Quadratic on bottom14 # BOX is inherited from first plot

15 $pl->xyplot($x, $x**2, VIEWPORT

16 => [0.1, 0.9, 0.1, 0.45]);

17

18

19 $pl->close;

-2 0 2

-0.5

0.0

0.5

-2 0 2

-0.5

0.0

0.5

Page 51: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Box Example - Tweaked Box

1 use strict; use warnings; use PDL;

2 use aliased ’PDL::Graphics::PLplot’;

3

4 my $x = zeroes(20)->xlinvals(-3, 3);

5 my $pl = PLplot->new(6 DEV => ’pscairo’

7 , FILE => ’box example 8.eps’

8 , BACKGROUND => ’SKYBLUE’);

9

10 # Sine wave on top11 $pl->xyplot($x, sin($x), VIEWPORT

12 => [0.1, 0.9, 0.55, 0.9]);

13 # Quadratic on bottom14 $pl->xyplot($x, $x**2, VIEWPORT

15 => [0.1, 0.9, 0.1, 0.45]16 , BOX => [-3, 3, 0, 9]);

17

18

19 $pl->close;

-2 0 2

-0.5

0.0

0.5

-2 0 2

02

46

8

Page 52: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Box Example - Two Plots

1 use strict; use warnings; use PDL;

2 use aliased ’PDL::Graphics::PLplot’;

3

4 my $x = zeroes(20)->xlinvals(-3, 3);

5 my $pl = PLplot->new(6 DEV => ’pscairo’

7 , FILE => ’box example 9.eps’

8 , BACKGROUND => ’SKYBLUE’);

9

10 # Sine wave11 $pl->xyplot($x, sin($x));

1213 # Plotting a quadratic on top works14 # but the bounds are not good

15 $pl->xyplot($x, $x**2);

16

17 $pl->close;

-2 0 2

-0.5

0.0

0.5

-2 0 2

-0.5

0.0

0.5

Page 53: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Box Example - Changing Box but not Viewport

1 use strict; use warnings; use PDL;

2 use aliased ’PDL::Graphics::PLplot’;

3

4 my $x = zeroes(20)->xlinvals(-3, 3);

5 my $pl = PLplot->new(6 DEV => ’pscairo’

7 , FILE => ’box example 10.eps’

8 , BACKGROUND => ’SKYBLUE’);

9

10 # Sine wave11 $pl->xyplot($x, sin($x));

12

13 # Changing the box for the quadratic14 # does not work - bad y ticks

15 $pl->xyplot($x, $x**216 , BOX => [-3, 3, 0, 9]);

17

18 $pl->close;

-2 0 2

-0.5

0.0

0.5

-2 0 2

02

46

8

Page 54: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Summary

Introduction

First Steps

Multiple Curves

Boxes and Viewports

Three Layers

Surface Dimensions

Viewport Positioning

Clipping Box

Examples

Summary

Other Methods

Text

Using the mem

Device

Miscellaneous

Conclusions

For multiple plots on the same viewport, set the box withthe first call to xyplot

For non-overlapping plots (on different viewports), specifythe box as necessary

The viewport specifies the extent of the plotting region; ticklabels, axis labels, and titles are drawn outside the viewport

Page 55: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Other Methods

Introduction

First Steps

Multiple Curves

Boxes and Viewports

Other Methods

Sticky Options

Overview

shadeplot

histogram

bargraph

setparm

Text

Using the mem

Device

Miscellaneous

Conclusions

Page 56: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Sticky Options

Introduction

First Steps

Multiple Curves

Boxes and Viewports

Other Methods

Sticky Options

Overview

shadeplot

histogram

bargraph

setparm

Text

Using the mem

Device

Miscellaneous

Conclusions

Once you specify a plotting option, the option will carry over tofuture calls on the same PLplot object.

Page 57: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Overview

Introduction

First Steps

Multiple Curves

Boxes and Viewports

Other Methods

Sticky Options

Overview

shadeplot

histogram

bargraph

setparm

Text

Using the mem

Device

Miscellaneous

Conclusions

The high-level object-oriented interface in PDL::Graphics::PLplothas a handful of methods:

new, close create and finalize plot objects

xyplot, stripplots 2D plotting

shadeplot ‘topographical’ 3D data representation

histogram plot distribution of 1D data

bargraph plot distribution of categorical data

text annotate plots

setparm set various plotting parameters

Page 58: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

shadeplot

1 use strict; use warnings; use PDL;

2 use aliased ’PDL::Graphics::PLplot’;

3

4 my $pl = PLplot->new(5 DEV => ’pscairo’

6 , FILE => ’shadeplot2.eps’);

7

8 # Have x run from -10 to 109 # and y run from 1 to 7:

10 my $x = zeroes(51)

11 ->xlinvals(-10, 10);

12 my $y = zeroes(51)->xlinvals(1, 7);

13 # Define z = sin(x) + cos(y), a 2D piddle:14 my $z = sin($x)

15 + cos($y->transpose);16

17 # Make a shade plot with 15 color steps:18 $pl->shadeplot($z, 15, BOX => [$x->minmax, $y->minmax]);19

20 # Indicate the color scaling:21 $pl->colorkey($z, ’v’, VIEWPORT => [0.93, 0.96, 0.15, 0.85]);

22

23 $pl->close;

-10 -5 0 5 10

24

6

-10

1

Page 59: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

shadeplot

1 use strict; use warnings; use PDL;

2 use aliased ’PDL::Graphics::PLplot’;

3

4 my $pl = PLplot->new(5 DEV => ’pscairo’

6 , FILE => ’shadeplot3.eps’);

7

8 # Define z = sin(x) + cos(y), a 2D piddle:9 my $x=zeroes(51)->xlinvals(-10, 10);

10 my $y=zeroes(51)->xlinvals(1, 7);

11 my $z=sin($x) + cos($y->transpose);12

13 # Make a shade plot with 15 color steps:14 $pl->shadeplot($z, 15

15 , BOX => [$x->minmax, $y->minmax]16 , XLAB => ’x’, YLAB => ’y’

17 , TITLE => ’Egg Carton’);

18 # Add a ’vertical’ color key:19 $pl->colorkey($z, ’v’, VIEWPORT

20 => [0.93, 0.96, 0.15, 0.85]21 , XLAB => ’’, YLAB => ’’, TITLE => ’depth’);

22

23 $pl->close;

Egg Carton

x

y

-10 -5 0 5 10

24

6

depth

-10

1

Page 60: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

histogram

1 use strict; use warnings; use PDL;

2 use aliased ’PDL::Graphics::PLplot’;

3

4 my $pl = PLplot->new(5 DEV => ’pscairo’

6 , FILE => ’histogram.eps’);

7

8 # Generate some data:9 my $data = grandom(1000);

10

11 # Make a histogram of that data in 20 bins:12 $pl->histogram($data, 20);

13

14 $pl->close;-2 -1 0 1 2

0200

400

600

800

1000

Page 61: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

histogram

1 use strict; use warnings; use PDL;

2 use aliased ’PDL::Graphics::PLplot’;

3

4 my $pl = PLplot->new(5 DEV => ’pscairo’

6 , FILE => ’histogram2.eps’);

7

8 # Generate some data:9 my $data = grandom(1000);

10

11 # Get approximate binning:12 my $nbins = 20;

13 my $binwidth =

14 ($data->max-$data->min) / $nbins;15 my ($x, $y) = hist($data

16 , $data->minmax, $binwidth);

17

18 # Make a histogram of that data in 20 bins:19 my $fudgefactor = 1.1;

20 $pl->histogram($data, $nbins21 , BOX => [$x->minmax, 0, $y->max * $fudgefactor]);

22

23 $pl->close;

-3 -2 -1 0 1 2

020

40

60

80

100

120

140

Page 62: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

bargraph

1 use strict; use warnings; use PDL;

2 use aliased ’PDL::Graphics::PLplot’;

3

4 my $pl = PLplot->new(5 DEV => ’pscairo’

6 , FILE => ’bargraph.eps’);

7

8 # Generate some data:9 my @colors = qw(red orange yellow

10 green blue purple);

11 my $votes = random(scalar(@colors));

12

13 # Normalize the votes14 $votes /= $votes->sum;15

16 # Make a barchart of the votes.17 $pl->bargraph(\@colors, $votes);

18

19 $pl->close;0.10

0.15

0.20

red

orange

yellow

green

blue

purple

Page 63: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

bargraph

1 use strict; use warnings; use PDL;

2 use aliased ’PDL::Graphics::PLplot’;

3

4 my $pl = PLplot->new(5 DEV => ’pscairo’

6 , FILE => ’bargraph2.eps’);

7

8 # Generate some data:9 my @colors = qw(red orange yellow

10 green blue purple);

11 my $votes = random(scalar(@colors));

12

13 # Normalize the votes14 $votes /= $votes->sum;15

16 # Make a barchart of the votes.17 $pl->bargraph(\@colors, $votes

18 , COLOR => ’BLUE’19 , BOX => [0, scalar(@colors), 0, 1.1 * $votes->max]20 );

21

22 $pl->close;

0.00

0.05

0.10

0.15

0.20

0.25

red

orange

yellow

green

blue

purple

Page 64: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

bargraph

1 use strict; use warnings; use PDL;

2 use aliased ’PDL::Graphics::PLplot’;

3

4 my $pl = PLplot->new(5 DEV => ’pscairo’

6 , FILE => ’bargraph3.eps’);

7

8 # voting on letters:9 my @letters = (’a’ .. ’z’);

10 my $votes = random(0 + @letters);

11

12 # Normalize the votes13 $votes /= $votes->sum;14

15 # Make a barchart of the votes.16 $pl->bargraph(\@letters, $votes

17 , COLOR => ’LIGHTGOLDENROD’

18 , BOX => [0, scalar(@letters)

19 , 0, 1.1 * $votes->max]20 , MAXBARLABELS => 10

21 );

22

23 $pl->close;

02

46

8

(x10-2)

a d g j

m p s v y

Page 65: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

setparm

Introduction

First Steps

Multiple Curves

Boxes and Viewports

Other Methods

Sticky Options

Overview

shadeplot

histogram

bargraph

setparm

Text

Using the mem

Device

Miscellaneous

Conclusions

Page 66: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Text

Introduction

First Steps

Multiple Curves

Boxes and Viewports

Other Methods

Text

Typesetting

Greek Letters

psfrag

Annotations

Legends

Using the mem

Device

Miscellaneous

Conclusions

Page 67: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Typesetting

Introduction

First Steps

Multiple Curves

Boxes and Viewports

Other Methods

Text

Typesetting

Greek Letters

psfrag

Annotations

Legends

Using the mem

Device

Miscellaneous

Conclusions

Use escape sequences to insert superscripts, subscripts, Greekletters, etc.

#u - superscript until the next #d#d - subscript until the next #u#- - toggle underline mode#+ - toggle overline mode#fn - switch to normal (sans-serif) font#fr - switch to Roman (serif) font#fi - switch to italic font#fs - switch to script font

Unicode is supported though I won’t get into it here.

Page 68: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Typesetting

1 use strict; use warnings; use PDL;

2 use aliased ’PDL::Graphics::PLplot’;

3

4 # Generate a time series5 my $time = sequence(100)/10;

6 my $sinewave = 5 * sin($time);

7

8 # Create the PLplot object:9 my $pl = PLplot->new(

10 DEV => ’pscairo’

11 , FILE => ’Typesetting.eps’);

12

13 # Plot the time series14 $pl->xyplot($time, $sinewave15 , XLAB => ’#fi time #fn [Hz#u-1#d]’16 , YLAB => ’#fiposition#fn [cm]’

17 , TITLE => ’#frMass on Spring’

18 );

19

20 # Close the PLplot object to finalize21 $pl->close;

Mass on Spring

time [Hz-1

]

posit

ion

[cm

]

0 2 4 6 8

-4-2

02

4

Page 69: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Greek Letters

Introduction

First Steps

Multiple Curves

Boxes and Viewports

Other Methods

Text

Typesetting

Greek Letters

psfrag

Annotations

Legends

Using the mem

Device

Miscellaneous

Conclusions

Use the string #gx to print the Greek letter equivalent of x:

A B G D E Z Y H I K L MA B Γ ∆ E Z H Θ I K Λ M

N C O P R S T U F X Q WN Ξ O Π P Σ T Y Φ X Ψ Ω

a b g d e z y h i k l mα β γ δ ǫ ζ η θ ι κ λ µ

n c o p r s t u f x q wν ξ o π ρ σ τ υ φ χ ψ ω

1 # Use greek symbol rho for density:2 $pl->xyplot($radius, $density

3 , YLAB => ’density #gr’

4 , # ...5 );

Page 70: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

psfrag

Introduction

First Steps

Multiple Curves

Boxes and Viewports

Other Methods

Text

Typesetting

Greek Letters

psfrag

Annotations

Legends

Using the mem

Device

Miscellaneous

Conclusions

For LATEX typsetting, post-process eps images with psfrag.

replaces simple strings with any valid LATEX text.

ensures consistent fonts for both images and documents

Do not use the pscairo device. Use ps or psc.

Page 71: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Annotations

Introduction

First Steps

Multiple Curves

Boxes and Viewports

Other Methods

Text

Typesetting

Greek Letters

psfrag

Annotations

Legends

Using the mem

Device

Miscellaneous

Conclusions

To add text to a plot, use the text method, specifying theTEXTPOSITION option. The TEXTPOSITION takes either four orfive arguments. The four-argument form places text outside theviewport along one of its edges:

1 $pl->text($string, TEXTPOSITION => [$side, $disp, $pos, $just]);

$side is one of ’t’, ’b’, ’l’, or ’r’ indicating the top, bottom, left,or right edge

$disp is the number of character heights out from the edge

$pos is the position of the string’s reference point along theedge of the viewport, from 0 to 1

$just indicates the location of the reference point of the string.0 means the reference point is the string’s left edge; 1 indicatesthe right edge

Page 72: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Annotations

Introduction

First Steps

Multiple Curves

Boxes and Viewports

Other Methods

Text

Typesetting

Greek Letters

psfrag

Annotations

Legends

Using the mem

Device

Miscellaneous

Conclusions

The five-argument form places the text within the viewport at anarbitrary position and slope:

1 $pl->text($string, TEXTPOSITION => [$x, $y, $dx, $dy, $just]);

$x, $y are the location of the string’s reference point within theclipping box

$dx, $dy together indicate the slope along which the text isdrawn

$just indicates the location of the reference point of the string.0 means the reference point is the string’s left edge; 1 indicatesthe right edge

Page 73: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Annotations

1 use strict; use warnings; use PDL;

2 use aliased ’PDL::Graphics::PLplot’;

3

4 my $pl = PLplot->new(5 DEV => ’pscairo’

6 , FILE => ’text1.eps’);

7

8 my $x = zeroes(100)->xlinvals(-3,3);9 my $y = $x**2;

10 $pl->xyplot($x, $y);

1112 $pl->setparm(CHARSIZE => 1.2);

13 # x label on the lower right14 $pl->text(’Position x [m]’,15 , TEXTPOSITION => [’b’, 3, 1, 1]);

16 # y label on the upper left17 $pl->text(’Potential Energy V [J]’18 , TEXTPOSITION => [’l’, 3.5, 1, 1]);

19 # title at the center top20 $pl->text(’Harmonic Oscillator’21 , CHARSIZE => 2.5, TEXTPOSITION => [’t’, 1.5, 0.5, 0.5]);

22

23 $pl->close;

-2 0 2

24

68

P [m]

Pote

nti

al Energ

y V

[J]

Harmonic Oscillator

Page 74: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Annotations

1 use strict; use warnings; use PDL;

2 use aliased ’PDL::Graphics::PLplot’;

3

4 my $pl = PLplot->new(5 DEV => ’pscairo’

6 , FILE => ’text2.eps’);

7

8 # Plot a quadratic9 my $x = zeroes(100)->xlinvals(-3,3);

10 my $y = $x**2;

11

12 $pl->xyplot($x, $y, TITLE => ’SHO’

13 , XLAB => ’Position x [m]’

14 , YLAB => ’Potential V [J]’);

15

16 # annotate negative slope at (-2, 4)17 $pl->text(’Slope is negative’18 , TEXTPOSITION => [-1.8, 4.1, 1, -4, 0.5]);

19 # annotate positive slope at (2, 4)20 $pl->text(’Slope is positive’21 , TEXTPOSITION => [1.9, 3.9, 10, 40, 1]);

22

23 $pl->close;

SHO

Position x [m]

Pote

nti

al V

[J]

-2 0 2

24

68

Slo

pe is n

egativ

e

Slo

pe is

posi

tive

Page 75: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Legends

Introduction

First Steps

Multiple Curves

Boxes and Viewports

Other Methods

Text

Typesetting

Greek Letters

psfrag

Annotations

Legends

Using the mem

Device

Miscellaneous

Conclusions

PLplot does not have a command to create legends. We mustmake them ourselves.

Legends are only necessary when plotting discrete data sets.

If possible, use color keys instead of constructing legends byhand.

Page 76: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Legends

Introduction

First Steps

Multiple Curves

Boxes and Viewports

Other Methods

Text

Typesetting

Greek Letters

psfrag

Annotations

Legends

Using the mem

Device

Miscellaneous

Conclusions

1 use strict; use warnings; use PDL;

2 use PDL::Graphics::PLplot;

3 my $pl = PDL::Graphics::PLplot->new(DEV => ’pscairo’

4 , FILE => ’legend.eps’);

5

6 my $x = zeroes(100)->xlinvals(-1.2, 1.2);

7 my @colors = qw(BLACK GREEN BLUE);

8 my @labels = qw(Linear Quadratic Cubic);

9

10 my $legend x = pdl(0.3, 0.5);

11 my $legend y = -0.5;

12

13 # Plot linear, quadratic, and cubic curves with a legend14 for my $i (0..2) 15 $pl->xyplot($x, $x**($i+1), COLOR => $colors[$i]);16 $pl->xyplot($legend x, pdl($legend y, $legend y)

17 , COLOR => $colors[$i]);

18 $pl->text($labels[$i], COLOR => ’BLACK’19 , TEXTPOSITION => [0.6, $legend y, 1, 0, 0]);

20 $legend y -= 0.2;

21 22

23 $pl->close;

Page 77: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Legends

Introduction

First Steps

Multiple Curves

Boxes and Viewports

Other Methods

Text

Typesetting

Greek Letters

psfrag

Annotations

Legends

Using the mem

Device

Miscellaneous

Conclusions

-1.0 -0.5 0.0 0.5 1.0

-1.0

-0.5

0.0

0.5

1.0

-1.0 -0.5 0.0 0.5 1.0

-1.0

-0.5

0.0

0.5

1.0

Linear

-1.0 -0.5 0.0 0.5 1.0

-1.0

-0.5

0.0

0.5

1.0

-1.0 -0.5 0.0 0.5 1.0

-1.0

-0.5

0.0

0.5

1.0

Quadratic

-1.0 -0.5 0.0 0.5 1.0

-1.0

-0.5

0.0

0.5

1.0

-1.0 -0.5 0.0 0.5 1.0

-1.0

-0.5

0.0

0.5

1.0

Cubic

Page 78: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Legends

Introduction

First Steps

Multiple Curves

Boxes and Viewports

Other Methods

Text

Typesetting

Greek Letters

psfrag

Annotations

Legends

Using the mem

Device

Miscellaneous

Conclusions

1 use strict; use warnings; use PDL;

2 use PDL::Graphics::PLplot;

3 my $pl = PDL::Graphics::PLplot->new(DEV => ’pscairo’

4 , FILE => ’no legend.eps’);

5

6 my $x = zeroes(100)->xlinvals(0, 1.2);

7 my $powers = zeroes(7)->xlinvals(1, 4)->transpose;8 my $ys = $x**$powers;

9

10 $pl->xyplot($x, $ys

11 , COLORMAP => $powers

12 , XLAB => ’x’, YLAB => ’x#upower#d’

13 , PALETTE => ’REDGREEN’, PLOTTYPE => ’POINTS’

14 );

15 $pl->colorkey($powers, ’v’

16 , XLAB => undef, YLAB => undef

17 , TITLE => ’power’

18 , VIEWPORT => [0.93, 0.96, 0.15, 0.85]

19 );

20

21 $pl->close;

Page 79: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Legends

Introduction

First Steps

Multiple Curves

Boxes and Viewports

Other Methods

Text

Typesetting

Greek Letters

psfrag

Annotations

Legends

Using the mem

Device

Miscellaneous

Conclusions

x

xpower

0.0 0.2 0.4 0.6 0.8 1.0 1.2

0.0

0.5

1.0

1.5

2.0

power

12

34

Page 80: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Using the mem Device

Introduction

First Steps

Multiple Curves

Boxes and Viewports

Other Methods

Text

Using the mem

Device

Uses for mem DeviceCreating a Memory

BufferPlotting over an

image

Miscellaneous

Conclusions

Page 81: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Uses for mem Device

Introduction

First Steps

Multiple Curves

Boxes and Viewports

Other Methods

Text

Using the mem

Device

Uses for mem DeviceCreating a Memory

BufferPlotting over an

image

Miscellaneous

Conclusions

load an image and plot over that image

plot to a custom windowing device

animated plots

Page 82: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Creating a Memory Buffer

Introduction

First Steps

Multiple Curves

Boxes and Viewports

Other Methods

Text

Using the mem

Device

Uses for mem DeviceCreating a Memory

BufferPlotting over an

image

Miscellaneous

Conclusions

1 ## For the mem device ##2

3 # Allocate the buffer4 my $buffer = zeroes(byte, 3, $width, $height);

5

6 # Create the PLplot object7 my $pl = PDL::Graphics::PLplot->new(8 DEV => ’mem’

9 , MEM => $buffer

10 );

11

12

13 ## For the memcairo device ##14

15 # Allocate the buffer16 my $buffer = zeroes(byte, 4, $width, $height);

17

18 # Create the PLplot object19 my $pl = PDL::Graphics::PLplot->new(20 DEV => ’memcairo’

21 , MEM => $buffer

22 );

Page 83: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Plotting over an image

1 use strict; use warnings; use PDL;

2 use aliased ’PDL::Graphics::PLplot’;3 use PDL::IO::Pic;

4

5 # Load an image6 # (has dims 3 x width x height)7 my $pic = rpic(’starry night.jpg’);

8 # Flip the y axis9 $pic = $pic->slice(’:,:,-1:0:-1’);

10 # Whiten the image a bit11 $pic = 127 + $pic / 2;

12

13 my $pl = PLplot->new(DEV => ’mem’14 , MEM => $pic);

15

16 # Plot a quadratic curve over the image17 my $x=zeroes(51)->xlinvals(-10, 10);

18 $pl->xyplot($x, $x**2);

19 $pl->close;20

21 # flip the y axis back and save the image22 $pic = $pic->slice(’:,:,-1:0:-1’);23 wpic($pic, ’starry plot.png’);

Page 84: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Miscellaneous

Introduction

First Steps

Multiple Curves

Boxes and Viewports

Other Methods

Text

Using the mem

Device

Miscellaneous

use aliased

Log plots

Bad Values

Conclusions

Page 85: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

use aliased

Introduction

First Steps

Multiple Curves

Boxes and Viewports

Other Methods

Text

Using the mem

Device

Miscellaneous

use aliased

Log plots

Bad Values

Conclusions

The aliased module, available on CPAN, makes dealing withlength package names much simpler:

1 use strict;

2 use warnings;

3 use PDL;4 use aliased ’PDL::Graphics::PLplot’;

56 my $pl = PLplot->new(DEV => ’pscairo’

7 , FILE => ’text.eps’);

8

9 # ...

Page 86: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Log plots

Introduction

First Steps

Multiple Curves

Boxes and Viewports

Other Methods

Text

Using the mem

Device

Miscellaneous

use aliased

Log plots

Bad Values

Conclusions

If you need to have one (or both) axes to have a log scale, usethe XBOX or YBOX option, these let you determine certain aspectsof the x and y ticks and tick labels.

1 $pl->xyplot(log($x), $y

2 # include ’l’ for a logarithmic axis3 , XBOX => ’bclnst’

4 );

Page 87: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Bad Values

Introduction

First Steps

Multiple Curves

Boxes and Viewports

Other Methods

Text

Using the mem

Device

Miscellaneous

use aliased

Log plots

Bad Values

Conclusions

The PDL wrapper to PLplot supports bad values.

Bad values are not plotted, and a gap is inserted into the plot.

Page 88: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Conclusions

Introduction

First Steps

Multiple Curves

Boxes and Viewports

Other Methods

Text

Using the mem

Device

Miscellaneous

Conclusions

What I Skipped

Strengths and

Weaknesses

Improvements

Other

Documentation

Page 89: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

What I Skipped

Introduction

First Steps

Multiple Curves

Boxes and Viewports

Other Methods

Text

Using the mem

Device

Miscellaneous

Conclusions

What I Skipped

Strengths and

Weaknesses

Improvements

Other

Documentation

the many low-level commands

3D plots (using low-level commands)

options for creation: BACKGROUND, JUST, OPTS,

ORIENTATION

plotting options: LINEWIDTH, LINESTYLE,

MAJTICKSIZE, MINTICKSIZE, NXSUB, NYSUB, XBOX,

XTICK, YBOX, YTICK, ZRANGE

Page 90: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Strengths and Weaknesses

Introduction

First Steps

Multiple Curves

Boxes and Viewports

Other Methods

Text

Using the mem

Device

Miscellaneous

Conclusions

What I Skipped

Strengths and

Weaknesses

Improvements

Other

Documentation

The PDL PLplot bindings are excellent at plotting 2D data.

The bindings are poor for 3D plotting and have many quirks.

Page 91: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Improvements

Introduction

First Steps

Multiple Curves

Boxes and Viewports

Other Methods

Text

Using the mem

Device

Miscellaneous

Conclusions

What I Skipped

Strengths and

Weaknesses

Improvements

Other

Documentation

better overall documentation

bring low-level functions up-to-date with current PLplotlibrary

create low-level wrappers for colored error bars and lines

better handling of histograms and bargraphs with moreoptions

high level 3D plotting

Alien::PLplot

Page 92: An Introduction to Plotting in Perl using PDL::Graphics::PLplot

Other Documentation

Introduction

First Steps

Multiple Curves

Boxes and Viewports

Other Methods

Text

Using the mem

Device

Miscellaneous

Conclusions

What I Skipped

Strengths and

Weaknesses

Improvements

Other

Documentation

PLplot’s web site:http://plplot.sourceforge.net/documentation.php.

PDL::Graphics::PLplot:http://pdl.perl.org/?docs=Graphics/PLplot&title=PDL::Grap