The following code refreshes the bar plot whenever the slider is moved
from ipywidgets import widgets
import pandas as pd
import matplotlib.pyplot as plt
slider = widgets.IntSlider(value=0,min=0,max=100,step=1,continuous_update=False)
def run_model(slider):
df = pd.DataFrame({"label":["A","B","C"],"value":[0,slider,100]})
fig,ax = plt.subplots(1,1,figsize=(3,2))
df.plot.bar(ax=ax)
plt.show()
output = widgets.interactive_output(run_model, {"slider":slider})
display(slider,output)
However without the plt.show()
line, the output is appended rather than replaced (not desirable behaviour!), I think because the plot is sent to the Jupyter cell output rather than the (freshly cleared) ipywidgets output.
Why is it necessary to call plt.show()
to achieve a refresh, and furthermore why does it change where the plot has gone after it's already been produced by df.plot
?