python - set_ydata for subplots OR why is figure containing two subplots significantly slower than figure without subpots -
i've extended following example use multiple subplots. here code:
#!/usr/bin/python import numpy np import matplotlib.pyplot plt matplotlib.widgets import slider ######################################## t = np.arange(0.0, 1.0, 0.001) a0 = 5 f0 = 3 s = a0*np.sin(2*np.pi*f0*t) ######################################## plt.close('all') fig, ax = plt.subplots(nrows=2, ncols=1) plt.subplots_adjust(bottom=0.30) ######################################## ax[0].plot(t,s, lw=2, color='red', label="red") ax[1].plot(t,s, lw=2, color='green', label="green") ######################################## # plt.axis([0, 1, -10, 10]) ax[0].set_xlim([0, 1]) ax[0].set_ylim([-10, 10]) ax[1].set_xlim([0, 1]) ax[1].set_ylim([-10, 10]) ######################################## axcolor = 'lightgoldenrodyellow' f1 = plt.axes([0.25, 0.20, 0.65, 0.03], axisbg=axcolor) a1 = plt.axes([0.25, 0.15, 0.65, 0.03], axisbg=axcolor) f2 = plt.axes([0.25, 0.1, 0.65, 0.03], axisbg=axcolor) a2 = plt.axes([0.25, 0.05, 0.65, 0.03], axisbg=axcolor) sf1 = slider(f1, 'freq1', 0.1, 30.0, valinit=f0) sa1 = slider(a1, 'amp1', 0.1, 10.0, valinit=a0) sf2 = slider(f2, 'freq2', 0.1, 30.0, valinit=f0) sa2 = slider(a2, 'amp2', 0.1, 10.0, valinit=a0) ######################################## def update1(val): amp = sa1.val freq = sf1.val # not works - set_ydata not exists # ax[1].set_ydata(amp*np.sin(2*np.pi*freq*t)) # ax[2].set_ydata(amp*np.sin(2*np.pi*freq*t)) # works seems slow s = amp*np.sin(2*np.pi*freq*t) ax[0].clear() ax[0].plot(t,s, lw=2, color='red', label="red") ax[0].set_xlim([0, 1]) ax[0].set_ylim([-10, 10]) # has no effect on speed fig.canvas.draw_idle() sf1.on_changed(update1) sa1.on_changed(update1) def update2(val): amp = sa2.val freq = sf2.val # not works - set_ydata not exists # ax[1].set_ydata(amp*np.sin(2*np.pi*freq*t)) # ax[2].set_ydata(amp*np.sin(2*np.pi*freq*t)) # works seems slow s = amp*np.sin(2*np.pi*freq*t) ax[1].clear() ax[1].plot(t,s, lw=2, color='green', label="green") ax[1].set_xlim([0, 1]) ax[1].set_ylim([-10, 10]) # has no effect on speed fig.canvas.draw_idle() sf2.on_changed(update2) sa2.on_changed(update2) plt.show()
the problem is slower (when slider clicked) original. suspect caused bunch of code used in update1
, update2
functions. not know how rewrite more effectively. original example using set_ydata
function seems subplots not have function. i've been thinking 1 update
function 4 sliders, not know if possible distinguish object on update triggered , handle inside function. thanks
you try call set_ydata
on ax[0]
(or ax[1]
, respectively). not work, these axes instances. set_ydata
method of line2d
artists. in example have linked line l
created in beginning, receives new y data in update function. forgot in code.
so need create lines:
line0, = ax[0].plot(t,s, lw=2, color='red') line1, = ax[1].plot(t,s, lw=2, color='green')
in beginning , in update functions can call:
line0.set_ydata(amp*np.sin(2*np.pi*freq*t))
or
line1.set_ydata(amp*np.sin(2*np.pi*freq*t))
note: idea here change y coordinates of lines. workaround, deleted entire graph , recreated it. more work , therefore makes things slower.
Comments
Post a Comment