[Graph-dev] Making a graph by passing the formula

Martin Thornquist martint at ifi.uio.no
Thu Aug 26 23:22:19 PDT 2004


[ t01331hk at sfc.keio.ac.jp ]

> I'm using the perl module "Chart::Graph::Gnuplot" and have a
> question about it. I want to make a line graph by giving the formula
> (or function), instead of giving the data set in ARRAY format. For
> example, when we use gnuplot from command lines, we could get a line
> graph by just typing "plot sin(x)". Is it possible to do it by using
> the module "Chart::Graph::Gnuplot"?

I patched Chart::Graph::Gnuplot some time ago to accept a function.
Here are patches against the version 2.0 distribution:

--- Gnuplot.pm.orig	2001-10-29 23:56:25.000000000 +0100
+++ Gnuplot.pm	2004-05-07 13:33:53.000000000 +0200
@@ -488,13 +488,12 @@
 	    $type = $value;
 	}
     }
-    
-    print $handle "$ranges \"$filename\" $using $axes $title $style";
 
-    # we give the user 3 formats for supplying the data set
+    # we give the user 4 formats for supplying the data set
     # 1) matrix
     # 2) column
     # 3) file
+    # 4) function
     # please see the online docs for a description of these 
     # formats
     if ($type eq "matrix") {
@@ -503,6 +502,9 @@
 	$result = _columns_to_file($filename, @data);
     } elsif ($type eq "file") {
 	$result = _file_to_file($filename, @data);
+    } elsif ($type eq "function") {
+	$using = "";
+	$result = 1;
     } elsif ($type eq "") {
 	carp "Need to specify data set type";
 	return 0;
@@ -510,6 +512,11 @@
 	carp "Illegal data set type: $type"; 
         return 0;
     }
+
+    print $handle "$ranges " .
+      ($type eq "function" ? "@data" : "\"$filename\"")
+	. " $using $axes $title $style";
+
     return $result;
 }
 
--- doc/graph_gnuplot.html.orig	2001-10-29 23:57:40.000000000 +0100
+++ doc/graph_gnuplot.html	2004-05-25 11:15:47.643286801 +0200
@@ -125,10 +125,11 @@
  |                | "impulses", "<x | y | xy>"  |                             |
  |                | "errorbars", etc...         |                             |
  | "axes"         | "x1y1", "x2y2", "x1y2", etc.|      "x1y1"                 |
- | "type"         | "matrix", "columns", "file" |      no default             |
+ | "type"         | "matrix", "columns", "file",|      no default             |
+ |                | "function"                  |                             |
  | "using"        | (set your own format string)|      "1:2"                  |
  +----------------+-----------------------------+-----------------------------+</PRE>
-<P>Data can be presented to <EM>Chart::Graph::Gnuplot</EM> in one of 3 formats for
+<P>Data can be presented to <EM>Chart::Graph::Gnuplot</EM> in one of four formats for
 the convenience of the user:</P>
 <PRE>
  \@matrix: an array reference of [x,y] pairs of data</PRE>
@@ -136,7 +137,7 @@
 <PRE>
  \@x_column, \@y_column: two array references of data of equal length.
  \@x_column is the x-axis data. \@y_column is the y-axis data.</PRE>
-<P>Finally, data can be stored in a file.</P>
+<P>Finally, data can be stored in a file or specified as a function.</P>
 <P>
 <H2><A NAME="using gnuplot to read and plot date/time data directly">USING GNUPLOT TO READ AND PLOT DATE/TIME DATA DIRECTLY</A></H2>
 <P><EM>Gnuplot</EM> now has the capability to read date/time data and to create



Example of usage:

use Chart::Graph::Gnuplot qw(gnuplot);

gnuplot({ title => "test",
	  "output type" => "gif",
	  "output file" => "test.gif",
	},
	[ { title => "graph",
	    type => "function" },
	  "f(x) = x**2, f(x)" ]);


Martin
-- 
"An ideal world is left as an exercise to the reader."
                                                 -Paul Graham, On Lisp


More information about the Graph-dev mailing list