# Set default plotting parameters.

import matplotlib.pyplot as plt

plt.rcParams['text.usetex'] = True
plt.rcParams['font.family'] = 'computer modern sans serif'
plt.rcParams['font.size'] = 9
plt.rcParams['lines.markersize'] = 2.5
plt.rcParams['figure.figsize'] = 3, 2
plt.rcParams['figure.dpi'] = 136

3.1. Widgets testΒΆ

Below is a test of matplotlib widgets.

%matplotlib widget

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

def get_fourier_sums(xs, N):
    fourier_sums = np.zeros(len(xs)) + .5
    for n in range(1, N+1):
        fourier_sums -= np.sin(2*np.pi*n*xs) / (np.pi*n)
    return fourier_sums

dx = .01
xs, N = np.arange(0+dx, 1, dx), 1

# Initialize the plot.
fig, ax = plt.subplots()
plt.subplots_adjust(left=0.25, bottom=0.25)  # Make room for sliders.
[line] = ax.plot(xs, get_fourier_sums(xs, N))
ax.set_ylim([0, 1])

# Create parameter sliders.
ax_N = plt.axes([0.25, 0.1, 0.65, 0.03])
slider_N = Slider(ax_N, label='$N$', valmin=1, valmax=100, valstep=1, valinit=N)

# Update sliders.
def update_plot(val):
    N = slider_N.val
    line.set_ydata(get_fourier_sums(xs, N))
    fig.canvas.draw_idle()  # Redraw with new slider parameters.

slider_N.on_changed(update_plot);
plt.show()