How to make two sliders in matplotlib

后端 未结 2 932
攒了一身酷
攒了一身酷 2021-01-22 17:09

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
fr         


        
2条回答
  •  日久生厌
    2021-01-22 17:57

    So, I have tried with this code:

    from scipy import integrate
    from matplotlib.widgets import Slider, Button, RadioButtons
    
    fig, ax = plt.subplots()
    plt.subplots_adjust(left=0.25, bottom=0.25)
    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)
    
    l1, l2 = plt.plot(t, equation)
    
    axcolor = 'b'
    ax_N = plt.axes([0.25, 0.1, 0.65, 0.03], axisbg=axcolor)
    ax_P = plt.axes([0.25, 0.15, 0.65, 0.03], axisbg=axcolor)
    
    sN = Slider(ax_N, 'N', 0, 80, valinit=1)
    sP = Slider(ax_P, 'P', 0, 80, valinit=1)
    
    
    def update(val):
        N = N*sN.val
        P = P*sP.val
        x = equation
        fig.canvas.draw_idle()
        l1, l2.set_ydata(y)    
        ax.set_ylim(y.min(), y.max())  
        draw()
    
    sN.on_changed(update)
    sP.on_changed(update)
    
    plt.show()
    

    I could not manipulate the sliders. Thank you so much @berna1111

提交回复
热议问题