'Random output when using IPython.display

I'm currently using this code to plot the scores and mean scores for the deep reinforcement snake I've been working on.

plt.ion()
def plot_data(scores, mean_scores, agent_num=0, gen=1):
    display.clear_output(wait=True)
    display.display(plt.gcf())
    plt.clf()

    plt.title(f"Training Agent {agent_num}\n(Generation {gen})")
    plt.xlabel("Number of Games")
    plt.ylabel("Score")

    plt.plot(scores)
    plt.plot(mean_scores)
    plt.text(len(scores)-1, scores[-1], f"Score: {scores[-1]}")
    plt.text(len(mean_scores)-1, mean_scores[-1], f"Mean Score: {mean_scores[-1]}")

    plt.xlim(xmin=0)
    plt.ylim(ymin=0)

    plt.show(block=False)
    plt.pause(0.001)

The code itself works perfectly fine, but it prints Figure(1280x960) to the console every time it runs.

Since I have other things I'm printing to the console, seeing Figure(1280x960) has started to get really annoying. I want to get rid of it, but I can't figure out how to do it. The only thing I'm certain of is that once of this two lines is causing it:

display.clear_output(wait=True)
display.display(plt.gcf())

How do I prevent this unwanted and unwelcome output?



Solution 1:[1]

plt.ion()
def plot_data(scores, mean_scores, agent_num=0, gen=1):
    # plt.clf() # I'm not sure it is neccessary to save memory
    plt.title(f"Training Agent {agent_num}\n(Generation {gen})")
    plt.xlabel("Number of Games")
    plt.ylabel("Score")

    plt.plot(scores)
    plt.plot(mean_scores)
    plt.text(len(scores)-1, scores[-1], f"Score: {scores[-1]}")
    plt.text(len(mean_scores)-1, mean_scores[-1], f"Mean Score: {mean_scores[-1]}")

    plt.xlim(xmin=0)
    plt.ylim(ymin=0)

    plt.show(block=False)
    plt.pause(0.001)
    display.clear_output(wait=True)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kang San Lee