question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Matplotlib graphs do not clear in events with clear_output

See original GitHub issue

Hi, I noticed this in 7.0.5, however it used to work in 6.0.0 with just clear_output, before it’s behaviour was changed. Below is a screenshot to replicate it; every time I click the button, the frame is cleared, but the graph is not. I would appreciate it if you can have a look.

Versions

jupyter                   1.0.0            py36h422fd7e_2
jupyter_client            5.1.0            py36h9902a9a_0
jupyter_console           5.2.0            py36h6d89b47_1
jupyter_core              4.3.0            py36h511e818_0
jupyterlab                0.27.0           py36h34cc53b_2
jupyterlab_launcher       0.4.0            py36h22c3ccf_0
jupyterthemes             0.18.2                    <pip>

ipywidgets                7.0.5                    py36_0    conda-forge
widgetsnbextension        3.0.8                    py36_0    conda-forge

matplotlib                2.1.0                     <pip>
pandas                    0.20.3           py36hce827b7_2

Screenshot ipy705

Code

import pandas as pd
import numpy as np
from IPython.display import clear_output
import matplotlib
from matplotlib import pyplot as plt
%matplotlib inline
import ipywidgets as widgets

button = widgets.Button(description="Plot!")
display(button)
out = widgets.Output()
display(out)

def on_button_clicked(b):
    with out:
        clear_output()
        x = pd.DataFrame(np.random.randn(8, 1))
        display(x.T)
        x.plot(figsize=[8, 1])
#         plt.plot(x) --- THIS ALSO CREATED DUPLICATES

button.on_click(on_button_clicked)

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
jasongroutcommented, Dec 5, 2017

What’s happening here is that the matplotlib inline backend makes some assumptions about how to display graphs that don’t mesh well with capturing output. We had to work around this in interact as well at https://github.com/jupyter-widgets/ipywidgets/blob/02f406bd3ce1480ed3df2533f6acc1a4587b52af/ipywidgets/widgets/interaction.py#L75, which calls this function: https://github.com/jupyter-widgets/ipywidgets/blob/02f406bd3ce1480ed3df2533f6acc1a4587b52af/ipywidgets/widgets/interaction.py#L35

So you can use the same workaround we used. Import that function:

from ipywidgets.widgets.interaction import show_inline_matplotlib_plots

then change your function to:

def on_button_clicked(b):
    with out:
        clear_output()
        x = pd.DataFrame(np.random.randn(8, 1))
        display(x.T)
        x.plot(figsize=[8, 1])
        show_inline_matplotlib_plots()

Even better would be to use the matplotlib widget, which lets you update a matplotlib graph, respond to user input, etc: https://github.com/matplotlib/jupyter-matplotlib

0reactions
bybytecommented, May 22, 2019

I was referring to the workaround not working for me. Yes, I understand – I will post in a new thread. Thanks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Clear A Plot In Python (without closing the window)
This article focuses on how to clear a plot by clearing the current Axes and Figure state of a plot, without closing the...
Read more >
Clear plots generated by ipywidgets.observe - Stack Overflow
Try sending your charts to an Output widget, which can be used as a context manager, and then cleared with out.clear_output() .
Read more >
Matplotlib.figure.Figure.clear() in Python - GeeksforGeeks
The clear() method figure module of matplotlib library is used to Clear the figure. ... Returns: This method does not return any value....
Read more >
Messaging in Jupyter — jupyter_client 7.4.8 documentation
There are a number of actions in Python which generate side effects: print() ... This dictionary structure is not part of the Jupyter...
Read more >
Event handling and picking — Matplotlib 3.6.2 documentation
This does not affect free functions used as callbacks. ... On the button release event, just reset all the button press data you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found