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?
I have got it with plotly. Here is my code to get a plot described in the question.