switcherswitcherswitcherswitcher

User login

Who's online

There are currently 0 users and 2 guests online.

Who's new

  • vsn3480
  • fengzhixuan
  • caseybergman
  • tomas81
  • raju_dharna

RSS Feeds

Technorati

GnuPlot: An Introductory Tutorial

If you're not sure what GnuPlot is, here's a quick introduction, courtesy of gnuplot.info:

Gnuplot is a portable command-line driven interactive data and function plotting utility for UNIX, IBM OS/2, MS Windows, DOS, Macintosh, VMS, Atari and many other platforms.

Gnuplot supports many types of plots in either 2D and 3D. It can draw using lines, points, boxes, contours, vector fields, surfaces, and various associated text. It also supports various specialized plot types.

Basically, it's a pretty useful utility for plotting data - great! Now let's get on with how you use it.

First things first. Lets find out if GnuPlot is installed on your system. If you are running windows or OS X then, unless you have installed it, it won't be there. On linux, depending upon your system, you may or may not have it - run the following at the command-line to see if you have it (as always - ignore the numbers, they are just there to indicate line numbering):

$ gnuplot

If you have GnuPlot installed you will see somthing like this:

        G N U P L O T
        Version 4.0 patchlevel 0
        last modified Thu Apr 15 14:44:22 CEST 2004
        System: Linux 2.6.16.27-0.6-smp

        Copyright (C) 1986 - 1993, 1998, 2004
        Thomas Williams, Colin Kelley and many others

        This is gnuplot version 4.0.  Please refer to the documentation
        for command syntax changes.  The old syntax will be accepted
        throughout the 4.0 series, but all save files use the new syntax.

        Type `help` to access the on-line reference manual.
        The gnuplot FAQ is available from
                http://www.gnuplot.info/faq/

        Send comments and requests for help to
                <gnuplot-info@lists.sourceforge.net>
        Send bugs, suggestions and mods to
                <gnuplot-bugs@lists.sourceforge.net>

Terminal type set to 'x11'

Windows/Mac OS X

If you are running Windows use the cygwin package located here.
If you are running Mac OS X get GnuPlot here.

Linux

If your version of linux does not come with GnuPlot then you can get it here. The alternative is to use your distributions update tool. In my case (SuSe 10.1) I can use yast or apt.

Using Yast (yet another setup tool):

  1. Open a terminal
  2. Get r00t (type su)
  3. Type yast
  4. Select Software Management - this might take a few minutes
  5. Search for gnuplot
  6. Mark gnuplot-debuginfo and gnuplot for install
  7. Exit yast (and root), then type gnuplot

Using APT:

  1. Open a terminal
  2. Get r00t (type su)
  3. Type apt-cache search gnuplot
  4. Type apt-get install gnuplot
  5. Type atp-get install gnuplot-debuginfo
  6. Exit root and type gnuplot

For other versions of linux check your packet managers guide... better yet - get your sys admin to do it for you!

GnuPlot

As you might have guessed, GnuPlot is command-line based. It can be used for plotting functions or even your data - if you are lucky enough to have some!

A Simple Function
The above plot shows a simple function f(x) = 1 / (1 + exp ( 1 * x - 10 )).

The first thing we shall do is generate some basic data and generate a simple 2D plot of some x and y coordinates.

1 10
2 20
3 35
4 40
5 50
6 45
7 40
8 35.77
9 15
10 10

Copy the above data into a text file, and save the file as "plot.txt" or whatever you wish to call it. Now start GnuPlot (by typing "gnuplot" at the command-line). You should now be in the GnuPlot terminal. To create a simple plot do the following:

gnuplot> plot 'plot.txt'

This should create an image similar to the one shown.

A simple plot of the above data
A simple plot of two dimensional data with a line.

The image you have will not have a line. It should be simple dots. When you plotted the graph you did the most basic of operations, you simply told GnuPlot to make a graph of the first two columns in the file "plot.txt". This line is identical to:

gnuplot> plot 'plot.txt' using 1:2

Where 1 is the x-axis and 2 is the y-axis. If the "plot.txt" file had another column (3) and we wanted to plot column 1 and 3 the statement should read:

gnuplot> plot 'plot.txt' using 1:3

Now that is very simple. Create some more random data and have a play!

Now using your dataset, or the one I provided type:

gnuplot> plot 'plot.txt' using 1:2 with

you should get something identical to this:

gnuplot> plot 'plot.txt' with
                                   ^
         expecting 'lines', 'points', 'linespoints', 'dots', 'impulses',
        'yerrorbars', 'xerrorbars', 'xyerrorbars', 'steps', 'fsteps',
        'histeps', 'filledcurves', 'boxes', 'boxerrorbars', 'boxxyerrorbars',
        'vectors', 'financebars', 'candlesticks', 'errorlines', 'xerrorlines',
        'yerrorlines', 'xyerrorlines', 'pm3d'

