Python ASCII plots in terminal

后端 未结 9 2158
一向
一向 2020-12-04 15:39

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:

9条回答
  •  爱一瞬间的悲伤
    2020-12-04 16:02

    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
    

提交回复
热议问题