Skip to content
Advertisement

Python ASCII plots in terminal

With Octave I am able to plot arrays to the terminal, for example, plotting an array with values for the function x^2 gives this output in my terminal:

   10000 ++---------+-----------+----------+-----------+---------++
         ++         +           +          +           +         ++
         |+         :           :          :           :         +|
         |++        :           :          :           :        ++|
         | +        :           :          :           :        + |
         | ++       :           :          :           :       ++ |
    8000 ++.+..................................................+.++
         |  ++      :           :          :           :      ++  |
         |   ++     :           :          :           :     ++   |
         |    +     :           :          :           :     +    |
         |    ++    :           :          :           :    ++    |
         |     +    :           :          :           :    +     |
    6000 ++....++..........................................++....++
         |      ++  :           :          :           :  ++      |
         |       +  :           :          :           :  +       |
         |       ++ :           :          :           : ++       |
         |        ++:           :          :           :++        |
    4000 ++........++..................................++........++
         |          +           :          :           +          |
         |          ++          :          :          ++          |
         |          :++         :          :         ++:          |
         |          : ++        :          :        ++ :          |
         |          :  ++       :          :       ++  :          |
    2000 ++.............++........................++.............++
         |          :    ++     :          :     ++    :          |
         |          :     +++   :          :   +++     :          |
         |          :       ++  :          :  ++       :          |
         |          :        +++:          :+++        :          |
         +          +          ++++      ++++          +          +
       0 ++---------+-----------+----------+-----------+---------++
         0        20000       40000      60000       80000     100000

Is there some way I can do something similar in Python, specifically with matplotlib? bashplotlib seems to offer some of this functionality but appears to be quite basic compared to Octave’s offering.

Advertisement

Answer

As few answers already suggested the gnuplot is a great choice.

However, there is no need to call a gnuplot subprocess, it might be much easier to use a python gnuplotlib library.

Example (from: https://github.com/dkogan/gnuplotlib):

>>> import numpy as np
>>> import gnuplotlib as gp

>>> x = np.linspace(-5,5,100)

>>> gp.plot( x, np.sin(x) )
[ graphical plot pops up showing a simple sinusoid ]


>>> gp.plot( (x, np.sin(x), {'with': 'boxes'}),
...          (x, np.cos(x), {'legend': 'cosine'}),

...          _with    = 'lines',
...          terminal = 'dumb 80,40',
...          unset    = 'grid')

[ ascii plot printed on STDOUT]
   1 +-+---------+----------+-----------+-----------+----------+---------+-+
     +     +|||+ +          +         +++++   +++|||+          +           +
     |     |||||+                    +     +  +||||||       cosine +-----+ |
 0.8 +-+   ||||||                    +     + ++||||||+                   +-+
     |     ||||||+                  +       ++||||||||+                    |
     |     |||||||                  +       ++|||||||||                    |
     |     |||||||+                +        |||||||||||                    |
 0.6 +-+   ||||||||               +         +||||||||||+                 +-+
     |     ||||||||+              |        ++|||||||||||                   |
     |     |||||||||              +        |||||||||||||                   |
 0.4 +-+   |||||||||              |       ++||||||||||||+                +-+
     |     |||||||||             +        +||||||||||||||                  |
     |     |||||||||+            +        |||||||||||||||                  |
     |     ||||||||||+           |       ++||||||||||||||+           +     |
 0.2 +-+   |||||||||||          +        |||||||||||||||||           +   +-+
     |     |||||||||||          |        +||||||||||||||||+          |     |
     |     |||||||||||         +         ||||||||||||||||||         +      |
   0 +-+   +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++   +-+
     |       +        ||||||||||||||||||+         |       ++||||||||||     |
     |       |        +|||||||||||||||||          +        |||||||||||     |
     |       +        ++||||||||||||||||          |        +||||||||||     |
-0.2 +-+      +        |||||||||||||||||          +        |||||||||||   +-+
     |        |        ++||||||||||||||+           |       ++|||||||||     |
     |        +         |||||||||||||||            +        ++||||||||     |
     |         |        +||||||||||||||            +         |||||||||     |
-0.4 +-+       +        ++||||||||||||+             |        +||||||||   +-+
     |          +        |||||||||||||              +        |||||||||     |
     |          |        +|||||||||||+               +       ++|||||||     |
-0.6 +-+        +        ++||||||||||                |        +|||||||   +-+
     |           +        |||||||||||                +        ++||||||     |
     |           +        +|||||||||+                 +        |||||||     |
     |            +       ++||||||||                  +       +++|||||     |
-0.8 +-+          +      + ++||||||+                   +      + +|||||   +-+
     |             +    +   +||||||                     +    +  ++||||     |
     +           +  +  ++   ++|||++     +           +   ++  +  + ++|||     +
  -1 +-+---------+----------+-----------+-----------+----------+---------+-+
    -6          -4         -2           0           2          4           6
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement