Skip to content

tgandor/asciiplotlib

 
 

Repository files navigation

asciiplotlib

CircleCI codecov Code style: black PyPi Version GitHub stars

asciiplotlib is a Python 3 library for all your terminal plotting needs. It aims to work like matplotlib.

Line plots

For line plots, asciiplotlib relies on gnuplot. With that installed, the code

import asciiplotlib as apl

x = numpy.linspace(0, 2 * numpy.pi, 10)
y = numpy.sin(x)

fig = apl.figure()
fig.plot(x, y, label="data", width=50, height=15)
fig.show()

produces

    1 +---------------------------------------+
  0.8 |-+  **    +A*   +     +     +    +   +-|
  0.6 |-+ A         **           data ***A***-|
  0.4 |-**                                  +-|
  0.2 |*+             A*                    +-|
    0 |-+               **                  +-|
      |                                   A   |
 -0.2 |-+                 A*            **  +-|
 -0.4 |-+                   **         *    +-|
 -0.6 |-+                            *A     +-|
 -0.8 |-+   +    +     +     +A*** **   +   +-|
   -1 +---------------------------------------+
      0     1    2     3     4     5    6     7

Tables

asciiplotlib provides many options for table plotting. For the most basic example, the code

import asciiplotlib as apl

numpy.random.seed(0)
data = numpy.random.rand(5, 2)

fig = apl.figure()
fig.table(data)
fig.show()

produces

table1

You can control border style, padding, alignment, and various other attributes. For example,

import asciiplotlib as apl

data = [
    [["a", "bb", "ccc"]],
    [[1, 2, 3], [613.23236243236, 613.23236243236, 613.23236243236]],
]

fig = apl.figure()
fig.table(data, border_style="thin", force_ascii=True, padding=(0, 1), alignment="lcr")
fig.show()

produces

+-----------------+-----------------+-----------------+
| a               |       bb        |             ccc |
+=================+=================+=================+
| 1               |        2        |               3 |
+-----------------+-----------------+-----------------+
| 613.23236243236 | 613.23236243236 | 613.23236243236 |
+-----------------+-----------------+-----------------+

See test/test_table.py for more examples.

Horizontal histograms

import asciiplotlib as apl

numpy.random.seed(123)
sample = numpy.random.normal(size=1000)
counts, bin_edges = numpy.histogram(sample)

fig = apl.figure()
fig.hist(counts, bin_edges, orientation="horizontal", force_ascii=False)
fig.show()

produces

hist1

Vertical histograms

import asciiplotlib as apl

numpy.random.seed(123)
sample = numpy.random.normal(size=1000)
counts, bin_edges = numpy.histogram(sample, bins=40)
fig = apl.figure()
fig.hist(counts, bin_edges, grid=[15, 25], force_ascii=False)
fig.show()

produces

hist2

Installation

asciiplotlib is available from the Python Package Index, so simply do

pip install -U asciiplotlib

to install or upgrade. Use sudo -H to install as root or the --user option of pip to install in $HOME.

Testing

To run the asciiplotlib unit tests, check out this repository and type

pytest

Distribution

To create a new release

  1. bump the __version__ number,

  2. publish to PyPi and tag on GitHub:

    $ make publish
    

License

asciiplotlib is published under the MIT license.

About

Plotting on the command line

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 98.4%
  • Makefile 1.6%