Dynamic plot with matplotlib slider

266 views Asked by At

I want to make a dynamic plot with the help of matplotlib Slider. My function gets an array of a, b values for plotting beta distribution functions, and each value from slider should update the plot with the new a, b values. Here is my code.

def plot_dynamic_beta(a_b_list: np.array, label_name: str):
    def update(val):
        timestamp = time_slider.val
        a, b = a_b_list[timestamp] # new a, b values to draw the new distribution 
        rv = beta(a, b)
        Plot.set_ydata(rv.pdf(np.linspace(0, 1, 100))) # i guess here is the wrong part 
        plt.draw()

    fig, ax = plt.subplots()
    plt.subplots_adjust(bottom=0.25)

    rv = beta(1, 1)
    Plot, = plt.plot(rv.pdf(np.linspace(0, 1, 100)), 'k-', lw=2, label=label_name)
    plt.axis([0, 1, -10, 10])

    # slider_x = np.arange(0, len(a_b_list))
    slider_x = plt.axes([0.25, 0.15, 0.65, 0.03], facecolor=axcolor)
    time_slider = Slider(slider_x, 'timepoint',
                         0, len(a_b_list) - 1, valinit=0, valstep=np.arange(0, len(a_b_list)))

    time_slider.on_changed(update)
    plt.show()

so it plots the first plot correctly but changing the slider value doesn't plot what I need. Example a_b_list= np.array([(1,2),(2,2),(3,2),(4,2)]) When I change the slider value it doesn't plot the beta distribution with the a,b values I gave. So for example if I change the slide to 2 it should plot beta distribution with a=3 and b=2 but it doesn't do it. What did I do wrong?

1

There are 1 answers

0
foxel On

I have got it with plotly. Here is my code to get a plot described in the question.

def plot_dynamic_beta(a_b_list: np.array or list, label_name: str):
    fig = go.Figure()
    for a, b in a_b_list:
        beta_new = beta(a, b)
        fig.add_trace(go.Scatter(visible=False,
                                 line=dict(color="#00CED1", width=6),
                                 name="t = " + str(np.where(a_b_list == (a, b))),
                                 x=np.linspace(0, 1, 100),
                                 y=beta_new.pdf(np.linspace(0, 1, 100))))

    # Create and add slider
    steps = []
    for i in range(len(fig.data)):
        step = dict(
            method="update",
            args=[{"visible": [False] * len(fig.data)},
                  {"title": "Beta distribution switched to timestamp: " + str(i)}],  # layout attribute
        )
        step["args"][0]["visible"][i] = True  # Toggle i'th trace to "visible"
        steps.append(step)

    sliders = [dict(
        active=0,
        currentvalue={"prefix": "Frequency: "},
        pad={"t": 100},
        steps=steps
    )]

    fig.update_layout(
        sliders=sliders
    )

    fig.show()