Search

gnuplot: a command-line driven interactive plotting program

February 25th, 2007 edited by ana

Entry submitted by Henryk Gerlach. DPOTD needs your help, please contribute !

A picture says more than a thousand words. Sometimes you have just plain data files and want to produce some nice plot out of them. If this does not happen on a regular basis you might use oocalc, but it is not so easy to automatize this using OpenOffice.org.
That’s were gnuplot comes into play.

Here is a real world example: sar is a utility to monitor the performance of your server (it should be covered here some other day!). A typically sar output for a not so busy server looks like this (in fact the data was sexed up as the average reveals):

Linux 2.6.9-023stab039.1-smp (host.domain.tld)     02/17/07

00:05:01          CPU     %user     %nice   %system   %iowait     %idle
01:35:01          all      0.19      0.00      0.03      0.00     99.78
01:45:01          all      0.35      0.00      0.26      0.04     99.35
01:55:01          all      0.41      0.00      0.66      0.00     98.93
02:05:01          all      0.48      0.00      1.19      0.03     98.31
02:15:01          all      0.13      0.00      5.03      0.01     94.83
02:25:01          all     10.34      0.00     20.46      0.00     69.20
02:35:01          all     40.35      0.00     20.79      0.00     38.86
02:45:01          all     15.32      0.00     10.61      0.01     74.06
02:55:01          all      0.08      0.00      0.17      0.00     99.74
03:05:01          all      0.05      0.00      0.01      0.00     99.94
03:15:01          all      0.01      9.39      0.25      9.90     80.45
03:25:01          all      0.00     14.11      0.30      2.79     82.80
03:35:01          all      1.07      0.00      0.04      0.02     98.87
03:45:01          all      0.00      0.00      0.00      0.00    100.00
03:55:01          all      0.05      0.00      0.01      0.00     99.95
04:05:01          all      0.04      0.00      0.00      0.00     99.96
04:15:01          all      0.05      0.00      0.01      0.00     99.95
04:25:01          all      0.01      0.00      0.01      0.02     99.96
04:35:01          all      0.57      0.00      0.25      0.00     99.18
04:45:01          all      0.00      0.00      0.01      0.00     99.99
04:55:01          all      0.01      0.00      0.00      0.00     99.99
Average:          all      0.14      0.18      0.06      0.20     99.43

by piping the above in through tail +4 | head -n -1 > data.txt into a file we clip of the first three and the last line which would confuse gnuplot.

Let’s start an interactive session of gnuplot by typing gnuplot on the command line. Type the following:

gnuplot> set xdata time
gnuplot> set timefmt "%H:%M:%S"
gnuplot> plot [*:*][0:100] “data.txt” using 1:3 with linespoints title “user”, \\
 > “data.txt” using 1:5 with linespoints title “system”, \\
 > “data.txt” using 1:6 with linespoints title “iowait”

In line 1 we tell gnuplot to interpret the x axis as a time axis.

Line 2 defines the time format of the x-axis (the first column of “data.txt”).

Finally line 3 produces the plot. The [*:*] tells gnuplot to chose the scale on the x-axis, as it seems fit, while [0:100] scales the y-axis between 0 and 100. Then “data.txt” using 1:3 with linespoints title “user”, tells gnuplot to take the first and the third column of “data.txt” and plot it with points connected by lines.

The title “user” produces a legend for the line.

You can use the help plot command to learn more about the many options to format your graph.
If we are happy with the output, we can save it to a “png”-file by changing the terminal

gnuplot> set terminal png

and defining an output-filename:

gnuplot> set output “stats.png”

Okay, this all seems still pretty manual, but here is how we can automatically generate the png-file in some bash script, too:

#!/bin/bash
#... generate data.txt with sar, head and tail.
OUTFILE="stat.png"
gnuplot <<END
set terminal png
set output "$OUTFILE"
set xdata time
set timefmt "%H:%M:%S"
plot [*:*][0:100] “data.txt” using 1:3 with linespoints title “user”, \\
 “data.txt” using 1:5 with linespoints title “system”, \\
 “data.txt” using 1:6 with linespoints title “iowait”
END

But gnuplot can do much more! It can print functions and 3d-data. Check out the tutorials.

