Real-time plots in 20 lines

The Plot Twists

© Lead Image © Volodymyr Horbovyy, 123rf.com

© Lead Image © Volodymyr Horbovyy, 123rf.com

Author(s):

Use Gnuplot with command-line utilities.

Some excellent charting and plotting packages can be found, but if you're like me, you sometimes just want to do a quick dynamic test plot without a lot of custom setup. Gnuplot is a command-line charting utility that has been around for a while, and I was amazed how easy it was to get up and running. In only 20 lines of scripting code, I was able to create real-time line and bar charts.

In this article, I introduce Gnuplot with two dynamic examples: The first shows the status of Raspberry Pi I/O pins, and the second is a line chart of CPU diagnostics.

Getting Started

Gnuplot [1] can be installed on Linux, Windows, and macOS. To install Gnuplot on Ubuntu, enter:

sudo apt-get install gnuplot

Gnuplot is typically run as a command-line utility, but it can also be run manually, with the charting instructions and data values inserted inline. To plot four sets of data points in a line chart, you could enter:

$ gnuplot
gnuplot> $Mydata << EOD
# Now enter some data
2 1
3 1.5
4 2.1
5 3.3
EOD
gnuplot> plot $Mydata with line

Data block names must begin with a $ character, which distinguishes them from other types of persistent variables. The end-of-data delimiter (EOD here) can be any sequence of characters. For this example, the plot command creates a line chart from the $Mydata variable (Figure 1).

Figure 1: A line chart plotted with inline data.

Static Bar Chart

For a simple Gnuplot bar chart, you could plot the real-time status of Raspberry Pi general purpose input/output (GPIO) pins. A static bar chart presentation can be created with a data file (called gpio.dat here):

# gpio.dat - data file for GPIO pin values
# column1 = chart position, column2 = heading, column3 = value
0 GPIO2 0
1 GPIO3 1
2 GPIO4 1
# ...

To plot a bar chart (Figure 2), the fill style and bar width need to be defined. The using 1:3:xtic(2) argument, shown in the next code block, configures the first column in the data file as the x position, the third column as the y value, and the second column as the x-axis labels. Use the interactive commands

$ gnuplot
gnuplot> set style fill solid
gnuplot> set boxwidth 0.5
gnuplot> plot "gpio.dat" using 1:3:xtic(2)with boxes title ""

to plot the file.

Figure 2: A simple bar chart of Raspberry Pi GPIO pins.

Real-Time Bar Chart

The previous example used a manually created gpio.dat data file. The current status of GPIO pins can be found with the gpio command-line utility [2]. For example, to get the status of GPIO pin 9, enter:

gpio read 9

By adding some Bash and an Awk script, you can create a gpio.dat file:

$ gpio read 9
1
$ gpio read 9 | awk '{ print "9 GPIO9 " $1 }'
9 GPIO9 1
$ gpio read 9 | awk '{ print "9 GPIO9 " $1 }' >gpio.dat
$ cat gpio.dat
9 GPIO9 1

To make a dynamic bar chart, create the gpio_bars.txt Gnuplot script shown in Listing 1. The Gnuplot scripting language is quite powerful and supports a wide range of functions and control statements.

Listing 1

Dynamic Bar Chart

01 # Create a dynamic bar chart that reads GPIO pins every 5 seconds
02 #
03 set title "PI GPIO Data"
04 set boxwidth 0.5
05 set style fill solid
06
07 # Create a dummy file to get started without errors
08 system "echo '0 GPIO2 1' > gpio.dat"
09
10 plot "gpio.dat" using 1:3:xtic(2) with boxes title ""
11
12 while (1) {  # make a new 'gpio.dat' every cycle with fresh data
13   system "echo '' > gpio.dat"
14   do for [i=2:29] {
15     j = i-2 # put first GPIO pin at position 0
16     system "gpio read " .i.  "  | awk '{ print  \"" . j . " GPIO" . i . " \" $1 }' >> gpio.dat
17   }
18   replot
19   pause 5
20 }

Rather than manually adding lines for each GPIO pin status, a for loop can iterate from pins 2 to 29 (lines 14-17). A system command runs the GPIO utility and Bash commands (line 16). To refresh the data, use the replot and pause commands (lines 18 and 19), and enter

gnuplot -persist gpio_bars.txt

to run the script (Figure 3).

Figure 3: Dynamic status of Rasp Pi GPIO pins.

Simple Line Chart

A line chart presentation can be created from a data file (GPU.dat), as well:

# GPU.dat - a time stamp with two data points
18:48:30 51.0 49.0
18:48:40 50.5 49.5
18:48:45 51.5 49.0
18:48:50 50.0 50.5
18:48:55 50.5 49.5

The interactive Gnuplot commands to show a line chart of this data are shown in Listing 2. This Gnuplot script requires a few extra lines: The plot needs to know that the x-axis is time data, and it needs to know the format of the time data and the x labels.

