jupyter-widgets / ipywidgets

Interactive Widgets for the Jupyter Notebook
https://ipywidgets.readthedocs.io
BSD 3-Clause "New" or "Revised" License
3.13k stars 948 forks source link

How to remove Matplotlib plots flickering when refreshing the output often #2419

Open bybyte opened 5 years ago

bybyte commented 5 years ago

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]))
bybyte commented 5 years ago

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.

kolibril13 commented 4 years ago

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()
amarnathreddy0201 commented 2 years ago

Hi, import matplot.pyplot as plt

plt.draw_idle()

with this we will avoid flickering. use above draw_idle i think it will be ok or else R&D is the best way.