How to change multiple interactive plots with a single Python login

I am trying to make some interactive plots with the ipywidgets library in a Jupyter Notebook. I am facing one problem: I am trying to change multiple graphs when one input widget, like a slider, changes.

In the added MWE, this is illustrated by a sine and cosine function that depends on a single input widget that sets the amplitude of both the sine and the cosine function. Thus, if the value of the input slider is set to a value of 2, the amplitude of both the sine and cosine functions should immediately follow suit.

If you run the code yourself, you can see that both sliders move perfectly together. But not both graphs change at the same time accordingly.

Does anyone have a solution for this? Not necessarily with ipywidgets?

Thank you in advance,

Rick

# FIRST JUPYTER NOTEBOOK CELL
import matplotlib.pyplot as plt
from ipywidgets import *
from math import * 
import numpy as np
%matplotlib inline

default_value = 1  # Default value of amplutide 

a = IntSlider(min = 1, max = 10, value = default_value, description = 'Amplitude of sine function')
b = IntSlider(min = 1, max = 10, value = default_value, description = 'Amplitude of sine function')

mylink = jslink((a, 'value'), (b, 'value'))

def widget_sine_function(amp_s = a):
  x = np.linspace(0,20,100000)  # Creat x-values for sine function
  y = [amp_s*sin(i) for i in x]  # Create y-values for sine function with amplitude according to value of widget a
  plt.clf()
  plt.figure(figsize=(15,5))
  plt.subplot(1, 2, 1)
  plt.plot(x,y)

interact(widget_sine_function)

# SECOND JUPYTER NOTEBOOK CELL
def widget_cosine_function(amp_c = b):
  x = np.linspace(0,20,100000)  # Creat x-values for cosine function
  y = [amp_c*cos(i) for i in x]  # Create y-values for cosine function with amplitude according to value of widget b
  plt.clf()
  plt.figure(figsize=(15,5))
  plt.subplot(1, 2, 1)
  plt.plot(x,y)

interact(widget_cosine_function)

      

+3
python jupyter-notebook ipywidgets


source to share


No one has answered this question yet

See similar questions:

2
How do I make one slider / widget update multiple charts in Bokeh / Python / Pywidgets?

or similar:

4268
How to combine two dictionaries in one expression?
3790
How can I safely create a subdirectory?
2601
How can I make a time delay in Python?
2568
How to find the current time in Python
2112
How do I copy a file in Python?
2047
How do I concatenate two lists in Python?
2005
How do I parse a string to float or int?
1782
How can I get the number of items in a list?
1746
How do I write a lowercase string in Python?
3
How to sort key names in ipywidgets interactive widgets (slider)?



All Articles
Loading...
X
Show
Funny
Dev
Pics