Я думаю, что прежде всего проблема в том, что вы хотите показать. Если вы измените частоту, вы, вероятно, захотите пересчитать спектр. Итак, давайте сначала исправим это, используя линейный график.
%matplotlib notebook
import matplotlib.pyplot as plt
import numpy as np
from scipy import fftpack
from ipywidgets import *
def func(f_s):
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
return freqs, np.abs(X)
f = 10
f_s = 100
fig, ax = plt.subplots()
line, = ax.plot(*func(f_s), marker="o")
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):
if f_s > 0:
x, y = func(f_s)
line.set_data(x, y)
ax.set_xlim(-f_s / 2, f_s / 2)
ax.set_ylim(None, 1.1*y.max())
fig.canvas.draw()
interact(update);
Теперь проблема с stem
графиком состоит в том, что он создает много линий, которые необходимо обновить. Поэтому я бы рекомендовал использовать LineCollection
.
%matplotlib notebook
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
import numpy as np
from scipy import fftpack
from ipywidgets import *
def func(f_s):
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
return freqs, np.abs(X)
f = 10
f_s = 100
fig, ax = plt.subplots()
x,y = func(f_s)
markers, = ax.plot(x,y, ls="none", marker="o")
baseline = ax.axhline(0, color="crimson")
verts=np.c_[x, np.zeros_like(x), x, y].reshape(len(x),2,2)
col = LineCollection(verts)
ax.add_collection(col)
ax.set_xlabel('Frecuency [Hz]')
ax.set_ylabel('Spectrum')
ax.set_xlim(-f_s / 2, f_s / 2)
ax.set_ylim(None, 110);
def update(f_s=100):
if f_s > 0:
x, y = func(f_s)
markers.set_data(x, y)
verts=np.c_[x, np.zeros_like(x), x, y].reshape(len(x),2,2)
col.set_segments(verts)
ax.set_xlim(-f_s / 2, f_s / 2)
ax.set_ylim(None, 1.1*y.max())
fig.canvas.draw_idle()
interact(update);