Why Jupyter Notebook creates duplicate graphs when creating graphs

I am trying to make graphs in a Jupyter Notebook that update every second or so. Right now I have a simple code that works:

%matplotlib inline
import time
import pylab as plt
import numpy as np
from IPython import display

for i in range(10):
    plt.close()
    a = np.random.randint(100,size=100)
    b = np.random.randint(100,size=100)

    fig, ax = plt.subplots(2,1)
    ax[0].plot(a)
    ax[0].set_title('A')
    ax[1].plot(b)
    ax[1].set_title('B')

    display.clear_output(wait=True)
    display.display(plt.gcf())
    time.sleep(1.0)

Which updated the graphics that I created every second. However, at the end, there is an additional copy of the graphs:

enter image description here

Why is he doing this? And how can I make this not happen? Thank you in advance.

+4
source share
1 answer

The backend is inlineconfigured so that when each cell is finished, any matplotlib chart created in the cell will be displayed.

, display, .

- plt.close() .

+3

All Articles