Listing 2

Line Chart from a File

$ gnuplot
gnuplot> # plot 2 variables in the file GPU.dat
gnuplot> #
gnuplot> set xdata time
gnuplot> set timefmt "%H:%M:%S"
gnuplot> set format x "%H:%M:%S"
gnuplot> plot "GPU.dat" using 1:2 with line title "GPU temp" , "GPU.dat" using
    1:3:3 with line title "CPU temp"

Multiple data points can be plotted at the same time (Figure 4). The using argument tells Gnuplot how to reference the <x>:<y> columns in the data file. (If the data file had a third column of data points, the using reference to get the last column of data would be 1:4:4).

Figure 4: A simple line chart.

Real-Time Line Chart

Linux has a lot of useful command-line troubleshooting tools, such as iostat, vmstat, and top, to name just a few. For the line chart example, I use the sensors utility [3] to get the fan speed and CPU temperature of my Linux server. The sensors command returns a number of lines of information.

$ sensors
dell_smm-virtual-0
Adapter: Virtual device
Processor Fan: 2676 RPM
CPU: +47.0~Z∞C
Ambient: +38.0∞C
SODIMM: +37.0∞C
...

With some Bash and Awk commands, you can get just the fan speed and CPU temperature (Listing 3).

Listing 3

Parsing Data

$ sensors | grep RPM
Processor Fan: 2685 RPM
$ sensors | grep RPM | awk '{print $3}'
2685
$ sensors | grep CPU
CPU: +48.0∞C
$ sensors | grep CPU | awk '{print $2}'
+48.0∞C
$ sensors | grep CPU | awk '{print substr($2,2,4)}'
48.0

Awk supports a systime() call to return the present date/time, and a strftime() call to customize the presentation. (Note: You might have to install gawksudo apt-get install gawk – on the Raspberry Pi to get this added functionality.)

Once the measurements have been parsed, the next step is to format the sensor output with a timestamp:

$ sensors | grep RPM |awk '{print strftime("%H:%M:%S ",systime()) $3}'
10:26:46 2685
$sensors | grep CPU |awk '{print strftime(\"%H:%M:%S \",systime()) substr($2,2,4)}'
10:27:46 48.0

After a time and value string have been generated, you can create a Gnuplot script, line_fan_cpu.txt, to show real-time data (Listing 4). To make the script a little easier, I create two data files, fan.dat and cpu.dat.

Listing 4

Dynamic Line Chart Script

01 # Create a Plot or User and System CPU Usage, update every 5 seconds
02 #
03 set title "GnuPlot - Fan Speed  and CPU Temperature"
04 set yrange [2650:2700]
05 set ylabel "Fan Speed"
06 set y2range [43:49]
07 set y2label "CPU Temp (C)"
08 set y2tics
09 set xdata time
10 set timefmt "%H:%M:%S"
11 set format x "%H:%M:%S"
12
13 system "sensors | grep RPM | awk '{print strftime(\"%H:%M:%S \", systime()) $3}' > fan.dat"
14 system "sensors | grep CPU | awk '{print strftime(\"%H:%M:%S \",systime()) substr($2,2,4)}'  > cpu.dat"
15
16 plot "fan.dat" using 1:2  with lines axes x1y1 title "fan speed (RPM)",  "cpu.dat" using 1:2 with lines axes x1y2 title "CPU Temp (C)"
17 while (1) {
18   pause 5
19   system "sensors | grep RPM | awk '{print strftime(\"%H:%M:%S \", systime()) $3}' >> fan.dat"
20   system "sensors | grep CPU | awk '{print strftime(\"%H:%M:%S \", systime()) substr($2,2,4)}'  >> cpu.dat"
21   replot
22 }

The plot accounts for different scale ranges with y2range and y2label definitions. The final addition is to include an axis (x1y2 or x1y2) to each plot point that lines up the data values to the right or left y-axis.

The complete Gnuplot script to show fan speed and CPU temperature is only 20 lines of code! The command

$ gnuplot -persist line_fan_cpu.txt

runs this script (Figure 5).

Figure 5: Gnuplot real-time sensor data.

Final Comments

I won't give up using plotting packages like Matplotlib or ggplot, but I was very impressed with how easy it was to create real-time plots with Gnuplot.

Manipulating the Bash/Awk script can be a little complex, but it's incredibly useful to be able to use output from almost any command-line utility in Gnuplot.

Gnuplot can plot a large number of data points, but it makes sense to do a tail command to create a sliding view of the latest information.

Infos

  1. Gnuplot documentation: http://www.gnuplot.info/
  2. gpio command-line utility: http://wiringpi.com/the-gpio-utility/
  3. sensors command-line utility: https://wiki.archlinux.org/index.php/Lm_sensors

The Author

You can investigate more neat projects by Pete Metcalfe and his daughters at https://funprojects.blog.