python - 如何在终端中绘制图表

标签 python python-2.7 matplotlib command-line theano

我正在研究 ML/Theano,最近遇到了这个脚本:https://gist.github.com/notmatthancock/68d52af2e8cde7fbff1c9225b2790a7f玩起来很酷。和所有 ML 研究人员一样,我最近升级到服务器,虽然它更强大,但也给我带来了问题。

脚本很长,但以这段代码结束:

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)

生成此图表:

enter image description here 当我试图在服务器上运行它时,服务器没有屏幕只有命令行,我可以预见到这个错误:

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

有没有在命令行中显示图表和图形的方式/方法/功能?

最佳答案

termplotlib (我的一个小项目)在这里可能会派上用场。安装

pip install termplotlib

并生成像这样的终端图

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

关于python - 如何在终端中绘制图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37288421/

相关文章:

python - pyowm 调用在自己的程序中有效,但在从其他程序调用时无效

python - 如何测试变量的类型?

python - py2exe 中的 Matplotlib — ImportError : cannot import name dist (File "distutils\__init__.pyc")

numpy - 如何获得 0 和 1 之间的 sigmoid 函数以获得正确答案的概率?

python - 如何在django中传递绝对url

Python包导入困惑

python - 如何读取位图文件并在之后裁剪它?

python - 强制 ElementTree 使用结束标签

python-2.7 - 在kivy python中在屏幕之间交换变量

python - 在matplotlib中自动缩放y时设置x轴标签范围