Disable text output for dynamic plotting in jupyter-lab

Viewed 111

I have two dynamic plots that get values continuously from a generator. The whole thing looks like this.

for data in generator(a,b):
    plt.clf()
    f, ax = plt.subplots(2, 1, figsize=(15, 12), dpi=90)
    ax[0].plot(data[0])
    ax[1].plot(data[1])
    display.clear_output(wait=True)
    display.display(f)

The problem is that after the execution in jupyter-lab the final plot and countless <Figure size 1350x1080 with 0 Axes> text outputs can be seen. Is there a way to disable this or does anyone know of any other way to create a dynamic plot without text output?

1 Answers

Instead of calling plt.clf() directly, pass clear=True and num=1 to plt.subplots, this will suppress the output from all but the last plot while also clearing the plot each iteration:

for ii in range(5):
    f, ax = plt.subplots(2, 1, num=1, figsize=(15,12), dpi=90, clear=True)
    ax[0].plot(np.ones(10)*ii)
    ax[1].plot(np.ones(10)*ii)

enter image description here

You can also supply a different num for each figure, if you need to redraw multiple.

Related