Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在colab中运行py文件时,使用%matplotlib笔记本或%matplotlib笔记本无法工作_Python_Matplotlib_Ipython_Google Colaboratory - Fatal编程技术网

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