0

I've stuck with an issue that I have a slider but It's not interactive, I followed by documentation but even ready solution didn't worked, how to solve that? Not worked in Google Colab and Jupyter Notebook. I have already tried to change matplotlib backend kernel from qt to ktinker but nothing

my code :

%matplotlib inline !pip install --upgrade matplotlib import numpy as np import matplotlib.pyplot as plt from matplotlib.widgets import Slider, Button # The parametrized function to be plotted def reliability_graph (t,mu_,lambda_): return (mu_/(lambda_+mu_))+(lambda_/(lambda_+mu_))*np.exp(-(lambda_+mu_)*t) t = np.linspace(0, 10,1000) # Define initial parameters init_mu = 0 init_lambda = 0.1 # Create the figure and the line that we will manipulate fig, ax = plt.subplots() line, = plt.plot(t, reliability_graph (t,init_mu,init_lambda), lw=2) ax.set_xlabel('Relibility') # adjust the main plot to make room for the sliders plt.subplots_adjust(left=0.25, bottom=0.25) # Make a horizontal slider to control the frequency. axmu = plt.axes([0.25, 0.1, 0.65, 0.03]) mu_slider = Slider( ax=axmu, label='Mu', valmin=0, valmax=1, valinit=init_mu, ) # Make a vertically oriented slider to control the amplitude axlambda = plt.axes([0.1, 0.25, 0.0225, 0.63]) lambda_slider = Slider( ax=axlambda, label="Lambda", valmin=0, valmax=1, valinit=init_lambda, orientation="vertical" ) # The function to be called anytime a slider's value changes def update(val): line.set_ydata(reliability_graph(t, mu_slider.val, lambda_slider.val)) fig.canvas.draw_idle() # register the update function with each slider mu_slider.on_changed(update) lambda_slider.on_changed(update) # Create a `matplotlib.widgets.Button` to reset the sliders to initial values. resetax = plt.axes([0.8, 0.025, 0.1, 0.04]) button = Button(resetax, 'Reset', hovercolor='0.975') def reset(event): mu_slider.reset() lambda_slider.reset() button.on_clicked(reset) plt.show() 

My aim is to get a graph with working sliders ,so that I can change values of my parameters interactively. The problem is that sliders aren’t works, they appears as a picture not an interactive object

1
  • 1
    Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. Commented Feb 26, 2022 at 22:02

1 Answer 1

1

I've never used sliders or buttons in Colab, but by running your code and introducing the two-point library from the error message, the graph and slider buttons are now enabled.

#!pip install ipympl #from google.colab import output #output.enable_custom_widget_manager() import numpy as np import matplotlib.pyplot as plt from matplotlib.widgets import Slider, Button %matplotlib widget def reliability_graph (t,mu_,lambda_): return (mu_/(lambda_+mu_))+(lambda_/(lambda_+mu_))*np.exp(-(lambda_+mu_)*t) t = np.linspace(0, 10,1000) # Define initial parameters init_mu = 0 init_lambda = 0.1 # Create the figure and the line that we will manipulate fig, ax = plt.subplots() line, = plt.plot(t, reliability_graph (t,init_mu,init_lambda), lw=2) ax.set_xlabel('Relibility') # adjust the main plot to make room for the sliders plt.subplots_adjust(left=0.25, bottom=0.25) # Make a horizontal slider to control the frequency. axmu = plt.axes([0.25, 0.1, 0.65, 0.03]) mu_slider = Slider( ax=axmu, label='Mu', valmin=0, valmax=1, valinit=init_mu, ) # Make a vertically oriented slider to control the amplitude axlambda = plt.axes([0.1, 0.25, 0.0225, 0.63]) lambda_slider = Slider( ax=axlambda, label="Lambda", valmin=0, valmax=1, valinit=init_lambda, orientation="vertical" ) # The function to be called anytime a slider's value changes def update(val): line.set_ydata(reliability_graph(t, mu_slider.val, lambda_slider.val)) fig.canvas.draw_idle() # register the update function with each slider mu_slider.on_changed(update) lambda_slider.on_changed(update) # Create a `matplotlib.widgets.Button` to reset the sliders to initial values. resetax = plt.axes([0.8, 0.025, 0.1, 0.04]) button = Button(resetax, 'Reset', hovercolor='0.975') def reset(event): mu_slider.reset() lambda_slider.reset() button.on_clicked(reset) plt.show() 

enter image description here

Sign up to request clarification or add additional context in comments.

7 Comments

When I press the link Colab above and run the notebook I get an error in the second cell at line 23.... Has something changed in colab since this was posted? Or some other error? Very interested to get this to work.
@janpeter The first three lines are commented out, but can be executed by uncommenting them. I removed the link in the comment. Thanks for pointing this out.
I copied the file in your example and run it in my own Google Drive / Colab. And included the three first lines as you said. Still does not work... looks like something simple though. Error at line 20 : fig, ax = plt.subplots()
Did you install correctly by executing the first three lines? Do you see an error statement, but the following on the last line? Successfully installed fonttools-4.33.3 ipympl-0.9.1 matplotlib-3.5.2
I execute the first three lines. But do not get "Successfully....", still no error message here. Seems hard to add a screenshot here.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.