I am trying to make a Python stem
plot in a Jupyter notebook.
The plot is this:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
from scipy import fftpack
f = 10
f_s = 100
t = np.linspace(0, 2, 2 * f_s, endpoint=False)
x = np.sin(f * 2 * np.pi * t)
X = fftpack.fft(x)
freqs = fftpack.fftfreq(len(x)) * f_s
fig, ax = plt.subplots()
ax.stem(freqs, np.abs(X))
ax.set_xlabel('Frecuency [Hz]')
ax.set_ylabel('Spectrum')
ax.set_xlim(-f_s / 2, f_s / 2)
ax.set_ylim(-5, 110);
And I would like to add a slider bar like in this post, Jupyter Notebook: interactive plot with widgets . The solution in this post
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
from scipy import fftpack
from ipywidgets import *
f = 10
f_s = 100
t = np.linspace(0, 2, 2 * f_s, endpoint=False)
x = np.sin(f * 2 * np.pi * t)
X = fftpack.fft(x)
freqs = fftpack.fftfreq(len(x)) * f_s
fig, ax = plt.subplots()
line, = ax.stem(freqs, np.abs(X))
ax.set_xlabel('Frecuency [Hz]')
ax.set_ylabel('Spectrum')
ax.set_xlim(-f_s / 2, f_s / 2)
ax.set_ylim(-5, 110);
def update(f_s=100):
line.set_ydata(fftpack.fftfreq(len(x)) * f_s)
ax.set_xlim(-f_s / 2, f_s / 2)
fig.canvas.draw()
interact(update);
returns me an error:
ValueError Traceback (most recent call last)
<ipython-input-18-fd1aaf0ca96d> in <module>()
7
8 fig, ax = plt.subplots()
----> 9 line, = ax.stem(freqs, np.abs(X))
10 ax.set_xlabel('Frecuency [Hz]')
11 ax.set_ylabel('Spectrum')
ValueError: too many values to unpack (expected 1)