In my plot above you can clearly see a line running between the data. This is achieved using the with "lines" command (in the above block). The type of statement is going to depend upon the type of data you want to plot, in my case mostly scatter plots, if you want to experiment with these styles have a go. If you just want to see the options at your disposal type:

gnuplot> help [function] # where [function] is any of the above options
gnuplot> help lines
                The `lines` style connects adjacent points with straight line segments.
                See also `linetype`, `linewidth`, and `linestyle`.

So you now have a graph. The thing is the graph has a key in the top right corner, no labels, no title and you have no idea how to get it onto a sheet of paper...

The Key

I like the key. I know the name of the file I have used, the dimensions and if there are several things plotted, I know which line belongs to which data. We are now presented with several choices...

  • Leave the graph key alone.
  • Move the graph key off the graph.
  • Get rid of that thing now.
  1. Is simple - move to the next section.
  2. This is solved very simply:

    gnuplot> plot 'plot.txt' using 1:2 with lines # plot the graph
    gnuplot> set key outside # tell gnuplot to move the key to the right of the graph
    gnuplot> replot # like java you have to tell gnuplot to redraw the screen image
  3. This is even simpler:

    gnuplot> unset key
    gnuplot> replot
Copyright © 2007. All rights reserved. The contents, examples, and images contained within this post are owned and copyrighted by Daniel Klose. Any information contained within this page may not be used in any other publication (web, print or otherwise) without the express permission of the author first.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Submitted by perlmunky on January 25, 2007 - 1:05pm.

One thing I meant to do in the first introduction was how to plot a function. I find this useful as I lack the ability to visualise a graph in my head (heap allocation error).

Like the other features I showed, plotting a function is not a complex task. Simpy given our function we type it into GnuPlot like we would in say perl:

gnuplot> plot 1 / (1 + exp ( 1 * ( x - 10 ) ) )

Where x is our variable. Also plot the following functions for a laugh:

7 + log10(x)
7 - log10(x)

enjoy.

Submitted by j1n3l0 on July 10, 2007 - 10:32pm.

Hi,

Just checking here. Do you have any idea how to work out the values of the margins? i.e If you run the "show margin" command you get:

gnuplot> show margin

        lmargin is computed automatically
        bmargin is computed automatically
        rmargin is computed automatically
        tmargin is computed automatically

I would like to find out what these values actually are ... preferably in pixels! I need this for an attempt at creating an image map of the plots. I have checked the mailing-lists, currently I am awaiting a response, I just thought I'd ask here as well.

Any ideas?

Nelo

Smoothie, smoothie, hundre prosent naturlig!

Submitted by perlmunky on July 11, 2007 - 9:18am.

I am not sure I understand what it is you are trying to do.

I had a look at making changes to the lmargin and rmargin values. It looks like you have a 1/2 cm as the margin. The default values appear to be (~5) ... I am not sure what this is (actual units). When you find out, can you post back here please.

Thanks.

PM

/(bb|[^b]{2})/

Submitted by j1n3l0 on July 19, 2007 - 4:50pm.

Ok,

I did some digging ... well, measuring ... and found out a few things:

1: The ratio of the length of the {l|r}margin to the Gnuplot value (i.e. the value specified by set command) is 7.
2: The same ratio for the {t|b}margin is 11.
3: The length of the margins is independent on the size of your plot.
4: The default plot dimensions are 640 by 480 pixels.

With this knowledge it is possible to work out the screen coordinates from graph coordinates (values) like so:

screen coordinates [x, y] = [ (lmargin + (abs(xmin - x)/xrange * xlength)), (tmargin + (abs(ymax - y)/yrange * ylength)) ]

where:
   
    lmargin is the length in pixels of the left margin

        lmargin = 7 * gnuplot lmargin
   
    xrange is the difference between the max and min x values
   
        xrange = abs(xmax - xmin)
   
    xlength is the length of the x axis in pixels
   
        xlength = image_width - (lmargin + rmargin)
   
    tmargin is the length in pixels of the top margin
   
        tmargin = 11 * gnuplot tmargin
   
    yrange is the difference between the max and min y values
   
        yrange = abs(ymax - ymin)
   
    ylength is the length of the y axis in pixels
   
        ylength = image_height - (tmargin + bmargin)

 

To get anything useful out of this I suggest you set these values yourself. I find the most useful, that allow you to get your axis labels and titles in are:

set lmargin 10
set rmargin 5
set tmargin 5
set bmargin 5
 

From this it is possible to write a script to do this automatically for you from any set of x and y values :)

PLEASE NOTE
> This was done using Gnuplot version 4.0. I am not aware if any changes have been made to this mechanism in version 4.2 or if there is now a means to retrieve the value of the margins in pixels that are generated automatically.
> Also this holds true when the terminal is set to png and default fonts are used. For our purposes, this is adequate. I will investigate if there is a difference using other terminals and/or fonts posting any findings back here.

I someone finds this useful.

Nelo

Smoothie, smoothie, hundre prosent naturlig!