Target Users:

Everyone who needs to generate graphs of data or functions, especially in an automated manner.

Tutorials:

Other packages doing similar things:

  • Grace (xmgr) is possibly as old as gnuplot. It has a bigger emphasize on the GUI and a less convenient console. If you prefer to point and click grace might be better for you.
  • RLPlot(covered earlier in debaday) comes with a fresh KDE-GUI, is easy to use and might be the right thing for you if you just want to produce a few graphs. But scripting seems hard; maybe it is possible to use the DCOP-interface.
  • If gnuplot does not scratch your itch, you might even try to program something yourself using scipy (covered earlier in debaday) or the gnu plotutils (a nice example is piechart)

License:

Although it’s called gnuplot, it is not licensed under the GPL, but some homebrewn License.

You can find gnuplots’s homepage at http://www.gnuplot.info/.

gnuplot is available in both Debian and Ubuntu.

Posted in Debian, Ubuntu |

13 Responses

  1. Mike Says:

    you might try rrdtool (http://oss.oetiker.ch/rrdtool/) for these particular types of stats. i need to use gnuplot for some other stats soon though. Just a heads up in case you’re unfamiliar, either should be available in debian or ubuntu.

  2. Jordan Says:

    people might also be interested in a GTK frontend for gnuplot that I maintain in Debian called plotdrop. For basic stuff it’s rather nice. Has drag-n-drop support, etc. It’s in both Debian and Ubuntu repos.

  3. jeremiah foster Says:

    Not able to get those commands to work on either of my Ubuntu systems. I have gnuplot installed but when I run the supplied shell script it says:

    “gnuplot> plot [*:*][0:100] “data.txt” using 1:3 with linespoints title “user” “data.txt” using 1:5 with linespoints title “system” “data.txt” using 1:6 with linespoints title “iowait”
    ^
    line 0: invalid expression”

    Does one really want those commas at the end of the line?

  4. Luis Echevarria Says:

    Thanks for your article and Jordan’s post. I am using plotdrop
    now. Comes in very handy for quick
    and dirty plotting.

  5. Henryk Gerlach Says:

    Dear jeremia, the script should read:
    —–
    plot [*:*][0:100] “data.txt” using 1:3 with linespoints title “user”, \
    “data.txt” using 1:5 with linespoints title “system”, \
    “data.txt” using 1:6 with linespoints title “iowait”
    —–

    Note the final backslashes.

    Look like these were lost sometime during the processing of the article. I’ll ask to change that.

  6. ana Says:

    Entry updated with the scripts fixed.

  7. ak Says:

    I can’t believe it! sar and gnuplot is just what I need right now.

    Thank you very much.

  8. aubustou Says:

    Wonderful, i was trying to compile Grace and it kept failing.
    Shortly after, i found out there is a .deb for Grace thank to this article :D

    Thanks a lot :D

  9. Jowel Sut Says:

    I am on going to do some bench-mark on a aix platform. I can collect some performance info thanks to famous nmon and produce some graph with the ‘nmon Analyser’. But it request an transfer to a ms *dow :-/. Is somebody knows a script to produce same kind of graph with gnuplot?

    Tia,
    Jowel

  10. George White Says:

    The S-plus language can handle many things people do with gnuplot, and is
    freely available as “R”. For ad-hoc data analysis S-plus is much more powerful. It can produce publication quality plots (.pdf, .eps).

  11. thePsychologist Says:

    R is great (although technically it’s an implementation of S, not S-Plus, which is the commercial implementation of S). R command line but there are some GUIs in development including RKWard which is a KDE app and R Commander.

    However it’s worth it to learn the language of R if you have time because it is a full interpreted programming language and is thus much more versatile than relying on prebuilt functions.

  12. Jowel Sut Says:

    Fwiw, imho this ‘ploticus’ (see: http://packages.debian.org/unstable/misc/ploticus) seems to do better what I need (aix activity plot reports) and it’s gpl.

    Hth,
    Jowel

  13. Don Says:

    The plot flashes up and disappears very quickly. gnuplot -persist works from a command line but NOT a script. So, how can your script be modified to keep the plot on screen?