# Source - https://stackoverflow.com/a/6697555 # Posted by Joe Kington, modified by community. See post 'Timeline' for change history # Retrieved 2026-02-19, License - CC BY-SA 4.0 from numpy import pi, sin, exp, log #import numpy as np import matplotlib.pyplot as plt from matplotlib.widgets import Slider, Button, RadioButtons def X(x,r): return log(r)*x*(1-x) def evolve(r,x_0): a=[x_0] for n in range(100): a.append(X(a[-1],r)) return a axis_color = 'lightgoldenrodyellow' axis_color = 'white' fig = plt.figure() ax = fig.add_subplot(111) # Adjust the subplots region to leave some space for the sliders and buttons fig.subplots_adjust(left=0.25, bottom=0.25) amp_0 = 2 x_0=1/2 # Draw the initial plot # The 'line' variable is used for modifying the line later [line] = ax.plot(evolve(amp_0,x_0),'.-') #ax.set_xlim([0, 1]) ax.set_ylim([0, 1]) # Add two sliders for tweaking the parameters # Define an axes area and draw a slider in it amp_slider_ax = fig.add_axes([0.25, 0.15, 0.65, 0.03], facecolor=axis_color) amp_slider = Slider(amp_slider_ax, 'parameter', 0.1, exp(4.0), valinit=amp_0) amp_slider_ax2 = fig.add_axes([0.25, 0.1, 0.65, 0.03], facecolor=axis_color) amp_slider2 = Slider(amp_slider_ax2, 'i.c.', 0., 1.0, valinit=x_0) # Define an action for modifying the line when any slider's value changes def sliders_on_changed(val): line.set_ydata(evolve(amp_slider.val,amp_slider2.val)) fig.canvas.draw_idle() amp = log(amp_slider.val) amp_slider.valtext.set_text(amp) amp_slider.on_changed(sliders_on_changed) amp_slider2.on_changed(sliders_on_changed) # Add a button for resetting the parameters reset_button_ax = fig.add_axes([0.8, 0.025, 0.1, 0.04]) reset_button = Button(reset_button_ax, 'Reset', color=axis_color, hovercolor='0.975') def reset_button_on_clicked(mouse_event): amp_slider.reset() amp_slider2.reset() reset_button.on_clicked(reset_button_on_clicked) plt.show()