Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion plotille/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@
from ._cmaps import Colormap, ListedColormap
from ._colors import color, hsl
from ._figure import Figure
from ._graphs import hist, hist_aggregated, histogram, plot, scatter
from ._graphs import hbar, hist, hist_aggregated, histogram, plot, scatter


__all__ = [
'Canvas',
'color',
'Colormap',
'Figure',
'hbar',
'hist',
'hist_aggregated',
'histogram',
Expand Down
46 changes: 46 additions & 0 deletions plotille/_graphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,59 @@

from math import log
import os
from typing import List, Optional

from ._colors import color
from ._figure import Figure
from ._input_formatter import InputFormatter
from ._util import hist as compute_hist


def hbar(counts, labels=None, width=80, log_scale=False, linesep=os.linesep,
lc=None, bg=None, color_mode='names'):
"""
Create a horizontal bar chart.

Parameters:
counts: List[int] Counts for each bucket.
labels: List[str] Labels for the provided counts. Optional.
Hence, `len(labels) == len(counts)`.
width: int The number of characters for the width (columns).
log_scale: bool Scale the histogram with `log` function.
linesep: str The requested line seperator. default: os.linesep
lc: multiple Give the line color.
bg: multiple Give the background color.
color_mode: str Specify color input mode; 'names' (default), 'byte' or 'rgb'
see plotille.color.__docs__
Returns:
str: histogram over `X` from left to right.
"""
def _scale(a):
if log_scale and a > 0:
return log(a)
return a

h = counts

ipf = InputFormatter()
l_max = max(len(label) for label in labels)
h_max = _scale(max(h)) or 1

bins_count = len(h)

canvas = ['{}| {} {}'.format('label'.ljust(l_max + 1), '_' * width, 'Total Counts')]
lasts = ['', '⠂', '⠆', '⠇', '⡇', '⡗', '⡷', '⡿']
for i in range(bins_count):
height = int(width * 8 * _scale(h[i]) / h_max)
canvas += ['{} | {} {}'.format(
ipf.fmt(labels[i], delta=None, chars=l_max, left=True),
color('⣿' * (height // 8) + lasts[height % 8], fg=lc, bg=bg, mode=color_mode)
+ color('\u2800' * (width - (height // 8) + int(height % 8 == 0)), bg=bg, mode=color_mode),
h[i])]
canvas += ['‾' * (l_max + 2 + 3 + width + 12)]
return linesep.join(canvas)


def hist_aggregated(counts, bins, width=80, log_scale=False, linesep=os.linesep,
lc=None, bg=None, color_mode='names'):
"""
Expand Down
Loading