DEV Community

petercour
petercour

Posted on

Draws Graphs in the Terminal with Python

If you are an advanced Linux or Mac user, you may use the terminal often.

Sometimes the terminal commands you run show a progress bar.

Like this:

Wondering how to show a progress bar in the terminal?

You can with termgraph

If you have a simple data file like this:

# Example Data Set 1
2007    183.32
2008    231.23
2009     16.43
2010     50.21
2011    508.97
2012    212.05
2014 1.0
Enter fullscreen mode Exit fullscreen mode

Then you can plot it like this:

~  ~/.local/bin/termgraph data.txt

2007: ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 183.32
2008: ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 231.23
2009: ▇ 16.43
2010: ▇▇▇▇ 50.21
2011: ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 508.97
2012: ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 212.05
2014: ▏ 1.00 
Enter fullscreen mode Exit fullscreen mode

From Python

Yes, you want to do this from code!

You can use this module from Python like this:

from termgraph import termgraph as tg

labels = ['2007', '2008', '2009', '2010', '2011', '2012', '2014']
data = [[183.32, 190.52], [231.23, 5.0], [16.43, 53.1], [50.21, 7.0], [508.97, 10.45], [212.05, 20.2], [30.0, 20.0]]
normal_data = [[48.059508408796894, 50.0], [60.971862871927556, 0.0],
               [3.080530401034929, 12.963561880120743],
               [12.184670116429496, 0.5390254420008624],
               [135.82632600258734, 1.4688443294523499],
               [55.802608883139285, 4.096593359206555],
               [6.737818025010781, 4.042690815006468]]
len_categories = 2
args = {'filename': 'data/ex4.dat', 'title': None, 'width': 50,
        'format': '{:<5.2f}', 'suffix': '', 'no_labels': True,
        'color': None, 'vertical': False, 'stacked': True,
        'different_scale': False, 'calendar': False,
        'start_dt': None, 'custom_tick': '', 'delim': '',
        'verbose': False, 'version': False}
colors = [91, 94]
tg.stacked_graph(labels, data, normal_data, len_categories, args, colors)
Enter fullscreen mode Exit fullscreen mode

More info:

Top comments (1)

Collapse
 
lisa_duignan profile image
lisa duignan 🇭🇰

What's the difference between the normal data and data? I'm not following how the data file and program inputs match. Would be grateful for more info!