Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
837 views
in Technique[技术] by (71.8m points)

matplotlib - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As @Benjamin Barenblat pointed out, there is currently no way using matplotlib. If you really want to use a pure python library, you may check ASCII Plotter. However, as I commented above, I would use gnuplot as suggested e.g. in this question.

To use gnuplot directly from python you could either use Gnuplot.py (I haven't tested this yet) or use gnuplot with the scripting interface. Latter can be realised (as suggested here) like:

import numpy as np
x=np.linspace(0,2*np.pi,10)
y=np.sin(x)
import subprocess
gnuplot = subprocess.Popen(["/usr/bin/gnuplot"], 
                           stdin=subprocess.PIPE)
gnuplot.stdin.write("set term dumb 79 25
")
gnuplot.stdin.write("plot '-' using 1:2 title 'Line1' with linespoints 
")
for i,j in zip(x,y):
   gnuplot.stdin.write("%f %f
" % (i,j))
gnuplot.stdin.write("e
")
gnuplot.stdin.flush()

This gives a plot like

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

Some styling options can be found e.g. here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...