Python 在colab中运行py文件时,使用%matplotlib笔记本或%matplotlib笔记本无法工作
我有一个名为Python 在colab中运行py文件时,使用%matplotlib笔记本或%matplotlib笔记本无法工作,python,matplotlib,ipython,google-colaboratory,Python,Matplotlib,Ipython,Google Colaboratory,我有一个名为utils.py的文件。在该文件中,我有一个名为plot\u results的函数,定义如下: def plot_results(results, epochs): """ The function to show results on each epoch. Parameters: results (keras.history): History of each epoch. It comes directly
utils.py
的文件。在该文件中,我有一个名为plot\u results
的函数,定义如下:
def plot_results(results, epochs):
"""
The function to show results on each epoch.
Parameters:
results (keras.history): History of each epoch. It comes directly from keras.
epochs (int): The number of epochs.
"""
_, (ax1, ax2) = plt.subplots(1, 2)
ax1.set_xlabel("Epochs")
ax1.set_ylabel("Losses")
ax1.plot(
range(1, epochs+1),
results.history['val_loss'],
label="Validation loss",
marker='o')
ax1.plot(
range(1, epochs+1),
results.history['loss'],
label="loss",
marker='o')
ax1.legend()
ax2.set_xlabel("Epochs")
ax2.set_ylabel("Accuracies")
ax2.plot(
range(1, epochs+1),
[accuracy * 100 for accuracy in results.history['accuracy']],
label="Accuracy",
marker='o')
ax2.plot(
range(1, epochs+1),
[accuracy * 100 for accuracy in results.history['val_accuracy']],
label="validation accuracy",
marker='o')
ax2.legend()
plt.show()
我还有一个名为main.py
的文件,在该文件中我调用plot\u results
。当我在本地机器中运行main.py
时,我正确地看到了绘图
但当我在google colab单元中运行它时:
! python main.py --ne 1
我刚收到
据我所说:
以及:
以及:
但它们都不起作用。
如何在该函数中显示绘图?试试这个
%run main.py
它将像一行一行地运行一样工作
%matplotlib notebook
! python main.py --ne 1
%matplotlib inline
%matplotlib notebook
! python main.py --ne 1
%run main.py