• <tfoot id='TT1FC'></tfoot>

    <small id='TT1FC'></small><noframes id='TT1FC'>

    <legend id='TT1FC'><style id='TT1FC'><dir id='TT1FC'><q id='TT1FC'></q></dir></style></legend>
        <bdo id='TT1FC'></bdo><ul id='TT1FC'></ul>

      1. <i id='TT1FC'><tr id='TT1FC'><dt id='TT1FC'><q id='TT1FC'><span id='TT1FC'><b id='TT1FC'><form id='TT1FC'><ins id='TT1FC'></ins><ul id='TT1FC'></ul><sub id='TT1FC'></sub></form><legend id='TT1FC'></legend><bdo id='TT1FC'><pre id='TT1FC'><center id='TT1FC'></center></pre></bdo></b><th id='TT1FC'></th></span></q></dt></tr></i><div id='TT1FC'><tfoot id='TT1FC'></tfoot><dl id='TT1FC'><fieldset id='TT1FC'></fieldset></dl></div>

        如何在 matplotlib 中制作两个滑块

        How to make two sliders in matplotlib(如何在 matplotlib 中制作两个滑块)

            <tfoot id='h3NGZ'></tfoot>
              <bdo id='h3NGZ'></bdo><ul id='h3NGZ'></ul>

                  <small id='h3NGZ'></small><noframes id='h3NGZ'>

                  <i id='h3NGZ'><tr id='h3NGZ'><dt id='h3NGZ'><q id='h3NGZ'><span id='h3NGZ'><b id='h3NGZ'><form id='h3NGZ'><ins id='h3NGZ'></ins><ul id='h3NGZ'></ul><sub id='h3NGZ'></sub></form><legend id='h3NGZ'></legend><bdo id='h3NGZ'><pre id='h3NGZ'><center id='h3NGZ'></center></pre></bdo></b><th id='h3NGZ'></th></span></q></dt></tr></i><div id='h3NGZ'><tfoot id='h3NGZ'></tfoot><dl id='h3NGZ'><fieldset id='h3NGZ'></fieldset></dl></div>
                    <tbody id='h3NGZ'></tbody>

                  <legend id='h3NGZ'><style id='h3NGZ'><dir id='h3NGZ'><q id='h3NGZ'></q></dir></style></legend>
                  本文介绍了如何在 matplotlib 中制作两个滑块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  I would like to make two sliders in matplotlib to manually change N and P values in my predator-prey model:

                  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)
                  

                  This is my code which produces a graph for fixed initial values of N and P. However, I'd like to change them to see how the plot changes. And for this, I'd like to use sliders like: http://matplotlib.org/users/screenshots.html#slider-demo but I do not know how to add this into my code...

                  Could anyone please give me any direction? Many thanks!! xx

                  解决方案

                  From the example, hope the comments help you understand what's what:

                  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)
                  

                  Notice, however, you should have shown how you tried to adapt the example to what you had and how it was misbehaving.

                  Nevertheless, welcome to Stackoverflow.

                  这篇关于如何在 matplotlib 中制作两个滑块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Running .jl file from R or Python(从 R 或 Python 运行 .jl 文件)
                  Running Julia .jl file in python(在 python 中运行 Julia .jl 文件)
                  Using PIP in a Azure WebApp(在 Azure WebApp 中使用 PIP)
                  How to run python3.7 based flask web api on azure(如何在 azure 上运行基于 python3.7 的烧瓶 web api)
                  Azure Python Web App Internal Server Error(Azure Python Web 应用程序内部服务器错误)
                  Run python dlib library on azure app service(在 azure app 服务上运行 python dlib 库)
                    <tfoot id='gw4Wt'></tfoot>
                      <tbody id='gw4Wt'></tbody>
                    • <bdo id='gw4Wt'></bdo><ul id='gw4Wt'></ul>
                      <i id='gw4Wt'><tr id='gw4Wt'><dt id='gw4Wt'><q id='gw4Wt'><span id='gw4Wt'><b id='gw4Wt'><form id='gw4Wt'><ins id='gw4Wt'></ins><ul id='gw4Wt'></ul><sub id='gw4Wt'></sub></form><legend id='gw4Wt'></legend><bdo id='gw4Wt'><pre id='gw4Wt'><center id='gw4Wt'></center></pre></bdo></b><th id='gw4Wt'></th></span></q></dt></tr></i><div id='gw4Wt'><tfoot id='gw4Wt'></tfoot><dl id='gw4Wt'><fieldset id='gw4Wt'></fieldset></dl></div>

                      • <legend id='gw4Wt'><style id='gw4Wt'><dir id='gw4Wt'><q id='gw4Wt'></q></dir></style></legend>

                        <small id='gw4Wt'></small><noframes id='gw4Wt'>