Plots in a single line are really simple, and can help one see patterns of highs and lows.
See also pysparklines.
(Does anyone know of unicode slanting lines, which could be fit together to make
line, not bar, plots ?)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import division
import numpy as np
__version__ = "2015-01-02 jan denis"
#...............................................................................
def onelineplot( x, chars=u"▁▂▃▄▅▆▇█", sep=" " ):
""" numbers -> v simple one-line plots like
f ▆ ▁ ▁ ▁ █ ▁ ▁ ▁ ▁ ▁ ▁ ▁ ▁ ▁ ▁ ▁ ▁ ▁ ▁ ▁ osc 47 ▄ ▁ █ ▇ ▄ ▆ ▅ ▇ ▇ ▇ ▇ ▇ ▄ ▃ ▃ ▁ ▃ ▂ rosenbrock
f █ ▅ █ ▅ █ ▅ █ ▅ █ ▅ █ ▅ █ ▅ █ ▅ ▁ ▁ ▁ ▁ osc 58 ▂ ▁ ▃ ▂ ▄ ▃ ▅ ▄ ▆ ▅ ▇ ▆ █ ▇ ▇ ▃ ▃ ▇ rastrigin
f █ █ █ █ ▁ ▁ ▁ ▁ ▁ ▁ ▁ ▁ ▁ ▁ ▁ ▁ ▁ ▁ ▁ ▁ osc 90 █ ▇ ▇ ▁ █ ▇ █ ▇ █ ▇ █ ▇ █ ▇ █ ▇ █ ▇ ackley
Usage:
astring = onelineplot( numbers [optional chars= sep= ])
In:
x: a list / tuple / numpy 1d array of numbers
chars: plot characters, default the 8 Unicode bars above
sep: "" or " " between plot chars
How it works:
linscale x -> ints 0 1 2 3 ... -> chars ▁ ▂ ▃ ▄ ...
See also: https://github.com/RedKrieg/pysparklines
"""
xlin = _linscale( x, to=[-.49, len(chars) - 1 + .49 ])
# or quartiles 0 - 25 - 50 - 75 - 100
xints = xlin.round().astype(int)
assert xints.ndim == 1, xints.shape # todo: 2d
return sep.join([ chars[j] for j in xints ])
def _linscale( x, from_=None, to=[0,1] ):
""" scale x from_ -> to, default min, max -> 0, 1 """
x = np.asanyarray(x)
m, M = from_ if from_ is not None \
else [np.nanmin(x), np.nanmax(x)]
if m == M:
return np.ones_like(x) * np.mean( to )
return (x - m) * (to[1] - to[0]) \
/ (M - m) + to[0]
#...............................................................................
if __name__ == "__main__": # standalone test --
import sys
if len(sys.argv) > 1: # numbers on the command line, may be $(cat myfile)
x = map( float, sys.argv[1:] )
else:
np.random.seed( 0 )
x = np.random.exponential( size=20 )
print onelineplot( x )