I would like to create fig, ax objects outside of a function, then modify the ax from within the function. This approach works fine when my function is a typical python function, but fails when the function is decorated with the @interact or is wrapped by interact from ipywidgets. I'm curious why the wrapping with interact affects ability to pass ax through.
from ipywidgets import interact
from matplotlib import pyplot as plt
import numpy as np
fig, ax = plt.subplots(1)
def mod_ax(n=10):
x,y = np.random.rand(n), np.random.rand(n)
ax.scatter(x,y)
return
# The interact wrapped function will not modify ax
interact(mod_ax, n=20)
# However, the below line works fine to modify ax;
# comment out the interact line and comment in below line to see modified plot
# mod_ax(n=50)
plt.show()
When you use the interact wrapped function, it doesn't modify ax. I've tried passing the ax around as an argument, but still no luck. It works if you create fig, ax inside of the wrapped function, or just use plt.plot instead of ax.plot, but I want to be able to create the fig, ax outside of the wrapped function and then only modify the content inside to speed up the render time on changes to the interact slider. Any advice would be appreciated.