代码之家  ›  专栏  ›  技术社区  ›  bajafresh4life

基于Unix ASCII的命令行图表/绘图工具

  •  177
  • bajafresh4life  · 技术社区  · 17 年前

    有没有一个好的命令行UNIX图表/图形/绘图工具?我正在寻找一种可以在ASCII图上绘制xy点的东西。

    为了澄清一下,我正在寻找一种能够以ASCII格式输出图形(如ASCII艺术风格)的东西,这样我就可以在交互式shell会话中使用它,而不需要X。

    14 回复  |  直到 17 年前
        1
  •  143
  •   Palmin    17 年前

    尝试 gnuplot 它具有非常强大的绘图能力。

    它可以通过以下方式输出到您的终端:

    gnuplot> set terminal dumb
    Terminal type set to 'dumb'
    Options are 'feed 79 24'
    gnuplot> plot sin(x)
    
       1 ++----------------**---------------+----**-----------+--------**-----++
         +                *+ *              +   *  *          +  sin(x) ****** +
     0.8 ++              *    *                *    *                *    *   ++
         |               *    *                *    *                *    *    |
     0.6 ++              *     *              *      *              *      *  ++
         *              *       *             *       *             *      *   |
     0.4 +*             *       *             *       *             *      *  ++
         |*            *        *            *        *            *        *  |
     0.2 +*            *        *            *        *            *        * ++
         | *          *          *          *          *          *          * |
       0 ++*          *          *          *          *          *          *++
         |  *         *           *         *           *         *           *|
    -0.2 ++ *         *           *         *           *         *           *+
         |   *       *            *        *            *        *            *|
    -0.4 ++  *       *            *        *            *        *            *+
         |   *      *              *      *              *      *              *
    -0.6 ++  *      *              *      *              *      *             ++
         |    *     *               *     *               *    *               |
    -0.8 ++    *   *                 *   *                *    *              ++
         +     *  *        +         *  *   +              *  *                +
      -1 ++-----**---------+----------**----+---------------**+---------------++
        -10               -5                0                 5                10
    
        2
  •  72
  •   Xiong Chiamiov    13 年前

    与…同时 gnuplot 它功能强大,当你只想输入一堆点并得到一个图时,它也真的很烦人。

    幸运的是,有人创造了 eplot (简单的情节),它为你处理所有的废话。

    它似乎没有强制使用终端图的选项;我是这样修补的:

    --- eplot.orig  2012-10-12 17:07:35.000000000 -0700
    +++ eplot       2012-10-12 17:09:06.000000000 -0700
    @@ -377,6 +377,7 @@
                    # ---- print the options
                    com="echo '\n"+getStyleString+@oc["MiscOptions"]
                    com=com+"set multiplot;\n" if doMultiPlot
    +               com=com+"set terminal dumb;\n"
                    com=com+"plot "+@oc["Range"]+comString+"\n'| gnuplot -persist"
                    printAndRun(com)
                    # ---- convert to PDF
    

    使用示例:

    [$]> git shortlog -s -n | awk '{print $1}' | eplot 2> /dev/null
    
    
      3500 ++-------+-------+--------+--------+-------+--------+-------+-------++
           +        +       +        "/tmp/eplot20121012-19078-fw3txm-0" ****** +       *                                                                    |  3000 +*                                                                  ++       |*                                                                   |       | *                                                                  |  2500 ++*                                                                 ++       | *                                                                  |
           |  *                                                                 |
      2000 ++ *                                                                ++
           |  **                                                                |
      1500 ++   ****                                                           ++
           |        *                                                           |
           |         **                                                         |
      1000 ++          *                                                       ++
           |            *                                                       |
           |            *                                                       |
       500 ++            ***                                                   ++
           |                **************                                      |
           +        +       +        +    **********  +        +       +        +
         0 ++-------+-------+--------+--------+-----***************************++
           0        5       10       15       20      25       30      35       40
    
        3
  •  28
  •   Community Mohan Dere    9 年前

    另请参见: asciichart (在Node.js和 ported Python、Java, Go 哈斯克尔)

    enter image description here

        4
  •  24
  •   Igor Kroitor    7 年前

    我刚刚遇到的另一个选择是 bashplotlib 。这是一个在与(大致)相同的数据上运行的示例 my eplot example :

    [$]> git shortlog -s -n | awk '{print $1}' | hist
    
     33|   o
     32|   o
     30|   o
     28|   o
     27|   o
     25|   o
     23|   o
     22|   o
     20|   o
     18|   o
     16|   o
     15|   o
     13|   o
     11|   o
     10|   o
      8|   o
      6|   o
      5|   o
      3|   o o     o
      1|   o o     o o       o
      0|   o o o o o o       o
        ----------------------
    
    -----------------------
    |       Summary       |
    -----------------------
    |   observations: 50  |
    | min value: 1.000000 |
    |  mean : 519.140000  |
    |max value: 3207.000000|
    -----------------------
    

    调整箱子有助于提高分辨率:

    [$]> git shortlog -s -n | awk '{print $1}' | hist --nosummary --bins=40
    
     18|   o
       |   o
     17|   o
     16|   o
     15|   o
     14|   o
     13|   o
     12|   o
     11|   o
     10|   o
      9|   o
      8|   o
      7|   o
      6|   o
      5|   o   o
      4|   o   o o
      3|   o o o o   o
      2|   o o o o   o
      1|   o o o o   o                     o       o
      0|   o o o o o o           o     o   o o   o o                                 o
       |   o o o o o o           o     o   o o   o o                                 o
        --------------------------------------------------------------------------------
    
        5
  •  21
  •   Flimm D. Ben Knoble    4 年前

    feedgnuplot 是gnuplot的另一个前端,它处理数据中的管道。

        $ seq 5 | awk '{print 2*$1, $1*$1}' |
          feedgnuplot --lines --points --legend 0 "data 0" --title "Test plot" --y2 1 \
                      --terminal 'dumb 80,40' --exit
    
                                         Test plot
    
         10 ++------+--------+-------+-------+-------+--------+-------+------*A 25
            +       +        +       +       +       +        +       +    **#+
            |       :        :       :       :       :        : data 0+**A*** |
            |       :        :       :       :       :        :       :** #   |
          9 ++.......................................................**.##....|
            |       :        :       :       :       :        :    ** :#      |
            |       :        :       :       :       :        :  **   #       |
            |       :        :       :       :       :        :**   ##:      ++ 20
          8 ++................................................A....#..........|
            |       :        :       :       :       :      **:   #   :       |
            |       :        :       :       :       :    **  : ##    :       |
            |       :        :       :       :       :  **    :#      :       |
            |       :        :       :       :       :**      B       :       |
          7 ++......................................**......##................|
            |       :        :       :       :    ** :    ##  :       :      ++ 15
            |       :        :       :       :  **   :   #    :       :       |
            |       :        :       :       :**     : ##     :       :       |
          6 ++..............................*A.......##.......................|
            |       :        :       :    ** :     ##:        :       :       |
            |       :        :       :  **   :    #  :        :       :       |
            |       :        :       :**     :  ##   :        :       :      ++ 10
          5 ++......................**........##..............................|
            |       :        :    ** :      #B       :        :       :       |
            |       :        :  **   :    ## :       :        :       :       |
            |       :        :**     :  ##   :       :        :       :       |
          4 ++...............A.......###......................................|
            |       :      **:     ##:       :       :        :       :       |
            |       :    **  :   ##  :       :       :        :       :      ++ 5
            |       :  **    : ##    :       :       :        :       :       |
            |       :**    ##B#      :       :       :        :       :       |
          3 ++.....**..####...................................................|
            |    **####      :       :       :       :        :       :       |
            |  **## :        :       :       :       :        :       :       |
            B**     +        +       +       +       +        +       +       +
          2 A+------+--------+-------+-------+-------+--------+-------+------++ 0
            1      1.5       2      2.5      3      3.5       4      4.5      5
    

    您可以通过运行以下命令在Debian和Ubuntu上安装它 sudo apt install feedgnuplot .

        6
  •  20
  •   denis    11 年前

    单行图非常简单,可以帮助 看见 高点和低点的模式。
    另请参见 pysparklines .
    (有人知道unicode斜线吗,它可以组合在一起做成 线条,而不是条形图?)

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    from __future__ import division
    import numpy as np
    
    __version__ = "2015-01-02 jan  denis"
    
    
    #...............................................................................
    def onelineplot( x, chars=u"▁▂▃▄▅▆▇█", sep=" " ):
        """ numbers -> v simple one-line plots like
    
    f ▆ ▁ ▁ ▁ █ ▁ ▁ ▁ ▁ ▁ ▁ ▁ ▁ ▁ ▁ ▁ ▁ ▁ ▁ ▁  osc 47  ▄ ▁ █ ▇ ▄ ▆ ▅ ▇ ▇ ▇ ▇ ▇ ▄ ▃ ▃ ▁ ▃ ▂  rosenbrock
    f █ ▅ █ ▅ █ ▅ █ ▅ █ ▅ █ ▅ █ ▅ █ ▅ ▁ ▁ ▁ ▁  osc 58  ▂ ▁ ▃ ▂ ▄ ▃ ▅ ▄ ▆ ▅ ▇ ▆ █ ▇ ▇ ▃ ▃ ▇  rastrigin
    f █ █ █ █ ▁ ▁ ▁ ▁ ▁ ▁ ▁ ▁ ▁ ▁ ▁ ▁ ▁ ▁ ▁ ▁  osc 90  █ ▇ ▇ ▁ █ ▇ █ ▇ █ ▇ █ ▇ █ ▇ █ ▇ █ ▇  ackley
    
    Usage:
        astring = onelineplot( numbers [optional chars= sep= ])
    In:
        x: a list / tuple / numpy 1d array of numbers
        chars: plot characters, default the 8 Unicode bars above
        sep: "" or " " between plot chars
    
    How it works:
        linscale x  ->  ints 0 1 2 3 ...  ->  chars ▁ ▂ ▃ ▄ ...
    
    See also: https://github.com/RedKrieg/pysparklines
        """
    
        xlin = _linscale( x, to=[-.49, len(chars) - 1 + .49 ])
            # or quartiles 0 - 25 - 50 - 75 - 100
        xints = xlin.round().astype(int)
        assert xints.ndim == 1, xints.shape  # todo: 2d
        return sep.join([ chars[j] for j in xints ])
    
    
    def _linscale( x, from_=None, to=[0,1] ):
        """ scale x from_ -> to, default min, max -> 0, 1 """
        x = np.asanyarray(x)
        m, M = from_ if from_ is not None \
            else [np.nanmin(x), np.nanmax(x)]
        if m == M:
            return np.ones_like(x) * np.mean( to )
        return (x - m) * (to[1] - to[0]) \
            / (M - m)  + to[0]
    
    
    #...............................................................................
    if __name__ == "__main__":  # standalone test --
        import sys
    
        if len(sys.argv) > 1:  # numbers on the command line, may be $(cat myfile)
            x = map( float, sys.argv[1:] )
        else:
            np.random.seed( 0 )
            x = np.random.exponential( size=20 )
    
        print onelineplot( x )
    
        7
  •  9
  •   Savino Piccolomo    6 年前

    也, spark 是shell中一个很好的小条形图。

        8
  •  7
  •   Tyler    17 年前

    gnuplot 这是你问题的最终答案。

    我个人也是 google chart API ,可以在wget(或curl)的帮助下从命令行访问,下载png文件(并使用xview或类似工具查看)。我喜欢这个选项,因为我发现图表稍微漂亮一些(即更好的抗锯齿)。

        9
  •  6
  •   zdk    7 年前

    gnuplot的另一个更简单/更轻便的替代方案是 ervy ,一个基于NodeJS的终端图表工具。

    支持的类型:散点(XY点)、条形图、饼图、子弹图、圆环图和量规。

    在项目中可以找到各种选项的使用示例 GitHub repo

    enter image description here

    enter image description here

        10
  •  5
  •   Thomas Kammeyer    17 年前

    您应该使用gnuplot,并确保发出命令“set term dumb” 启动后。您还可以给出行数和列数。 如果您发出“set term dumb 64 10”,这是gnuplot的输出 然后“绘制sin(x)”:

     
    
        1 ++-----------****-----------+--***-------+------****--++
      0.6 *+          **+  *          +**   *      sin(x)*******++
      0.2 +*         *      *         **     **         *     **++
        0 ++*       **       *       **       *       **       *++
     -0.4 ++**     *         **     **         *      *         *+
     -0.8 ++ **   *     +      *   ** +         *  +**          +*
       -1 ++--****------+-------***---+----------****-----------++
         -10           -5             0            5             10
    
    
    

    它在79x24时看起来更好(不要在80x24显示器上使用第80列:一些curses实现在最后一列附近并不总是表现良好)。

    我使用的是gnuplot v4,但这应该适用于稍旧或稍新的版本。

        11
  •  5
  •   Daniel Korn    7 年前

    我找到了一个名为 ttyplot 在自制。很好。 https://github.com/tenox7/ttyplot

        12
  •  2
  •   Sankalp    5 年前

    termplotlib (我的一个项目)最近越来越受欢迎,所以也许这对一些人有帮助。

    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
    
        13
  •  1
  •   Max Kosyakov    9 年前

    这是我的eplot补丁,它为终端输出添加了-T选项:

    --- eplot       2008-07-09 16:50:04.000000000 -0400
    +++ eplot+      2017-02-02 13:20:23.551353793 -0500
    @@ -172,7 +172,10 @@
                                            com=com+"set terminal postscript color;\n"
                                            @o["DoPDF"]=true
    
    -                               # ---- Specify a custom output file
    +                               when /^-T$|^--terminal$/
    +                                       com=com+"set terminal dumb;\n"
    +
    +                                # ---- Specify a custom output file
                                    when /^-o$|^--output$/
                                            @o["OutputFileSpecified"]=checkOptArg(xargv,i)
                                            i=i+1
    
                                        i=i+1
    

    使用此功能,您可以将其作为 eplot -T 以获取ASCII图形结果,而不是gnuplot窗口。

        14
  •  1
  •   Nico Schlömer David Maze    5 年前

    街区里的新来的孩子: YouPlot .

    推荐文章