使用matplotlib在Google Colab中显示另一个类的图形 [英] Displaying figure from another class in Google Colab using matplotlib

查看:173
本文介绍了使用matplotlib在Google Colab中显示另一个类的图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在google-colab中:

In google-colab:

我有一个python代码,该代码使用另一个文件中的类.该类具有一个包含以下代码的方法:

I have a python code which uses a class from another file. The class has a method which has the following code:

def callbacks():
        plt.style.use("ggplot")
        plt.figure()
        plt.plot(N, self.H["loss"], label="train_loss")
        #code continues
        plt.legend()

        #save the fig
        plt.savefig(self.figPath)
        plt.close()

当我运行主文件的代码时,每次在每个时期都调用此方法(回调"的一部分).

This method( part of "callbacks") is invoked every time in each epoch when I run the code of the main file.:

model.fit(trainX, trainY, validation_data=(testX, testY),
        batch_size=64, epochs=100, callbacks=callbacks, verbose=1) 

但是我无法在google colab中获得该图,但该图已保存在正确的位置(按预期) 我做过但失败了的是:

But I couldn't get the figure in google colab but it is being saved at the correct location(as expected) What I did but failed are:

  1. 在plt.savefig(..)之前使用plt.show()
  2. 在代码开始之前运行%matplotlib inline

在各个时期运行时,应该怎么做才能显示该图? 我希望它们随着纪元显示而运行的原因是,它需要一些时间来更新Google驱动器上的图形

What should be done to dislpay the plot as the epochs are run? The reason I want them to run as the epochs are displayed is because it is taking some time to update the figure on google drive

推荐答案

一旦调用plt.close(),图形将关闭.调用上述代码后,显示图形的唯一方法是从正在调用的代码中删除plt.close().

Once plt.close() is called, the figure is closed. The only way to display the figure after calling the above code is to remove plt.close() from the code you are calling.

作为最后的选择,如果没有办法编辑此代码,则可以在调用有问题的代码之前先猴子关闭函数.例如:

As a last resort, if there's no way to edit this code, you could monkey-patch the close function before calling the code in question; for example:

plt.close = lambda: None

但是请注意,这种压倒一切的核心功能是一个非常糟糕的主意,并且可能会在其他地方产生意想不到的副作用.

but be aware that this overriding core functionality like this is a very bad idea and will likely have unintended side-effects elsewhere.

这篇关于使用matplotlib在Google Colab中显示另一个类的图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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