Termgraph – How To Draw basic graphs on Linux terminal

Posted on 20 views

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 7

If running CentOS 7 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 python3-setuptools git

Confirm successful installation by checking Python3 version:

$ python3 -V
Python 3.6.8

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

Then install python3.

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

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:

# Python 3.6
sudo easy_install-3.6 pip

# Python 3.8
sudo easy_install-3.8 pip

On Debian derivatives:

sudo apt update
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
  Downloading https://files.pythonhosted.org/packages/42/81/129559d468883a8a44edb50461c64d50ab96a1ac0b166586f99ba55b5311/termgraph-0.5.0-py3-none-any.whl
Requirement already satisfied: colorama in /usr/local/lib/python3.6/site-packages (from termgraph)
Installing collected packages: termgraph
Successfully installed termgraph-0.5.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:

$ vim 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:

$ termgraph dset1.dat
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:

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

Then run the following command to generate a graph:

$ termgraph test2.dat --color red,green

See expected output below:


Working with stacked data without categories:

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

Then run:

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

See output:


Using Calendar Heatmap

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

$ vim 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.



Gravatar Image
A systems engineer with excellent skills in systems administration, cloud computing, systems deployment, virtualization, containers, and a certified ethical hacker.