In [21]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from ipywidgets import StaticInteract, RangeWidget, RadioWidget
  
def plot(n):
    fig, ax = plt.subplots(figsize=(4, 3),
                           subplot_kw={'axisbg':'#EEEEEE',
                                       'axisbelow':True})
    ax.grid(color='w', linewidth=2, linestyle='solid')
    x = np.linspace(0, 2*np.pi, 100)
    y = np.zeros(len(x))
    i = 0
    while i < n+1 :
        y += (1.0/(2*i+1))*np.sin((2*i+1)*x)
        i += 1
    
    ax.plot(x, y, lw=5, alpha=0.4)
    ax.set_xlim(0,2*np.pi)
    ax.set_ylim(-1, 1)
    return fig

StaticInteract(plot,n=RangeWidget(0, 15, 1))
Out[21]:
n:
In [18]:
x = np.linspace(0,2*np.pi,100)
i = 0
n = 15
y = np.zeros(len(x))
while i < n+1:
    y += (1.0/(2*i+1))*np.sin((2*i+1)*x)
    i = i+1
    
plt.xlim(0,2*np.pi)
plt.plot(x,y)
Out[18]:
[<matplotlib.lines.Line2D at 0xa91da2ec>]
In []: