This is a read-only archive. Find the latest Linux articles, documentation, and answers at the new!

Feature: Tools & Utilities

Gplot simplifies gnuplot graph creation

By Ben Martin on February 27, 2008 (4:00:00 PM)

Share    Print    Comments   

Gnuplot can generate sophisticated graphs and output them in vector or bitmap image formats. It can produce many graph types, and you can customize the way the output will look to a great extent. But the customizability of gnuplot can work against it when all you want a simple line graph comparing two series of data points. In those cases, gplot lets you use gnuplot to create simple graphs using more semantic options to customize the appearance of common graph objects.

Gplot's main dependencies, gnuplot and Perl, should be packaged by your distribution. As gplot is a Perl script, installation simply involves copying the file from the distribution tarball into your path to a location such as /usr/local/bin.

You need one or more sets of input data in order to generate a graph image. By default each graph line comes from its own input file. The data in each input file is in the form of a single coordinate per line in the format "X Y," where the ordinates are separated by white space. If your input only contains the Y values and you are happy to have the X ordinate start at zero and increment by one for each value, then you can use the -onecolumn option to let gplot accept a file containing only Y ordinates. If you have a matrix of data values stored in a single data file, then the -using X:Y option allows you to pick out which columns in the matrix define the X and Y ordinates for the data you wish to plot.

The below commands take an input data series and create a graph (shown below) in JPEG format representing this series. I have broken the command line after the title because the next two options (name and onecolumn) relate to the data file Options which relate to a particular data file will use the data from the next data file path given on the command line. For example, if there were a second data file, I could repeat the name and onecolumn options after but before citing the second data file's path to create two lines in the output graph, one for each data file.

$ cat 1 2 3 4 2 3 2 3 $ -type jpg -title "Fun Index" \ -name "kitten index" -onecolumn Converting '' into two columns of data Created GUPLOT output in '/tmp/gplot.jpg'

You can change the bland X and Y axis labels using the -xlabel foo and -ylabel bar command-line options respectively. If you use the X axis to show time data -- for example for Web traffic statistics -- then you can use the -dateformat option to tell gplot what time format the X axis time data is in. For example, %d/%m/%y will parse slash-separated day/month/year information as X ordinates. For other time parsing field descriptors, see strptime(3).

Since gplot uses gnuplot to create graphs, you might wish to get access to the data that is fed to gnuplot. The -showgnuplot gplot option will show you this intermediate gnuplot file on stdout before proceeding to process it.

If you wish to customize the gnuplot output that gplot generates, you might consider changing the input template gnuplot file that gplot uses and passing -plotcmds to gplot to start with your customized gnuplot file. The template gnuplot file allows for very simple substations and conditionals. The default template file is embedded in the Perl script itself on line 104 where GNUCMDS is defined.

Values in the template gnuplot file that are contained between % characters are substituted with their values when gplot is executed. The below example places the value of the -xlabel command-line parameter where %XLABEL% appears in the template.

set xlabel "%XLABEL%" font "%FONT%,%FONTSIZE%"

One might hope that any command-line parameters, or those starting with a given prefix, could be substituted in this manner. Gplot will generate an error if you attempt to pass a new command-line option for template substitution like this. Adding options for use in template substitution is not difficult though. The script needs two slight modifications: change my %opts on line 131 to include the new options, and Getopt::Long::GetOptions on line 155 to include each new option and its type:

my %opts = (type => 'xwin', outfile => '/tmp/gplot.%TYPE%', ... style => 'linespoints', foo => 'defaultfoo', ); ... Getopt::Long::GetOptions( \%opts, 'help', 'showgnuplot', ...'font=s', 'fontsize=i',.... 'verbose', 'foo=s', @plotoptions ) || die "Failed to parse options\n";

With the addition of the foo command-line option, the template can include %FOO% wherever you wish:

set title "%TITLE% %FOO%" font "%TFONT%,%TFONTSIZE%"

The ability to customize gnuplot template files with gplot gives you the ability to produce some fairly advanced graphs using templates to specialize how things appear while retaining the simple interface of gplot.

The following example uses three different data series which are per-month statistics. Since I'm using time-related data I can take advantage of the -dateformat option to parse human-readable month names and numeric years. One downside to using -dateformat is that the axis ticks are not optimal in the default configuration. To get around that I created my own template gplot file that labels the X axis ticks in a more intuitive format. The main things to note in my-template.gplot are the last few lines, which set the minor X ticks to off and define how labeling will proceed for the X major ticks.

