Real-time plots in 20 lines
The Plot Twists
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).
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.
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).
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
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.
News
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.