How to plot a chart in the terminal?

后端 未结 7 1274
面向向阳花
面向向阳花 2020-12-25 09:13

I\'m researching ML/Theano, and recently came across this script: https://gist.github.com/notmatthancock/68d52af2e8cde7fbff1c9225b2790a7f which was cool to play with. And l

相关标签:
7条回答
  • 2020-12-25 09:29

    termplotlib (a small project of mine) might come in handy here. Install with

    pip install termplotlib
    

    and produce terminal plots like

    import termplotlib as tpl
    import numpy as np
    
    x = np.linspace(0, 2*np.pi, 100)
    y = np.sin(x) + x
    fig = tpl.figure()
    fig.plot(x, y, width=60, height=20)
    fig.show()
    
      7 +---------------------------------------------------+
        |                                                   |
      6 |                                             **    |
        |                                           **      |
        |                                         **        |
      5 |                                       **          |
        |                                     ***           |
      4 |                                  ****             |
        |                              *****                |
      3 |             *****************                     |
        |          ****                                     |
      2 |       ***                                         |
        |     ***                                           |
        |   ***                                             |
      1 |  **                                               |
        |**                                                 |
      0 +---------------------------------------------------+
        0      1       2      3       4      5       6      7
    
    0 讨论(0)
  • 2020-12-25 09:29

    It seems to me that terminalplot, which is much more complete than the package suggested by @William234234 might be a good solution.

    Example usage:

    import terminalplot as tp
    import numpy as np
    from math import sin, pi
    
    x=np.linspace(0,2*pi,100);
    y=[sin(m)+m for m in x];
    tp.plot(list(x),y)
    

    0 讨论(0)
  • 2020-12-25 09:39

    You have a couple of options here:

    1. Export to image or PDF. Information found here: http://matplotlib.org/faq/howto_faq.html The key piece of information here is below:

      # do this before importing pylab or pyplot
      import matplotlib
      matplotlib.use('Agg')
      import matplotlib.pyplot as plt
      fig = plt.figure()
      ax = fig.add_subplot(111)
      ax.plot([1,2,3])
      fig.savefig('test.png')
      
    2. If your server supports X11 forwarding (or if you can enable/install X11 forwarding), you can SSH into the server by setting your display. From linux, run:

      DISPLAY=:0.0 ssh -Y <server ip>
      

      This will set up your machine to forward any display output from the server to your PC. If you are running Windows, you can use MobaXterm which makes it easy, or configure an X11 client yourself. Mac is similarly easy if I remember correctly.

    0 讨论(0)
  • 2020-12-25 09:40

    It is possible to plot raster images in terminals and terminal emulators:

    import matplotlib
    matplotlib.use('module://matplotlib-sixel')
    from pylab import *
    plt.plot(sin(arange(100) / 10))
    show()
    

    This particular example uses matplotlib-sixel, a library that uses Xterm emulating a Sixel compatible terminal and ImageTrick. Similar technology could be implemented in the Linux terminal (through the Framebuffer) or emulators (kitty or iTerm2). The FOSS community has given great solutions in the last years (like libsixel).

    Another option would be using X11 forwarding or using a Sixel-based printer like lsix. But all these options would happens outside the Python shell itself.

    Of course, you are probably better off running a Jupyter Notebook in the server than trying to shoehorn an image in a terminal. It is probably not worth it.

    0 讨论(0)
  • 2020-12-25 09:45

    Check the package plotext which allows to plot data directly on terminal using python3. It is very intuitive as its use is very similar to the matplotlib package.

    Here is a basic example:

    You can install it with the following command:

    sudo -H pip install plotext
    

    As for matplotlib, the main functions are scatter (for single points), plot (for points joined by lines) and show (to actually print the plot on terminal). It is easy to specify the plot dimensions, the point and line styles and whatever to show the axes, number ticks and final equations, which are used to convert the plotted coordinates to the original real values.

    Here is the code to produce the plot shown above:

    import plotext.plot as plx
    import numpy as np
    
    l=3000
    x=np.arange(0, l)
    y=np.sin(4*np.pi/l*np.array(x))*np.exp(-0.5*np.pi/l*x)
    
    plx.scatter(x, y, rows = 17, cols = 70)
    plx.show(clear = 0)
    

    The option clear=True inside show is used to clear the terminal before plotting: this is useful, for example, when plotting a continuous flow of data. An example of plotting a continuous data flow is shown here:

    The package description provides more information how to customize the plot. The package has been tested on Ubuntu 16 where it works perfectly. Possible future developments (upon request) could involve extension to python2 and to other graphical interfaces (e.g. jupiter). Please let me know if you have any issues using it. Thanks.

    I hope this answers your problem.

    0 讨论(0)
  • 2020-12-25 09:47

    If you want to pop an external window with the chart, run the plot and then

    >>> matplotlib.pyplot.show(block=True)

    This will pop the chart in a separate window.

    If you call plot() several times prior to this call, it will then pop an equal amount of windows with the respective charts. Control returns to Python only when you close all popped chart windows.

    I like to wrap it in a little helper function, like so:

    def show():
       return matplotlib.pyplot.show(block=True) 
    

    Then I just call show() whenever I want to see any yet unshown plots.

    0 讨论(0)
提交回复
热议问题