1. Introduction

We sometimes need a visual presentation of our data to make it easier to understand. Indeed, there are various Linux utilities to help us in this regard.

In this tutorial, we’ll explore several command-line charting and plotting tools. For RPM-based distributions, epel-release may need to be installed before the plotting tool package in question.

2. Using gnuplot

gnuplot is a command-line and GUI program that can generate plots. It can produce output on the screen or in many graphics formats, including PNG, EPS, SVG, and JPEG.

First, let’s install gnuplot:

$ dnf -y install gnuplot

Now, let’s run it:

$ gnuplot
...
Terminal type is now 'qt'
gnuplot>

Here, we can see that the current terminal is qt. We should change the terminal type to dumb because it can plot into a text block using ASCII characters:

gnuplot> set terminal dumb
Terminal type is now 'dumb'
Options are 'feed  size 79, 24 aspect 2, 1 mono'

In essence, the terminal is set. Hence, we can use the plot command to plot our desired function:

gnuplot> plot cos(x)         
    1 +--------------------------------------------------------------------+   
      |          *  ** +               * +*               + **  *          |   
  0.8 |-+       *    *                *    *                *    *       +-|   
      |         *    *                *     *               *    *         |   
  0.6 |-+       *     *              *      *              *     *       +-|   
      |        *       *             *      *              *      *        |   
  0.4 |-+      *       *             *      *             *        *     +-|   
      |       *        *            *        *            *        *       |   
  0.2 |-+     *        *            *         *           *        *     +-|   
      |       *         *          *          *           *        *       |   
    0 |-+     *         *          *          *          *          *    +-|   
      |      *           *         *          *          *          *      |   
 -0.2 |-+    *           *         *           *        *            *   +-|   
      |     *             *       *            *        *            *     |   
 -0.4 |-+   *             *       *             *      *             *   +-|   
      |     *             *      *              *      *             *     |   
 -0.6 |-+   *              *     *               *     *              *  +-|   
      |    *               *    *                *    *               *    |   
 -0.8 |-+ *                 *   *                *   *                 * +-|   
      |* *             +    *  *         +        *  *    +             * *|   
   -1 +--------------------------------------------------------------------+   
     -10              -5                 0                5                10 

Also, we can change some options such as size, aspect, and output:

gnuplot> set terminal dumb size 60,15 aspect 1 ansi  
Terminal type is now 'dumb'
Options are 'feed  size 60, 15 aspect 1, 1 ansi'

gnuplot> plot cos(x) 
                                                            
    1 +-------------------------------------------------+   
  0.8 |+      +  + +         +++++          ++  +      +|   
  0.6 |+     +    +          +   +          +    +     +|   
  0.4 |+     +    +         +     +         +    +     +|   
  0.2 |+    +      +        +     +        +      +    +|   
    0 |+    +      +       +       +       +      +    +|   
      |    +        +      +       +      +        +    |   
 -0.2 |+   +        +     +         +     +        +   +|   
 -0.4 |+  +          +    +         +    +          +  +|   
 -0.6 |+  +          +   +           +   +          +  +|   
 -0.8 |+ +         +  +  +     +     +  +   +        + +|   
   -1 +-------------------------------------------------+   
     -10          -5           0            5           10 

For the above example, we’ve changed our terminal’s size to 60,15. Moreover, we changed the aspect to 1. Finally, we used ansi instead of mono output to show a multi-colored result.

If we want to save our output in a graphical format like PNG, we can set the output destination before running our plot command:

gnuplot> set output 'myfile.png'

After running this command, our next plot will be saved to myfile.png.

3. Using asciichart

asciichart draws line charts in pure JavaScript (for Node.js and browsers).

So, before installing asciichart, we need to install Node.js:

$ yum install nodejs

Now, we can install asciichart via npm:

$ npm install asciichart

Let’s open a file named mychart.js with an editor like nano:

$ nano mychart.js

Following this, we can write our JavaScript code in that file:

var asciichart = require ('asciichart');
var draw = new Array (50); 
for (var i = 0; i < draw.length; i++) 
  draw[i]=Math.sin(i)
console.log(asciichart.plot(draw))

On the first line, we asked JavaScript to use asciichart by requiring it. Then, we created an array with a length of 50 and stored our variables in it via a for loop. Finally, we plot our draw with asciichart.plot(draw).

We can run our JavaScript file with the node command:

$ node mychar.js
       1.00 ┤╭─╮   ╭─╮    ╭─╮   ╭─╮   ╭─╮   ╭──╮   ╭─╮   ╭─╮   
      -0.00 ┼╯ ╰╮ ╭╯ ╰╮  ╭╯ ╰╮ ╭╯ ╰╮ ╭╯ ╰╮ ╭╯  ╰╮ ╭╯ ╰╮ ╭╯ ╰╮  
      -1.00 ┤   ╰─╯   ╰──╯   ╰─╯   ╰─╯   ╰─╯    ╰─╯   ╰─╯   ╰─ 

In the output, we can see that all result points are between 1 and -1, as expected for the mathematical function.

4. Using bashplotlib

