1. Perl
  2. Module
  3. PDL

Creating a graph with PDL

I will explain how to create a graph using PDL.

Install PDL::Graphics::PLplot

Install the module PDL::Graphics::PLplot to draw the graph. (You need to PDL installed first.)

Windows

To install in ppm, execute the following command.

ppm install http://www.sisyphusion.tk/ppm/PDL-Graphics-PLplot.ppd - force

Press Enter to complete the installation. Since it is necessary to set the environment variable, set the displayed environment variable PLPLOT_LIB. For example, the following is displayed.

  In order to utilise the built in plplot capability set your
  PLPLOT_LIB environment variable to C:\Perl\site\lib\PDL\plplot_supp

Versions that support png

It is convenient if png output is supported, so if you want to install a version that supports png, run the following command.

ppm install http://www.sisyphusion.tk/ppm_alt/PDL-Graphics-PLplot_alt.ppd - force

Press Enter to complete the installation. Since it is necessary to set the environment variable, set the displayed environment variable PLPLOT_LIB and PLPLOT_DRV_DIR. For example, the following is displayed.

Running PDL-Graphics-PLplot-0.62 install script ...
  In order to utilise the built in plplot capability set your

  PLPLOT_LIB environment variable to:
   C:\Perl64\site\lib\PDL\plplot_supp

  And set your PLPLOT_DRV_DIR environment variable to:
   C:\Perl64\site\bin

Creating a simple graph

Let's draw a graph in PDL. Use the module PDL::Graphics::PLplot to draw the graph.

Linear function graph

First, let's draw a graph of the simplest linear function "y = 2x". Use the xyplot method to create a xy graph.

use PDL;
use PDL::Graphics::PLplot;

# Create PLPlot object (output as graph.png in PNG format)
my $pl = PDL::Graphics::PLplot->new(DEV =>'png', FILE =>'graph.png');

# Create a PDL variable that represents 0 to 99 as the value of x
my $x = pdl->sequence(100);

# y = 2x
my $y = 2 * $x;

# Create a graph
$pl->xyplot($x, $y);

# Close
$pl->close;

You can create a graph of the linear function as follows.

[f: id: perlcodeexample: 20131106082618p: image: w500]

new options

Here are some graph options. It can be passed as an argument to the new method.

my $pl = PDL::Graphics::PLplot->new(
  DEV =>'png',
  FILE =>'graph.png',
  PAGESIZE => [500, 400]
);

Graph size

You can specify the size of the graph to be output with PAGESIZE .

PAGESIZE => [500, 400]

Options for drawing graphs

This option is specified when drawing the graph.

$pl->xyplot($x, $y, XLAB =>'x', YLAB =>'y');

x-axis label

You can specify the x-axis label in XLAB .

XLAB =>'x'

y-axis label

You can specify the y-axis label in YLAB .

YLAB =>'y'

Drawing area

You can specify the drawing area with the BOX option.

BOX => [start of x, end of x, start of y, end of y]

Specify as follows.

BOX => [0, 150, 0, 300]

x-axis scale label width

You can specify the width of the x-axis tick label with XTICK .

XTICK => 10

y-axis scale label width

You can specify the width of the y-axis tick label with YTICK .

YTICK => 10

Title

You can specify the title of the graph with TITLE .

TITLE =>'y = 2x'

Viewport

You can specify where to actually draw the graph with VIEWPORT . Specify in a ratio of 0 to 1.

VIEWPORT => [Minimum value position of x, Maximum value position of x, Minimum value position of y, Maximum value position of y]

Specify as follows.

VIEWPORT => [0.1, 0.5, 0.1, 0.5]

Draw x-axis

Specify bcnsta for XBOX to draw the x-axis. This value defaults to bcnst and you can add a to draw the x-axis.

XBOX =>'bcnsta',

Draw x-axis

Specify bcnsta for YBOX to draw the y-axis. This value defaults to bcnst and you can add a to draw the y-axis.

YBOX =>'bcnsta',

Graph color

You can specify the color of the graph with COLOR .

COLOR =>'RED'

Below are the types of colors.

  BLACK GREEN WHEAT BLUE
  RED AQUAMARINE GRAY BLUE VIOLET
  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

Example

I wrote a graph of a linear function "y = 1/2 x + 10".

[f: id: perlcodeexample: 20131106082619p: image: w500]

use PDL;
use PDL::Graphics::PLplot;

# Create PLPlot object (output as graph.png in PNG format)
my $pl = PDL::Graphics::PLplot->new(
  DEV =>'png',
  FILE =>'graph.png',
  PAGESIZE => [600, 300],
);

