matplotlib滑块的参数 [英] matplotlib slider for parameters

查看:109
本文介绍了matplotlib滑块的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图用两个参数来绘制函数,这些参数可以改变以查看不同的行为.我想使用滑块来更改参数.

I have been trying to graph a function with two parameters that can be varied to see different behavior. I would like to use a slider to vary the parameters.

在搜索中,我遇到了可以更改轴但不改变数学函数部分的滑块.

In my search I have come across sliders that change the axes but not parts of a mathematical function.

所以我有以下代码,如果我的两个参数Gmax和Km是轴,该代码应该工作:

So I have the following code which should work if my two parameters Gmax and Km were the axes:

    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()

所以我想我的问题是,是否有用于参数的命令而不是用于轴滑块的ax命令? 还是有另一种方法呢?

So I guess my question is if there is a command for parameters instead of the ax command for axes sliders? Or if there is another way of doing it?

推荐答案

您的代码几乎正确,但是您应该将l = plot(f(S))更改为l, = plot(f(S)),因为plot()返回一个列表.然后,您可以调用l.set_ydata(...)设置新值.

Your code is almost right, but you should change l = plot(f(S)) to l, = plot(f(S)) because plot() returns a list. Then you can call l.set_ydata(...) to set the new value.

这是代码:

import matplotlib.pyplot as plt
import pylab
import numpy as np


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


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


S = np.arange(0, 100, 0.1)

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

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

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

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

plt.show()

这篇关于matplotlib滑块的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