2012-08-17 47 views
1

我一直在試圖繪製一個函數可以變化,看看不同的行爲兩個參數。我想用滑塊來改變參數。matplotlib滑塊參數

我在尋找我所遇到的是改變軸而不是一個數學函數的部分滑塊。

所以我有下面的代碼,如果我的兩個參數的Gmax和Km是軸這應該工作:

from matplotlib.widgets import Slider 
    import numpy as np 

    Gmax=1 
    Km= 1 

    def f(S): 
     s1 = Gmax*S #G_max 
     e1 = S + Km #K_m 
     return divide(s1,e1) 

    S=arange(0,100,0.1) 

    ax = subplot(111) 
    subplots_adjust(left=0.15, bottom=0.25) 
    l = plot(f(S)) 
    grid(False) 
    title('Playing with sliders') 
    xlabel('time') 
    ylabel('concentration') 


    axcolor = 'lightgoldenrodyellow' 
    axGmax = axes([0.15, 0.1, 0.65, 0.03], axisbg=axcolor) 
    axKm = axes([0.15, 0.15, 0.65, 0.03], axisbg=axcolor) 

    sGmax = Slider(axGmax, 'Gmax', 0.1, 3.0, valinit=1) 
    sKm = Slider(axKm, 'Km', 0.01, 1.0, valinit=1) 

    def update(val): 
     s1 = Gmax*S * sGmax.val 
     e1 = S + Km * sKm.val 
     l.set_ydata(y)  
     ax.set_ylim(y.min(), y.max()) 
     draw() 

    sGmax.on_changed(update) 
    sKm.on_changed(update) 

    show() 

所以我想我的問題是,如果有一個參數,而不是斧頭命令的命令軸滑塊? 或者,如果有這樣做的另一種方式?

+0

我不知道如何與matplotlib做到這一點,但對於交互式繪圖這樣我會強烈建議使用查科+性狀。下面是從他們的文檔滑塊一個類似的例子:http://code.enthought.com/projects/chaco/docs/html/user_manual/tutorial_2.html – reptilicus 2012-08-17 14:24:25

回答

3

你的代碼幾乎是正確的,但你應該改變l = plot(f(S))l, = plot(f(S))因爲情節()返回一個列表。然後您可以撥打l.set_ydata(...)來設置新值。

下面是代碼:

from matplotlib.widgets import Slider 
from pylab import * 

def f(S, Gmax, Km): 
    s1 = Gmax*S #G_max 
    e1 = S + Km #K_m 
    return divide(s1,e1) 

S=arange(0,100,0.1) 

ax = subplot(111) 
subplots_adjust(left=0.15, bottom=0.25) 
l, = plot(f(S, 1.0, 1.0)) 
grid(False) 
title('Playing with sliders') 
xlabel('time') 
ylabel('concentration') 

axcolor = 'lightgoldenrodyellow' 
axGmax = axes([0.15, 0.1, 0.65, 0.03], axisbg=axcolor) 
axKm = axes([0.15, 0.15, 0.65, 0.03], axisbg=axcolor) 

sGmax = Slider(axGmax, 'Gmax', 0.1, 3.0, valinit=1) 
sKm = Slider(axKm, 'Km', 0.01, 1.0, valinit=1) 

def update(val): 
    l.set_ydata(f(S, sGmax.val, sKm.val)) 

sGmax.on_changed(update) 
sKm.on_changed(update) 

show()