如何在终端上绘制图表? [英] How to plot a chart in the terminal?

查看:84
本文介绍了如何在终端上绘制图表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究ML / Theano,最近遇到了这个脚本:
当我尝试在服务器上运行它时,它作为服务器没有屏幕,只有命令行,我可以预料到此错误:

  fedora @ ip-173-33-18-911:〜/ scripting / spiral $ python spiral.py 
2000/2000迭代,损失:0.172083
Tra ceback(最近一次通话):
文件 spiral.py,第133行,在< module>中
plot_stuff(输入,输出,损失,net_func,n_hidden)
文件 spiral.py,行110,位于plot_stuff
图,轴= plt.subplots(1,2,figsize = (12,6))
文件 /usr/lib/pymodules/python2.7/matplotlib/pyplot.py,第1046行,在子图中
图=图(** fig_kw)
文件 /usr/lib/pymodules/python2.7/matplotlib/pyplot.py,第423行,在图中
** kwargs)
文件 /usr/lib/pymodules/python2.7 /matplotlib/backends/backend_tkagg.py,第79行,在new_figure_manager
中,返回new_figure_manager_given_figure(num,图)
文件 /usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py ,在new_figure_manager_given_figure
窗口中的第87行= Tk.Tk()
文件 /usr/lib/python2.7/lib-tk/Tkinter.py,在__init__ $ b $中的第1767行b self.tk = _tkinter.create(screenName,baseName,className,interactive,wantobjects,useTk,sync,use)
_tkinter.TclError:没有显示名称,没有$ DISPLAY环境变量

有没有一种方法/方法/功能可以在命令行中显示图表?

解决方案

您在这里有两个选择:


  1. 导出为图像或PDF。可在此处找到信息: http://matplotlib.org/faq/howto_faq.html 此处的信息如下:

     #在导入pylab或pyplot 
    之前执行此操作,请导入matplotlib
    matplotlib.use ('Agg')
    导入matplotlib.pyplot as plt
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.plot([1,2 ,3])
    图.savefig('test.png')


  2. 如果您的服务器支持X11转发(或者您可以启用/安装X11转发),则可以通过设置显示方式SSH进入服务器。在Linux上,运行:

      DISPLAY =:0.0 ssh -Y< server ip> 

    这将设置您的计算机以将任何显示输出从服务器转发到PC。如果您正在运行Windows,则可以使用MobaXterm简化操作,也可以自己配置X11客户端。如果我没记错的话,Mac同样容易。



I'm researching ML/Theano, and recently came across this script: https://gist.github.com/notmatthancock/68d52af2e8cde7fbff1c9225b2790a7f which was cool to play with. And like all ML researchers, I recently upgraded to a server, and while it's more powerful, it also presented me with a problem.

The script is very long, but it ends with this code:

def plot_stuff(inputs, outputs, losses, net_func, n_hidden):
fig,axes = plt.subplots(1,2,figsize=(12,6))

    axes[0].plot(np.arange(losses.shape[0])+1, losses)
    axes[0].set_xlabel('iteration')
    axes[0].set_ylabel('loss')
    axes[0].set_xscale('log')
    axes[0].set_yscale('log')

    x,y = np.mgrid[inputs[:,0].min():inputs[:,0].max():51j, inputs[:,1].min():inputs[:,1].max():51j]
    z = net_func( np.c_[x.flatten(), y.flatten()] ).reshape(x.shape)

    axes[1].contourf(x,y,z, cmap=plt.cm.RdBu, alpha=0.6)
    axes[1].plot(inputs[outputs==0,0], inputs[outputs==0,1], 'or') 
    axes[1].plot(inputs[outputs==1,0], inputs[outputs==1,1], 'sb') 
    axes[1].set_title('Percent missclassified: %0.2f%%' % (((net_func(inputs)>0.5) != outputs.astype(np.bool)).mean()*100))

    fig.suptitle('Shallow net with %d hidden units'%n_hidden)
    plt.show()

if __name__=='__main__':
    n_hidden = 40
    inputs, outputs = gen_data(n_samples_per_class=100)
    losses, net_func = train_neural_network(inputs=inputs, outputs=outputs, n_hidden=n_hidden, n_iters=int(2000), learning_rate=0.1)
    plot_stuff(inputs, outputs, losses, net_func, n_hidden)

Which generates this chart:

And when I tried to run it on the server, which being a sever has no screen only a command line, I predictably got this error:

fedora@ip-173-33-18-911:~/scripting/spiral$ python spiral.py
Iteration 2000 / 2000, Loss: 0.172083
Traceback (most recent call last):
  File "spiral.py", line 133, in <module>
    plot_stuff(inputs, outputs, losses, net_func, n_hidden)
  File "spiral.py", line 110, in plot_stuff
    fig,axes = plt.subplots(1,2,figsize=(12,6))
  File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 1046, in subplots
    fig = figure(**fig_kw)
  File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 423, in figure
    **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 79, in new_figure_manager
    return new_figure_manager_given_figure(num, figure)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 87, in new_figure_manager_given_figure
    window = Tk.Tk()
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1767, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable

Is there a way/method/function to display charts and graphs in the command line?

解决方案

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.

这篇关于如何在终端上绘制图表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