You can support us by downloading this article as PDF from the Link below. Download the guide as PDF

Hello folks, in this tutorial, we are going to learn how to draw basic graphs on the Linux terminal using a python based tool called Termgraph. Termgraph can be used to draw a number of graphs on the terminal such as; Bar Graphs, Color charts, Multi-variable, Stacked charts, Horizontal or Vertical and Emoji.

Installing Termgraph on Linux 

Below are the steps to install and use Termgraph to draw basic graphs on the terminal.

Step 1: Install Python3

Termgraph requires python3 on the local system to be installed. For Arch Linux users, python3 is the default version that ships with the OS.

Install Python3 on CentOS

If running CentOS 7/6 or any other RHEL derivatives, python3 is available on EPEL repository.

Add EPEL repository then install Python3 using the commands:

sudo yum install -y epel-release
sudo yum install -y python36-setuptools git

Install Python 3 on Ubuntu

Ubuntu 18.04 and other recent releases comes with Python 3.x.

$ python3 -V
Python 3.6.7

For older Ubuntu systems, Python3 is available on jonathonf PPA, add it using the command:

sudo add-apt-repository ppa:jonathonf/python-3.6

Then install python3.

sudo apt-get update
sudo apt-get install -y git python3.6

Step 2: Install colorama Python module

To install colorama, you need to have pip3 installed. If not already installed, run the following commands to install it;

On RHEL derivatives:

sudo easy_install-3.6 pip

On Debian derivatives:

sudo apt-get install -y python3-pip

Once pip3 is installed, install colorama using the commands:

sudo pip3 install colorama

Step 3: Install Termgraph on Linux

To install termgraph using pip3 package manager for Python.

$ sudo pip3 install termgraph
Collecting termgraph
Requirement already satisfied: colorama in /usr/lib/python3/dist-packages (from termgraph)
Installing collected packages: termgraph
Successfully installed termgraph-0.2.0

You are now ready to use termgraph to plot graphs.

$ which termgraph

Check help page for basic usage of termgraph:

$ termgraph --help
usage: termgraph [-h] [--title TITLE] [--width WIDTH] [--format FORMAT]
                 [--suffix SUFFIX] [--no-labels]
                 [--color [{red,blue,green,magenta,yellow,black,cyan} [{red,blue,green,magenta,yellow,black,cyan} ...]]]
                 [--vertical] [--stacked] [--different-scale] [--calendar]
                 [--start-dt START_DT] [--custom-tick CUSTOM_TICK]
                 [--delim DELIM] [--verbose] [--version]

draw basic graphs on terminal

positional arguments:
  filename              data file name (comma or space separated). Defaults to

optional arguments:
  -h, --help            show this help message and exit
  --title TITLE         Title of graph
  --width WIDTH         width of graph in characters default:50
  --format FORMAT       format specifier to use.
  --suffix SUFFIX       string to add as a suffix to all data points.
  --no-labels           Do not print the label column
  --color [{red,blue,green,magenta,yellow,black,cyan} [{red,blue,green,magenta,yellow,black,cyan} ...]]
                        Graph bar color( s )
  --vertical            Vertical graph
  --stacked             Stacked bar graph
  --different-scale     Categories have different scales.
  --calendar            Calendar Heatmap chart
  --start-dt START_DT   Start date for Calendar chart
  --custom-tick CUSTOM_TICK
                        Custom tick mark, emoji approved
  --delim DELIM         Custom delimiter, default , or space
  --verbose             Verbose output, helpful for debugging
  --version             Display version and exit

Example usage of Termgraph

Create a data file with two columns either comma or space separated. The first column is your labels, the second column is a numeric data

$ mkdir termgraph-data
$ cd termgraph-data

Create test data file:

$ cat dset1.dat
2018 200.45
2017 110.45
2016 77.89
2015 66.2
2014 760.00
2013 450.56

Then generate chart on the terminal:

2018: ▇▇▇▇▇▇▇▇▇ 200.45
2017: ▇▇▇ 110.45
2016: ▏ 77.89
2015: ▏ 66.20
2014: ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 760.00
2013: ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 450.56

This reads data from filedset1.dat.

You can also use colors, see below:

$ cat test2.dat
# Example Data Set with 2 Categories
@ Oranges,Pineapples


$ termgraph test2.dat --color {red,green}

Working with stacked data without categories:

$ cat test3.dat 
# Example Data Set without Categories' names

Then run:

$ termgraph test3.dat --color {magenta,green} --stacked --title "Stacked Data"

Using Calendar Heatmap

This expects the first column to be a date in the format YYYY-mm-dd

$ cat cal.dat
2017-01-10 100.34
2017-02-11 50.00
2017-03-12 66.45
2017-04-13 10.11
2017-05-14 0.00
2017-06-15 30
2017-07-16 24.56
2018-01-10 6.55
2018-02-11 6.88
2018-03-12 10.34
2018-04-13 40.45
2018-05-14 66.78
2018-06-15 55.45
2018-07-16 5.67

You can specify the start date

Using stdin and emoji

Example of providing emoji symbol on stdin:

echo "Label,5,10,20,15,35,10,45,3,7,28" | termgraph --custom-tick "?" --no-label

The output should be like below:

Enjoy using Termgraph and keep watching for updates.

Similar content:

How To Install and Use Tmux on Linux

How to Install and Use lazygit – A simple terminal UI for git commands

How To generate gif images from Screen recordings in Linux

Ten Best Terminal Emulators for Linux

You can support us by downloading this article as PDF from the Link below. Download the guide as PDF