终端中的 Python ASCII 图

新手上路,请多包涵

使用 Octave,我可以将数组绘制到终端,例如,绘制一个包含函数值的数组 x^2 在我的终端中给出以下输出:

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

有什么方法可以在 Python 中做类似的事情,特别是使用 matplotlib? bashplotlib 似乎提供了一些这样的功能,但与 Octave 的产品相比似乎非常基本。

原文由 Mike Vella 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 450
2 个回答

由于很少有答案已经表明 gnuplot 是一个不错的选择。

但是,不需要调用 gnuplot 子进程,使用 python gnuplotlib 库可能会容易得多。

示例(来自: 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

原文由 Datageek 发布,翻译遵循 CC BY-SA 3.0 许可协议

正如@Benjamin Barenblat 指出的那样,目前无法使用 matplotlib。如果你真的想使用纯 python 库,你可以检查 ASCII Plotter 。但是,正如我在上面评论的那样,我会按照 这个 问题中的建议使用 gnuplot

要直接从 python 使用 gnuplot,您可以使用 Gnuplot.py (我还没有测试过)或将 gnuplot 与脚本接口一起使用。后者可以像这样实现(如 这里 所建议的):

 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\n")
gnuplot.stdin.write("plot '-' using 1:2 title 'Line1' with linespoints \n")
for i,j in zip(x,y):
   gnuplot.stdin.write("%f %f\n" % (i,j))
gnuplot.stdin.write("e\n")
gnuplot.stdin.flush()

这给出了一个像

    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

可以在 此处 找到一些样式选项。

原文由 Jakob 发布,翻译遵循 CC BY-SA 3.0 许可协议

推荐问题