mkaz / termgraph

a python command-line tool which draws basic graphs in the terminal
MIT License
3.15k stars 164 forks source link

Stacked width inconsistent #83

Open mbforbes opened 3 years ago

mbforbes commented 3 years ago

Hi there,

Thank you very much for providing this library!

I'm attempting to use it to print a live indicator as a debugging aid. (I'm charting how much GPU memory is used vs free). I'm setting width=60 on every call. However, the total bar width changes when the data changes!

image

I'm wondering if the issue is with normalization not accounting for stacked charts?

In case it helps, here's how I'm calling it:

from collections import defaultdict
import time

from gpustat import core as gpus
from termgraph import termgraph

C = termgraph.AVAILABLE_COLORS

while True:
    # Settings
    gpu_idx = 7  # TODO: cmd line

    info = gpus.new_query()
    gpu = info[gpu_idx]

    termgraph.chart(
        colors=[C["red"], C["yellow"]],
        data=[[gpu.memory_used, gpu.memory_free]],
        args=defaultdict(
            bool,
            {
                "stacked": True,
                "width": 60,
                "format": "{:<5.2f}",
                "no_labels": True,
                "suffix": f" (used: {gpu.memory_used}, free: {gpu.memory_free})",
            },
        ),
        labels=[""],
    )

    time.sleep(1)

Many thanks for any tips, and apologies if I'm confused here!

mkaz commented 3 years ago

Correct it's the normalization, the width is the overall width and not necessarily what it will draw, just what it tries to fit the data in to.

I think to get consistent widths from call to call it will need a normalization factor passed in, so it treats data the same across calls.

danielhoherd commented 2 months ago

I'm also experiencing this on the CLI. EG:

$ termgraph --version
termgraph v0.5.3
$ cat data.txt
2024-07-23T12:48:01-04:00  2.27   97.73
2024-07-23T14:48:01-04:00  9.3    90.7
2024-07-23T16:48:01-04:00  12.93  87.07
2024-07-23T18:48:01-04:00  19.72  80.28
2024-07-23T20:48:01-04:00  23.6   76.4
2024-07-23T22:48:01-04:00  29.82  70.18
2024-07-24T00:48:01-04:00  31.33  68.67
$ cat data.txt | termgraph --stacked

2024-07-23T12:48:01-04:00: ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 100.00
2024-07-23T14:48:01-04:00: ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 100.00
2024-07-23T16:48:01-04:00: ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 100.00
2024-07-23T18:48:01-04:00: ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 100.00
2024-07-23T20:48:01-04:00: ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 100.00
2024-07-23T22:48:01-04:00: ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 100.00
2024-07-24T00:48:01-04:00: ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 100.00
Screenshot 2024-07-29 at 11 01 27 AM

If I truncate the decimals, essentially turning the floats into integers, the problem goes away:

$ cat data2.txt
2024-07-23T12:48:01-04:00  2   97
2024-07-23T14:48:01-04:00  9   90
2024-07-23T16:48:01-04:00  12  87
2024-07-23T18:48:01-04:00  19  80
2024-07-23T20:48:01-04:00  23  76
2024-07-23T22:48:01-04:00  29  70
2024-07-24T00:48:01-04:00  31  68
$ cat data2.txt | termgraph --stacked

2024-07-23T12:48:01-04:00: ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 99.00
2024-07-23T14:48:01-04:00: ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 99.00
2024-07-23T16:48:01-04:00: ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 99.00
2024-07-23T18:48:01-04:00: ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 99.00
2024-07-23T20:48:01-04:00: ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 99.00
2024-07-23T22:48:01-04:00: ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 99.00
2024-07-24T00:48:01-04:00: ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 99.00