0

I am trying to plot the biffurcation diagram and its equation.

My problem is that I want to put a slider for when I change the rate in the logistic map equation, but I can't seem to understand what I need to code in the update function.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider



rate = np.linspace(1, 4, 1000)
N = 1000
x = np.zeros(N) + 0.5
count = np.arange(round(N*0.9), N)

y = np.zeros(N) + 0.5
#t = 1

# Biffurcation
for rs in range(len(rate)):
    for n in range(N-1):
        x[n+1] = rate[rs] * x[n] * (1-x[n])

    u = np.unique(x[count])
    r = rate[rs] * np.ones(len(u))

    for i in range(N - 1):
        y[i + 1] = rate[rs] * y[i] * (1 - y[i])

    # plotting
    plt.plot(r, u, '.', markersize=2)
    plt.ylabel(ylabel='X')
    plt.xlabel(xlabel='r')
    plt.title('Biffurcation')


# Plotting
fig, ax = plt.subplots()
axes, = ax.plot(y, 'o-')
ax.set_ylabel(ylabel='X')
ax.set_xlabel(xlabel='Time')
ax.set_title('$x_{n+1}$ = r * $x_{n}$ * (1-$x_{n}$)')


# defining axSlider
fig.subplots_adjust(bottom=0.25)
ax_slider = fig.add_axes([0.15, 0.1, 0.65, 0.03])
slider = Slider(ax_slider, label='r', valmin=1, valmax=4, valinit=1, valstep=rate)


# updating the plot
def update(val):
    current_v = slider.val
    rate[rs] = current_v
    axes.set_ydata(rate[rs])
    fig.canvas.draw()


slider.on_changed(update)

plt.show()

I tried to update my plot for when I change the rate on my slider, but it is not working properly.

def update(val):
    current_v = slider.val
    rate[rs] = current_v
    axes.set_ydata(rate[rs])
    fig.canvas.draw()
Spaghetti
  • 1
  • 1

0 Answers0