$ cat my-template.gplot # GPLOT.PL pseudo commands for GNUPLOT IF TYPE eq ps set terminal postscript landscape color IF TYPE eq eps set terminal postscript eps color IF TYPE eq jpg set terminal jpeg IF TYPE eq png set terminal png IF TYPE eq pbm set terminal pbm IF TYPE ne xwin set output "%OUTFILE%" set xlabel "%XLABEL%" font "%FONT%,%FONTSIZE%" set ylabel "%YLABEL%" font "%FONT%,%FONTSIZE%" set multiplot set autoscale set data style lines set border 3 set xtics border nomirror set ytics border nomirror set origin 0.0,0.0 set title "%TITLE%" font "%TFONT%,%TFONTSIZE%" set xdata time set timefmt "%b/%Y" set format x "%b/%Y" set style line 10 lt 1 lw 1 pt 5 ps 0.65 set style line 11 lt 3 lw 1 pt 1 ps 0.65 set style line 12 lt 7 lw 1 pt 10 ps 0.65 unset mxtics set xtics ("jan/2007", "feb/2007", "mar/2007", "apr/2007" ) $ cat 1 jan/2007 1200 feb/2007 1542 mar/2007 4343 apr/2007 2424 $ -plotcmds my-template.gplot -showgnuplot -dateformat '%b/%Y' \ -title "Web Hits" -ylabel Hits -xlabel Month -type jpg \ -name 1 \ -name 2 \ -name 3

Gplot can also generate vector format output using -type eps. If you use pdflatex to generate academic papers, you can use epstopdf to include gplot graphs.

At times you can run up against the boundaries of gplot and have to write gnuplot directives in a gplot template file to achieve the desired result, but the ability to substitute variables in the gnuplot template file should allow you to handle many graphing needs using gplot. Gplot makes creating simple graphs a simple task, and still lets you tap into anything that gnuplot can do if the need arises.

Ben Martin has been working on filesystems for more than 10 years. He completed his Ph.D. and now offers consulting services focused on libferris, filesystems, and search solutions.

Share    Print    Comments   


on Gplot simplifies gnuplot graph creation

Note: Comments are owned by the poster. We are not responsible for their content.

Gplot simplifies gnuplot graph creation

Posted by: Anonymous [ip:] on February 27, 2008 05:20 PM
As a gnuplot wrapper I'm using 'muplot' to plot multiple data-files at a time. It needs no configuration and has s very simple command line interface. It can produce outputs to PS and PNG formats (


I think plotdrop is a better solution

Posted by: Anonymous [ip:] on February 27, 2008 06:31 PM
Plotdrop is a Gnuplot front-end that allows you to take a text file, and drop it into plotdrop to generate a simple line graph. I find it perfect for a quick check of data. I then go into Gnuplot later to produce presentation-quality graphs. It seems to be the best combo for me.



Posted by: Anonymous [ip:] on February 28, 2008 02:04 AM
I use Maxima for 3D graphs as it uses gnuplot as a backend for rendering them. Otherwise, .gpl files are not that difficult to construct.


Gplot simplifies gnuplot graph creation

Posted by: Anonymous [ip:] on February 28, 2008 07:25 AM
This could be the tool I've long been looking for! Thanks!!


Gplot simplifies gnuplot graph creation

Posted by: Anonymous [ip:] on February 28, 2008 08:01 AM
GNU/Octave is worth looking at. It uses GNUplot but simplifies the whole process by having functions like; plot(x,y); or title('Graph title'); etc. plus its syntax compatible with MATLAB and a great programing language.


Gplot simplifies gnuplot graph creation

Posted by: Anonymous [ip:] on February 29, 2008 07:38 AM
I found the graphics capability of the statistics package R to be very useful as well. Many features can be customized, multigraph layouts rendered, and complex 3d perspectives provided. Fully scriptable. Worth considering.


Gplot simplifies gnuplot graph creation

Posted by: Anonymous [ip:] on March 04, 2008 06:38 PM
We used a package called gplot at Cornell in physics courses that sounds similar to the package described here but I don't quite remember all the details. I could have sworn it was written by someone from the faculty and I'm pretty sure it never had any Perl dependencies so perhaps it's just a coincidence in naming.


Announcing: A Book on Gnuplot!

Posted by: Anonymous [ip:] on March 20, 2008 02:20 AM
For anybody who is interested in data analysis and visualization using open-source tools, there is now a book on Gnuplot: "Gnuplot in Action". You can pre-order it directly from the publisher: <a href=''>Manning: Gnuplot in Action</a>.

The book assumes no previous familiarity with Gnuplot. It introduces the most basic concepts rapidly, and then moves on to explain Gnuplot's advanced concepts and power features in detail.

If you want to learn more about the book and the author, check out my book page at <a href="">Principal Value - Gnuplot in Action</a>.


This story has been archived. Comments can no longer be posted.

Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya