【问题标题】:Using %matplotlib notebook or %matplotlib notebook does not work when run py file in colab在 colab 中运行 py 文件时,使用 %matplotlib notebook 或 %matplotlib notebook 不起作用
【发布时间】:2020-11-01 00:31:42
【问题描述】:

我有一个名为utils.py 的文件。在该文件中,我有一个名为 plot_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_results。当我在本地机器上运行 main.py 时,我正确地得到了可视化的情节。

但是当我在 google colab 单元格中运行它时:

! python main.py --ne 1

我刚收到<Figure size 640x480 with 2 Axes> 根据this post我试过了:

%matplotlib inline
! python main.py --ne 1

还有:

%matplotlib notebook
! python main.py --ne 1

还有:

%matplotlib inline
%matplotlib notebook
! python main.py --ne 1

但它们都不起作用。 如何在该函数中显示绘图?

【问题讨论】:

    标签: python matplotlib ipython google-colaboratory


    【解决方案1】:

    试试这个

    %run main.py
    

    它会像逐行运行一样工作。

    【讨论】:

      猜你喜欢
      • 2017-09-18
      • 1970-01-01
      • 1970-01-01
      • 2020-07-21
      • 2014-10-09
      • 2020-12-12
      • 2014-02-06
      • 2018-06-23
      • 2017-05-05
      相关资源
      最近更新 更多