(Last Updated On: July 30, 2018)

Hello folks, in this tutorial, we are going to learn how to draw basic graphs on the 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.

If running CentOS 7/6 or any other RHEL derivatives, python3 is available on EPEL repository. Add epel repo then install python3 using the commands:

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

On Ubuntu latest LTS releases, Python3 is available on jonathonf PPA, add it using the command:

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

Then check for updates and install python3 (even though they usually ship with python 3, if already installed, you can skip this step)

$ sudo apt-get update -y && apt-get install 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 2: Install Termgraph on Linux

To install termgraph, clone termgraph repository:

$ git clone https://github.com/mkaz/termgraph.git
Cloning into 'termgraph'...
remote: Counting objects: 183, done.
remote: Compressing objects: 100% (57/57), done.
remote: Total 183 (delta 46), reused 74 (delta 31), pack-reused 88
Receiving objects: 100% (183/183), 320.91 KiB | 37.00 KiB/s, done.
Resolving deltas: 100% (79/79), done.

You are now ready to use termgraph to plot graphs.

$ cd termgraph
$ ls
data docs readme.md termgraph.py

Copy termgraph.py to /usr/local/bin

$ sudo cp termgraph.py /usr/local/bin/termgraph
$ ls /usr/local/bin/termgraph

Since /usr/local/bin/ is in your $PATH, you can start using by checking Termgraph  by just calling the name

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]
                 [filename]

draw basic graphs on terminal

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

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

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
2018,200.45,70.5
2017,110.45,300.46
2016,77.89,120.45
2015,66.2,345.22
2014,760.00,30.24
2013,450.56,472

Then:

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

Working with stacked data without categories:

$ cat test3.dat 
# Example Data Set without Categories' names
2018,200.45,70.5
2017,110.45,300.46
2016,77.89,120.45
2015,66.2,345.22
2014,760.00,30.24
2013,450.56,472

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.