终端中的Python ASCII图

 Lo海豚 发布于 2023-02-13 18:09

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

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

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

7 个回答
  • 正如@Benjamin Barenblat指出的那样,目前没有办法使用matplotlib.如果你真的想使用纯python库,你可以检查ASCII绘图仪.但是,正如我在上面评论的那样,我会按照建议使用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
    

    一些造型选项可以在这里找到.

    2023-02-13 18:10 回答
  • 你也可以试试Sympy的TextBackend情节,参见doc.或者只是使用textplot.

    这是一个例子

    from sympy import symbols
    from sympy.plotting import textplot
    x = symbols('x')
    textplot(x**2,0,5)
    

    与输出

    24.0992 |                                                      / 
            |                                                    ..  
            |                                                   /    
            |                                                 ..     
            |                                               ..       
            |                                              /         
            |                                            ..          
            |                                          ..            
    12.0496 | ---------------------------------------..--------------
            |                                     ...                
            |                                   ..                   
            |                                 ..                     
            |                              ...                       
            |                           ...                          
            |                        ...                             
            |                   .....                                
            |              .....                                     
          0 | .............                                          
              0                      2.5                        5    
    

    2023-02-13 18:10 回答
  • 如果你受限于matplotlib,答案目前是否定的.目前,matplotlib有许多后端,但ASCII不是其中之一.

    2023-02-13 18:10 回答
  • 我刚刚发布了asciiplotlib,希望能让你的生活更轻松.对于线图,您需要安装gnuplot和asciiplotlib,

    pip3 install termplotlib --user
    

    在此之后,生成线图

    import termplotlib as tpl
    import numpy
    
    x = numpy.linspace(0, 2 * numpy.pi, 10)
    y = numpy.sin(x)
    
    fig = tpl.figure()
    fig.plot(x, y, label="data", width=50, height=15)
    fig.show()
    
        1 +---------------------------------------+
      0.8 |    **     **                          |
      0.6 |   *         **           data ******* |
      0.4 | **                                    |
      0.2 |*              **                      |
        0 |                 **                    |
          |                                   *   |
     -0.2 |                   **            **    |
     -0.4 |                     **         *      |
     -0.6 |                              **       |
     -0.8 |                       **** **         |
       -1 +---------------------------------------+
          0     1    2     3     4     5    6     7
    

    2023-02-13 18:10 回答
  • 如果您只需要快速浏览并且x轴间距相等,您也可以自己做一些快速的ascii输出.

    In [1]: y = [20, 26, 32, 37, 39, 40, 38, 35, 30, 23, 17, 10,  5,  2,  0,  1,  3,
       ....:         8, 14, 20]
    
    In [2]: [' '*(d-1) + '*' for d in y]
    Out[2]: 
    ['                   *',
     '                         *',
     '                               *',
     '                                    *',
     '                                      *',
     '                                       *',
     '                                     *',
     '                                  *',
     '                             *',
     '                      *',
     '                *',
     '         *',
     '    *',
     ' *',
     '*',
     '*',
     '  *',
     '       *',
     '             *',
     '                   *']
    

    如果您的y-data不是整数,则偏移并缩放它们,使它们处于可行的范围内.例如,上述数字基本上是( sin(x)+1 )*20.

    2023-02-13 18:10 回答
  • 另请参阅:asciichart(在Node.js,Python,Java,Go和Haskell中实现)

    2023-02-13 18:10 回答
  • 很少有答案已经表明这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
    

    2023-02-13 18:15 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有