user3415907
user3415907

Reputation: 11

Changing the parameters of a function using sliders on python matplotlib

I'm working on a Hodgking-Huxley model of a neuron and I like to create a slider to see the results produced by changing some fixed parameters like maximal conductances. The plot is V vs t, which both are arrays, V is computed using an iteration that include the parameters I'd like to play with. After some time I created a slider, but I can make it to change the parameter defined. I've seen some examples where set_ydata is used, but they provide the complete Y-axis function as an argument, which (I think) is not posible in my case.

This is how I calculate V, being the first parameters the ones I want to change and the last part is the slider:

#Modelo de Hodgkin-Huxley


import pylab as pl
import numpy as np

A = 1

  for i in range(1,len(time)):
    dV= A*V[i-1]
    V[i] = V[i-1]+dV
pl.clf()
pl.subplot(311)
pl.title('Hodgkin-Huxley Model')
l, = pl.plot(time,V)

def update(val):
    l.set_ydata(V)
    A = sA.val

axA = pl.axes([0.13, 0.02, 0.75, 0.02])
sA = pl.Slider(axA, "A", 0, 200, valinit=A, color='#AAAAAA')
sA.on_changed(update)

The point is, I can create the slider, but when I use it, nothing changes in the plot.

Upvotes: 1

Views: 5130

Answers (1)

Carlo M.
Carlo M.

Reputation: 331

Does this example work for you (see the top-rated answer).

They propose something similar to what you have done but here are the differences:

from pylab import *
from matplotlib.widgets import Slider

#define the plot objects
#TODO

#define the update method
def update(val):
   #do your update here
   pass

#create the slider
samp = Slider(axamp, 'Amp', 0.1, 10.0, valinit=a0)
samp.on_changed(update)

The reason yours might not work is because you aren't directly importing the Slider object. I hope this is of some use!

Upvotes: 1

Related Questions