# Create a PDL variable that represents 0 to 99 as the value of x
my $x = pdl->sequence(100)- 50;

# y = 2x
my $y = (1/2) * $x + 10;

# Create a graph
$pl->xyplot(
  $x,
  $y,
  XLAB =>'x',
  YLAB =>'y',
  TITLE =>'y = 1/2 x + 10',
  XBOX =>'bcnsta',
  YBOX =>'bcnsta',
  COLOR =>'RED',
  JUST => 1
);

# Close
$pl->close;

Graph of n - th order function

Now let's draw a cubic function. It's interesting to know immediately what kind of shape it is.

[f: id: perlcodeexample: 20131106102521p: image: w500]

use strict;
use warnings;

use PDL;
use PDL::Graphics::PLplot;

# Create PLPlot object
my $pl = PDL::Graphics::PLplot->new(DEV =>'png', FILE =>'graph.png');

# x value (0 to 4 in 0.1 increments)
my $x = pdl->sequence(40) * 0.1;

# y = x ^ 3 - 6x ^ 2 + 11x -6
my $y = ($x ** 3) -6 * ($x ** 2) + (11 * $x) -6;

# Create a graph
$pl->xyplot($x, $y);

# Close
$pl->close;

Trigonometric function

The following is a graph of trigonometric functions (sin). Use the sin method to calculate trigonometric functions. It is defined in PDL::Math.

[f: id: perlcodeexample: 20131106102522p: image: w500]

use strict;
use warnings;

use PDL;
use PDL::Graphics::PLplot;

# Create PLPlot object
my $pl = PDL::Graphics::PLplot->new(DEV =>'png', FILE =>'graph.png');

# x value
my $x = pdl->sequence(200) * 0.1;

# y = sin(x)
my $y = $x->sin;

# Create a graph
$pl->xyplot($x, $y);

# Close
$pl->close;

Exponential function

Next is the graph of the exponential function. Note that the value e to be calculated is created as a PDL variable.

[f: id: perlcodeexample: 20131106102523p: image: w500]

use strict;
use warnings;

use PDL;
use PDL::Graphics::PLplot;

# Create PLPlot object
my $pl = PDL::Graphics::PLplot->new(DEV =>'png', FILE =>'graph.png');

# e
my $e = pdl [exp(1)];

# x value
my $x = pdl->sequence(60) * 0.1 - 3;

# y = e ^ x
my $y = $e ** $x;

# Create a graph
$pl->xyplot($x, $y);

# Close
$pl->close;

Logarithmic function (natural logarithm)

The last is a graph of logarithmic function (natural logarithm). Use the log method to calculate the logarithmic function (natural logarithm). It is defined in PDL::Math. Note that the graph starts at 0.1, as the value of log0 will be infinitesimal.

[f: id: perlcodeexample: 20131106102524p: image: w500]

use strict;
use warnings;

use PDL;
use PDL::Graphics::PLplot;

# Create PLPlot object
my $pl = PDL::Graphics::PLplot->new(DEV =>'png', FILE =>'graph.png');

# x value
my $x = pdl->sequence(100) * 0.1 + 0.1;

# y = log(x)
my $y = $x->log;

# Create a graph
$pl->xyplot($x, $y);

# Close
$pl->close;

Strip plot - graph of one x - axis and multiple y - axis

One x-axis and multiple y-axis graphs are called strip plots and can be drawn using the stripplots method.

[f: id: perlcodeexample: 20131107113838p: image: w500]

use strict;
use warnings;

use PDL;
use PDL::Graphics::PLplot;

# Create PLPlot object
my $pl = PDL::Graphics::PLplot->new(DEV =>'png', FILE =>'graph.png');

# data
my $x = pdl->sequence(20);
my $pi = 4 * atan2 (1,1);
my $y1 = $x ** 2;
my $y2 = sqrt $x;
my $y3 = $x ** 3;
my $y4 = sin(($x/20) * 2 * $pi);
my $ys = $y1->cat($y2, $y3, $y4);

# Strip plot
$pl->stripplots(
  $x,
  $ys,
  PLOTTYPE =>'LINE',
  TITLE =>'functions',
  YLAB => ['x ** 2', 'sqrt(x)', 'x ** 3', 'sin(x/20 * 2pi)'],
  COLOR => ['GREEN', 'DEEPSKYBLUE', 'DARKORCHID1', 'DEEPPINK'],
  XLAB =>'X label'
);
$pl->close;

Related Informatrion