使用matplotlib在Google Colab中显示另一个类中的图形
在google colab中: 我有一个python代码,它使用另一个文件中的类。该类有一个具有以下代码的方法:使用matplotlib在Google Colab中显示另一个类中的图形,matplotlib,google-colaboratory,Matplotlib,Google Colaboratory,在google colab中: 我有一个python代码,它使用另一个文件中的类。该类有一个具有以下代码的方法: def callbacks(): plt.style.use("ggplot") plt.figure() plt.plot(N, self.H["loss"], label="train_loss") #code continues plt.legend() #save the fig
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()
当我运行主文件的代码时,每次都会调用此方法(“回调”的一部分):
model.fit(trainX, trainY, validation_data=(testX, testY),
batch_size=64, epochs=100, callbacks=callbacks, verbose=1)
但我无法在google colab中获得该数字,但它被保存在正确的位置(如预期的那样)
我所做但失败的是:
%matplotlib inline
plt.close()
,图形就会关闭。调用上述代码后显示图形的唯一方法是从正在调用的代码中删除plt.close()
作为最后的手段,如果无法编辑此代码,可以在调用相关代码之前对close函数进行猴子补丁;例如:
plt.close = lambda: None
但是请注意,像这样覆盖核心功能是一个非常糟糕的想法,可能会在其他地方产生意外的副作用。遗憾的是,它对我不起作用。在运行
%matplotlib inline命令之前和之后,我尝试运行代码,但无法显示图形。但是,它正在驱动器中成功保存和更新