如何在远程计算机上保存实时数据的图?

TJa*_*ain 9 python matplotlib

我想通过sshing和检查图来了解我的模型在训练时的表现(即实时数据方式).

animation.FuncAnimation每次在我的本地机器上更新时,使用我能够保存(和覆盖)一个帧,如下所示:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

def animate(i):
    fig.clf()

    plt.suptitle('Title here')

    # actually parse model outputs and plot
    values = np.random.random_integers(0, 100, (100,))
    plt.plot(np.arange(len(values)), values, label='random')
    plt.ylabel('demo data')
    plt.grid()


    plt.legend()
    plt.xlabel('Epochs')
    fig.savefig('Figure.png')


fig = plt.figure()
ani = animation.FuncAnimation(fig, animate, interval=10*60*1000)
plt.show()
Run Code Online (Sandbox Code Playgroud)

在本地机器上使用它很好,因为plt.show调用$ DISPLAY.但是,当在远程服务器上运行时(ssh当然是通过),因为没有显示,我得到了RuntimeError: Invalid DISPLAY variable.当使用像svgvia 这样的其他后端时matplotlib.use('svg').脚本退出而不实际保存任何图像.

另外,我决定在函数中使用plt.show()fig.savefig('Figure.png')内部animate是因为在plt.show()调用之后没有函数FuncAnimation,它不会运行animate给定的间隔.我尝试过做plt.savefig.

关于fig.savefig('Figure.png'),在animate功能之外这样做导致空白图像.我猜是因为我在animate功能开始时清除了图像.

所以,我的问题是:有没有办法在ssh 上使用animation(或FuncAnimation)像这样保存在实时数据上生成的数字直到某些事件发生(或者可能是超时)?

576*_*76i 1

“animation.FuncAnimation”背后的想法是动画函数正在调用函数来写入/更新无花果。

您似乎想要远程生成“png”文件并通过 SSH 或其他方式获取它。

在两台机器之间共享“无花果”是行不通的。

如果您在远程计算机上运行“animate”函数,请将 Fig = plt.figure() 移至函数中,而不是“fig.clf()”

def animate(i):
    fig = plt.figure()

....
Run Code Online (Sandbox Code Playgroud)

虽然可以编写连接到远程计算机的 SSH 代码,但我可能会在为每个框架提供服务的远程计算机上运行一个简单的 Web 服务器。

这样您就可以调用 http:///server_frame?index=i 来加载框架。

如果您是初学者并且没有巨大的性能要求,则可以使用cherrypy(https://cherrypy.org/)轻松完成此操作。关于如何提供图像的示例问题如下:如何从cherrypy提供多个matplotlib图像?

在本地端,您现在可以编写一个新的“get_frame”函数,该函数下载框架并将其放入本地Fig。

ani = animation.FuncAnimation(fig, get_frame, interval=10*60*1000)
Run Code Online (Sandbox Code Playgroud)