def test_save_animation(): # just test that no errors are thrown, don't actually check # that the saved animation makes any sense. Doing that seems hard :( x = np.linspace(0, np.pi, 100) tau = widgets.FloatSlider(min=1, max=10, step=1.5) def f1(x, amp, tau, beta): return amp * np.sin(x * tau) * x * beta def f2(x, amp, tau, beta): return amp * np.sin(x * beta) * x * tau x = np.linspace(0, 2 * np.pi, 200) fig, ax = plt.subplots() plt.subplots_adjust(bottom=0.25) axfreq = plt.axes([0.25, 0.1, 0.65, 0.03]) amp_slider = Slider(axfreq, label="amp", valmin=0.05, valmax=10) ctrls = Controls(use_ipywidgets=True, tau=tau, beta=(1, 10, 15), amp=amp_slider) iplt.plot(x, f1, ax=ax, controls=ctrls) iplt.plot(x, f2, ax=ax, controls=ctrls) ctrls.save_animation("animation-amp.gif", fig, "amp", N_frames=10) ctrls.save_animation("animation-beta.gif", fig, "beta") ctrls.save_animation("animation-tau.gif", fig, "tau")
def test_title(): fig, ax = plt.subplots() ctrls = iplt.plot(1, 1, E=3e7) expected = "E=3.00e+07" iplt.title("E={E:.2e}", controls=ctrls) assert not isinstance(ctrls.params["E"], np.ndarray) assert ax.get_title() == expected plt.close() fig, ax = plt.subplots() ctrls = iplt.plot(1, 1, E=np.array([3e7, 3e9])) iplt.title("E={E:.2e}", controls=ctrls) assert ax.get_title() == expected plt.close()
""" ===================================================== Matplotlib Sliders without a separate Controls Figure ===================================================== Demonstration of how to provide a matplotlib slider to prevent the creation of a separate controls figure. """ import matplotlib.pyplot as plt import numpy as np from matplotlib.widgets import Slider from mpl_interactions import ipyplot as iplt fig, ax = plt.subplots() plt.subplots_adjust(bottom=0.25) x = np.linspace(0, 2 * np.pi, 200) def f(x, freq): return np.sin(x * freq) axfreq = plt.axes([0.25, 0.1, 0.65, 0.03]) slider = Slider(axfreq, label="freq", valmin=0.05, valmax=10) controls = iplt.plot(x, f, freq=slider, ax=ax) plt.show()