Issue
I'm using ipywidges to create a plot. I'd like to have a dropdown with options (e.g. Scenario A, Scenario B). Each scenario should change the slider position (a=0, b=1), and one should be able to modify the parameters freely afterwards. Any ideas?
Here is my toy example:
import ipywidgets as widgets
def line(a=0,b=1):
#fig, ax = plt.subplots(figsize=(6, 4))
x = np.arange(-10,10)
y = a+xrange*b
plt.xlim((-10,10))
plt.ylim((-10,10))
plt.plot(x, y)
widgets.interact(line, a=(-10,10,0.1), b=(-10,10,0.1))
I was playing with an additional wrapper functions but with no success. In reality of course, I would like to have a few more scenarios and a lot more parameters.
Solution
All right, I think I've found a very nice workaround:
def line(a=0,b=0):
x = np.arange(-10,10)
y = a+x*b
plt.xlim((-10,10))
plt.ylim((-10,10))
plt.plot(x, y)
sliders = widgets.interact(a=(-10,10,0.1), b=(-10,10,0.1))
def test(chose_defaults):
if chose_defaults=="a":
@sliders
def h(a=5,b=5):
return(line(a,b))
if chose_defaults=="b":
@sliders
def h(a=0,b=1):
return(line(a,b))
widgets.interact(test, chose_defaults=["a","b"])
The above code basically nests two widgets. Firstly a separate widget for chosing a scenario is shown; action for the scenarios are the plots that differ only in the default setup.
Answered By - Djpengo
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.