gpt4 book ai didi

python - 如何在 matplotlib 中制作两个 slider

转载 作者:行者123 更新时间:2023-11-28 22:28:47 25 4
gpt4 key购买 nike

我想在 matplotlib 中制作两个 slider 来手动更改我的捕食者-猎物模型中的 N 和 P 值:

import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint

def lotka(x,t,params):
N, P = x
alpha, beta, gamma, delta = params
derivs = [alpha*N - beta*N*P, gamma*N*P - delta*P]
return derivs

N=2
P=1
alpha=3
beta=0.5
gamma=0.4
delta=3

params = [alpha, beta, gamma, delta]
x0=[N,P]
maxt = 20
tstep = 0.01

t=np.arange(0,maxt,tstep)
equation=odeint(lotka, x0, t, args=(params,))

plt.plot(t,equation)
plt.xlabel("Time")
plt.ylabel("Population size")
plt.legend(["Prey", "Predator"], loc="upper right")

plt.title('Prey & Predator Static Model')
plt.grid(color="b", alpha=0.5, linestyle="dashed", linewidth=0.5)

这是我的代码,它为 N 和 P 的固定初始值生成图表。但是,我想更改它们以查看绘图如何变化。为此,我想使用如下 slider :http://matplotlib.org/users/screenshots.html#slider-demo但我不知道如何将其添加到我的代码中...

有人可以给我任何方向吗?非常感谢!! xx

最佳答案

从例子中,希望评论能帮助你理解什么是什么:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button, RadioButtons
from scipy.integrate import odeint

# Function to draw
def lotka(x, t, params):
N, P = x
alpha, beta, gamma, delta = params
derivs = [alpha*N - beta*N*P, gamma*N*P - delta*P]
return derivs

# Parameters
Nmin = 1
Nmax = 100
Pmin = 1
Pmax = 100
N0 = 2
P0 = 1
alpha = 3
beta = 0.5
gamma = 0.4
delta = 3

params = [alpha, beta, gamma, delta]
x0=[N0,P0]
maxt = 20
tstep = 0.01

# Initial function values
t = np.arange(0, maxt, tstep)
prey, predator = odeint(lotka, x0, t, args=(params,)).T
# odeint returne a shape (2000, 2) array, with the value for
# each population in [[n_preys, n_predators], ...]
# The .T at the end transponses the array, so now we get each population
# over time in each line of the resultint (2, 2000) array.

# Create a figure and an axis to plot in:
fig = plt.figure()
ax = fig.add_axes([0.10, 0.3, 0.8, 0.6])
prey_plot = ax.plot(t, prey, label="Prey")[0]
predator_plot = ax.plot(t, predator, label="Predator")[0]

ax.set_xlabel("Time")
ax.set_ylabel("Population size")
ax.legend(loc="upper right")
ax.set_title('Prey & Predator Static Model')
ax.grid(color="b", alpha=0.5, linestyle="dashed", linewidth=0.5)
ax.set_ylim([0, np.max([prey, predator])])

# create a space in the figure to place the two sliders:
axcolor = 'lightgoldenrodyellow'
axis_N = fig.add_axes([0.10, 0.1, 0.8, 0.03], facecolor=axcolor)
axis_P = fig.add_axes([0.10, 0.15, 0.8, 0.03], facecolor=axcolor)
# the first argument is the rectangle, with values in percentage of the figure
# size: [left, bottom, width, height]

# create each slider on its corresponding place:
slider_N = Slider(axis_N, 'N', Nmin, Nmax, valinit=N0)
slider_P = Slider(axis_P, 'P', Pmin, Pmax, valinit=P0)

def update(val):
# retrieve the values from the sliders
x = [slider_N.val, slider_P.val]
# recalculate the function values
prey, predator = odeint(lotka, x, t, args=(params,)).T
# update the value on the graph
prey_plot.set_ydata(prey)
predator_plot.set_ydata(predator)
# redraw the graph
fig.canvas.draw_idle()
ax.set_ylim([0, np.max([prey, predator])])

# set both sliders to call update when their value is changed:
slider_N.on_changed(update)
slider_P.on_changed(update)

# create the reset button axis (where its drawn)
resetax = plt.axes([0.8, 0.025, 0.1, 0.04])
# and the button itself
button = Button(resetax, 'Reset', color=axcolor, hovercolor='0.975')

def reset(event):
slider_N.reset()
slider_P.reset()

button.on_clicked(reset)

但是请注意,您应该有 shown how you tried使示例适应您所拥有的以及它的行为不端。

尽管如此,欢迎使用 Stackoverflow。

关于python - 如何在 matplotlib 中制作两个 slider ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43381449/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com