4

あなたの助けが必要です。pylabinを使用して正弦波をプロットする以下のコードを検討してくださいIPython。軸の下のスライダーを使用すると、正弦波の周波数をインタラクティブに調整できます。

%pylab
# setup figure
fig, ax = subplots(1)
fig.subplots_adjust(left=0.25, bottom=0.25)

# add a slider
axcolor = 'lightgoldenrodyellow'
ax_freq = axes([0.3, 0.13, 0.5, 0.03], axisbg=axcolor)
s_freq = Slider(ax_freq, 'Frequency [Hz]', 0, 100, valinit=a0)

# plot 
g = linspace(0, 1, 100)
f0 = 1
sig = sin(2*pi*f0*t)
myline, = ax.plot(sig)

# update plot
def update(value):
    f = s_freq.val
    new_data = sin(2*pi*f*t)
    myline.set_ydata(new_data)     # crucial line
    fig.canvas.draw_idle()

s_freq.on_changed(update)

t上記の代わりに、各ポイントの振幅からx 軸までの範囲の垂直線として信号をプロットする必要があります。したがって、私の最初のアイデアは、 15行目vlinesの代わりに使用することでしplotた:

myline = ax.vlines(range(len(sig)), 0, sig)

このソリューションは、非対話型の場合に機能します。問題は、インタラクティブにデータを更新するメソッドを提供するオブジェクトをplot返すことです。によって返されるオブジェクトは型であり、そのようなメソッドを提供しません。私の質問:インタラクティブに更新するにはどうすればよいですか?matplotlib.lines.Line2Dset_ydatavlinesmatplotlib.collections.LineCollectionLineCollection

4

2 に答える 2