Matplotlib graphs do not clear in events with clear_output
See original GitHub issueHi, 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
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:
- Created 6 years ago
- Comments:7 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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:
then change your function to:
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
I was referring to the workaround not working for me. Yes, I understand – I will post in a new thread. Thanks.