Real-time plots in 20 lines

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
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}'
$ sensors | grep CPU
CPU: +48.0∞C
$ sensors | grep CPU | awk '{print $2}'
$ sensors | grep CPU | awk '{print substr($2,2,4)}'

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"
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"
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.


  1. Gnuplot documentation:
  2. gpio command-line utility:
  3. sensors command-line utility:

The Author

You can investigate more neat projects by Pete Metcalfe and his daughters at

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Open Data

    A lot of COVID-19 data is available through online REST APIs. With a little ingenuity and some open source tools, you can extract and analyze the data yourself.

  • Julia on the Pi

    Create GUIs and a web app that connects to sensors.

  • Tutorial – Matplotlib

    Matplotlib offers so many options that you may have trouble deciding on which ones to use for your plots.

  • Workspace: ExifTool

    Understanding the full power of ExifTool can be daunting. We show how to put it to practical use.

  • Go on the Rasp Pi

    We show you how to create a Go web app that controls Raspberry Pi I/O.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More