gnuplot: a command-line driven interactive plotting program
February 25th, 2007 edited by anaEntry 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:
- IBM Developerworks: Visualize your data with gnuplot
- gnuplot tips (not so Frequently Asked Questions)
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 Comments »