Ipywidgets: How to remove Matplotlib plots flickering when refreshing the output often

Created on 26 May 2019  路  2Comments  路  Source: jupyter-widgets/ipywidgets

I re-use the same figure and axis to plot some data in a succession. In order to display a single figure on the notebook, I need to clear the output. However, this seems to be causing some flickering.

It seems that objects like ipywidgets.interact do not suffer from such flickering.

How can I remove the flickering (without having to resort to ipywidgets.interact)?

import ipywidgets as widgets
import matplotlib.pylab as plt
import numpy as np
import IPython.display as display
%config InlineBackend.close_figures=False 

values = np.cumsum(np.random.randn(100))

plt.ioff()
figsize = (10, 8)
fig, ax = plt.subplots(1, 1, figsize=figsize)
plt.ion()

output = widgets.Output()
def plot(idx):
    with output:
        ax.plot(range(idx), values[:idx])   
        display.clear_output(wait=True)
        display.display(fig)

def on_slider_change(change):
    plot(change['new'])

player = widgets.Play(interval=300, value=0, min=0, max=len(values), step=1, description="Press play", disabled=False)
slider = widgets.IntSlider(min=0, max=len(values), step=1, value=0)
widgets.jslink((player, 'value'), (slider, 'value'))
slider.observe(lambda x: on_slider_change(x), names='value')
display.display(widgets.VBox([widgets.HBox([player, slider]), output]))

Most helpful comment

Same question here.
I also would like to reduce the flickering, for example in this case:

from matplotlib import pyplot as plt
from IPython.display import clear_output
import numpy as np
for i in range(10):
    clear_output(wait=True)
    y = np.random.random([10,1])
    plt.plot(y)
    plt.show()

All 2 comments

Using the matplotlib widget works, and it seems only natural to use a proper widget to display and update your plots. However, I'd still like to know if there is a solution that doesn't involve using the matplotlib widget.

Same question here.
I also would like to reduce the flickering, for example in this case:

from matplotlib import pyplot as plt
from IPython.display import clear_output
import numpy as np
for i in range(10):
    clear_output(wait=True)
    y = np.random.random([10,1])
    plt.plot(y)
    plt.show()
Was this page helpful?
0 / 5 - 0 ratings