February 12th, 2009 mysurface
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.
Python: Generating graphs with matplotlibMatplotlib is a python 2D plotting library that can produce dots lines, histograms, power spectra, bar charts, pie chart...
Plotting SPICE DataLast post, I only intro how we can save the data and plot in text files. Actually we can plot the SPICE data in graphica...
a graphical disk usage analyzerUbuntu Feisty Fawn has improves in graphics and user friendliness, one of the tools install by default is baobab. Baobab...
Posted in Developer, gnuplot | Hits: 376819 | 23 Comments »