Plot your graphs with command line gnuplot

February 12th, 2009 mysurface Posted in Developer, gnuplot | Hits: 235803 | 14 Comments »

gnuplot is a command line driven graph plotter tools for us to generate graphs. The common graphs that we are looking forward to present the resource performance per seconds, hours, days, weeks or months are usually plot graphs, which it consist of lines and dots. gnuplot allows us to read the data from text files which contains values in tabular format.

For example, I have a module that extract raw values into logs based on the specified interval. The result will looks like this (test.dat):


##date time slot_1  slot_2  slot_3  slot_4  slot_5  slot_6  slot_7  slot_8  slot_9  slot_10
06/02/2009 09:16:49 16  6  15  5  14  4  17  3  14  4
06/02/2009 09:16:54 16  8  13  3  15  4  17  4  15  5
06/02/2009 09:16:59 18  8  11  5  15  6  14  3  16  5
06/02/2009 09:17:04 19  6  12  5  18  7  15  5  18  5
06/02/2009 09:17:09 19  7  14  6  16  6  16  6  18  4
06/02/2009 09:17:14 21  7  17  5  19  7  16  4  20  5
06/02/2009 09:17:19 20  5  19  6  17  5  15  3  17  3
06/02/2009 09:17:24 20  6  21  5  18  4  14  4  18  4
06/02/2009 09:17:29 17  5  19  4  16  4  15  4  17  2

gnuplot allows us to create a template describe how our graph outlook. To generate the graph, we just have to execute the template, just like executing bash script.

This is a sample of a gnuplot template that I use to construct a plot graph, (test1.pg)


#!/usr/bin/gnuplot
reset
set terminal png

set xdata time
set timefmt "%d/%m/%Y %H:%M:%S"
set format x "%H:%M"
set xlabel "time"

set ylabel "total actives"
set yrange [0:31]

set title "M7YC Performance per time"
set key reverse Left outside
set grid

set style data linespoints

plot "test.dat" using 1:3 title "slot 1", \
"" using 1:4 title "slot 2", \
"" using 1:5 title "slot 3", \
"" using 1:6 title "slot 4", \
"" using 1:7 title "slot 5", \
"" using 1:8 title "slot 6", \
"" using 1:9 title "slot 7", \
"" using 1:10 title "slot 8", \
"" using 1:11 title "slot 9", \
"" using 1:12 title "slot 10"
#

To allow the template to be executable, the first line of the template must add the SHABANG ‘#!/usr/bin/gnuplot’, similar to bash script. And also remember to perform chmod to your template.

chmod +x test1.pg

I would like to generate graph that shows the performance value over time, therefore I have to read datetime into my X axis and performance value into my Y axis. First two columns of my data file are datetime, I need to set the xdata as time, the define the time format in the raw data files and at last define the x format to appear in my graph.


set xdata time
set timefmt "%d/%m/%Y %H:%M:%S"
set format x "%H:%M"

I would like to display my keys outside the graphs as well as enable the grid to ease the reading.


set key reverse Left outside
set grid

The plot graph I intended to generate consist of lines and dots.

set style data linespoints

The important part is how to define the source of my raw data, how many items I wanted to plot into the graph, as well as giving each item a title. The plot format may vary based on different graph format, but for my case it is


plot [raw_data] using [x value's column in data file]:[y value's column in data files] title [item's name], ...

The plot portion can be shorten into this:


plot "test.dat" u 1:3 t "slot 1", \
"" u 1:4 t "slot 2", \
"" u 1:5 t "slot 3", \
"" u 1:6 t "slot 4", \
"" u 1:7 t "slot 5", \
"" u 1:8 t "slot 6", \
"" u 1:9 t "slot 7", \
"" u 1:10 t "slot 8", \
"" u 1:11 t "slot 9", \
"" u 1:12 t "slot 10"

Execute the template and redirect the binary stream to construct a png file.

./test1.pg > test1.png

test1.png

gnuplot is far more powerful than what I have illustrate here, you may want to checkout the official website for more info.

P.S. I am using gnuplot v4.2 while writing this.

14 Responses to “Plot your graphs with command line gnuplot”

  1. This goes into my del.icio.us collection – I don’t want it now – but I am sure it will come in handy soon. Last time I had to create a graph, I had to use Open Office.

  2. Thank you very much for sharing!
    It’s always easier to do something if you have an example.
    Thank you.

  3. that look cool. have you tried generating the plot from RRD?

  4. Sweet, thanks mate. I need something like this at my work for our weekly votes on the “Chips” at the cafeteria :)

  5. This has solved my problem , this is what
    i really want.

  6. Hey there just wanted to give you a quick heads up and let you know a few of the pictures aren’t loading properly. I’m not sure why but I think its a linking issue. I’ve tried it in two different browsers and both show the same results.

  7. I’ve bookmarked, Dugg, and I joined the RSS subscription. Thanks! .

  8. Hi,
    How can we change this script that parameters are taken from command line as in the shell script with using $1,$2

    ex:
    ./test1.pg filename > filename.png

    thank you for your considerations

  9. HacktheHacker Says:

    thank you!! it was very helpful!! :)

  10. Abhijit Mondal Says:

    It is very useful. I have already bookmarked it.

  11. Amazing …really very helpful to beginners like me.

  12. how to display graph in ns2.34.what is the command for that??

  13. The measurement of this product or service is extremely exceptional.
    This evidently implies that link speeds are insufficient with this protocol.

  14. Chandrashekhar Says:

    Most use full information

Leave a Reply