bashplotlib is a Python package and command-line tool for drawing basic plots in the terminal. To visualize data, we use its main command named scatter.

First, let’s install pip:

$ yum install pip

Now, we can install bashplotlib via pip:

$ pip install bashplotlib

Let’s create a simple file, points.txt, containing some coordinates:

$ cat points.txt
1,1
2,2
3,3
4,4
5,5
6,6
7,7
8,8
9,9
10,10

Now, we can use the scatter command to plot our data file:

$ scatter --file points.txt
--------------------------
|                   x    |
|                 x      |
|               x        |
|             x          |
|           x            |
|         x              |
|       x                |
|     x                  |
|   x                    |
| x                      |
--------------------------

We can also change the properties by using –size and –color.

5. Using pysparklines

pysparklines is a Unicode sparkline chart generation library. A sparkline is a graph of sequential values exposed horizontally where the height of the line is proportional to the values in substitution.

Again, first, we need to install pip:

$ yum install pip

Now, let’s install pysparklines via the pip package management:

$ pip install pysparklines

At this point, we can use the sparkline command with a simple dataset:

$ sparkline 10 9 8 7 6 5 4 3 2 1
█▇▆▆▅▄▃▃▂▁

Alternatively, we just pipe data to sparkline:

$ echo "1 2 3 4 5 6 7 8 9 10" | sparkline
▁▂▃▃▄▅▆▆▇█

Also, we can change the aspect by using -r:

$ seq 10 | sort -R | sparkline -r4
 █    ▁  ▅
 █  ▆ █▂ █
 █ ▃█▇██ █
 ██▄█████▁█

In essence, the output of the above command is a sequence of numbers between 1 and 10, in random order, that the command shows in a sparkline chart with an aspect of 4.

6. Using ttyplot

ttyplot is a real-time plotting utility for text-mode consoles with data input.

This tool can take data from standard input like ping, snmpget, netstat, ifconfig, ip, sar, or vmstat, and plot in text mode.

To get ttyplot, we should first install snap. Then, we can install ttyplot via snap:

$ /var/lib/snapd/snap install ttyplot

Before using ttyplot, we should add /snap/bin to our $PATH:

$ export PATH=$PATH:/snap/bin

Now, we should be able to run ttyplot:

$ ttyplot
waiting for data from stdin

Basically, this message means the package is successfully installed.

To simulate a real-time environment, let’s use the ping command:

$ ping 1.1.1.1 | sed -u 's/^.*time=//g; s/ ms//g' | ttyplot -t "ping to 1.1.1.1" -u ms 
                            ping to 1.1.1.1
  ^ 185.0 ms                     │
  │                              │
  │                              │
  │                              │
  │                        │││   │
  │ 138.8 ms               │││   │            ││  │       │
  ││  ││ │││ ││││  │││  │ │││││││││   │ │     ││  │     │ │   │     │
  ││││││││││ ││││││││││││││││││││││││ ││││  ││││  │  ││││││ │││││││││
  │││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││
  │││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││
  ││92.5 ms││││││││││││││││││││││││││││││││││││││││││││││││││││││││││
  │││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││
  │││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││
  │││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││
  │││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││
  ││46.2 ms││││││││││││││││││││││││││││││││││││││││││││││││││││││││││
  │││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││
  │││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││
  │││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││
  │││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││
  └─────────────────────────────────────────────────────────────────>
     │ last=135.0 min=116.0 max=185.0 avg=130.6 ms r 11 13:54:19 2023
                                        github.com/tenox7/ttyplot 1.4

Here, we extract the ping command’s times via the sed command. Moreover, we use -t to set the chart title and -u to set the unit beside the vertical bar.

7. Using termgraph

termgraph is a Python-based tool that we can use to show data in different ways:

  • bar graphs
  • color charts
  • multi-variable graphs
  • stacked charts
  • horizontal charts
  • vertical charts

First, we need to install python3:

$ yum install -y python3-setuptools git

At this point, let’s install termgraph via pip3:

$ pip3 install termgraph

Next, we’ll prepare our data in a file:

$ cat myGraph.txt
January 27
February 136.5
March 98.7
April 84
May 27.35
June 74.7
July 83.2
August 67
September 74.4
October 66.3
November 46
December 19.5

Finally, we can easily use the termgraph command to read and plot the data from our file:

$ termgraph myGraph.txt 

January  : ▇▇▇▇▇▇▇▇▇ 27.00
February : ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 136.50
March    : ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 98.70
April    : ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 84.00
May      : ▇▇▇▇▇▇▇▇▇▇ 27.35
June     : ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 74.70
July     : ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 83.20
August   : ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 67.00
September: ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 74.40
October  : ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 66.30
November : ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 46.00
December : ▇▇▇▇▇▇▇ 19.50

Of course, the graph depends on the provided data and arguments. We can use –color in order to change the color of the chart and –title to set a title for it.

8. Conclusion

In this article, we’ve looked at different tools for charting and plotting via the Linux command line. Depending on our requirements, we can use one or more of these tools. Some are better at plotting mathematical functions like cos(x), while others use data files.