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.8.10
Install Python 3 on Ubuntu
Install Python3
sudo apt update
sudo apt install python3
Ubuntu 18.04 and other recent releases comes with Python 3.x.
$ python3 -V
Python 3.8.10
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 yum install python3-pip
On Debian derivatives:
sudo apt update
sudo apt 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 termgraph-0.5.3-py3-none-any.whl (15 kB)
Requirement already satisfied: colorama in /usr/lib/python3/dist-packages (from termgraph) (0.4.3)
Installing collected packages: termgraph
Successfully installed termgraph-0.5.3
You are now ready to use termgraph to plot graphs.
$ which termgraph
/usr/local/bin/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]
[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
--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
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 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
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"
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.
Similar content: