def set_animation(fig, ts, lines): fig.subplots_adjust(bottom=0.1) axslider = plt.axes([0.0, 0.0, 1, 0.03]) slider = Slider(axslider, "Time", ts[0], ts[-1], valinit=ts[0]) frames = len(ts) def frame_seq(): while True: frame_seq.cur = (frame_seq.cur + 1) % frames # slider.set_val(ts[frame_seq.cur]) yield frame_seq.cur frame_seq.cur = 0 def ani_func(i): return lines(i) line_ani = animation.FuncAnimation(fig, ani_func, frames=frame_seq, interval=max(5000 / frames, 20), blit=False) def onClick(event): if event.inaxes == axslider: return if onClick.anim_running: fig.__line_ani.event_source.stop() onClick.anim_running = False else: fig.__line_ani.event_source.start() onClick.anim_running = True onClick.anim_running = True fig.canvas.mpl_connect("button_press_event", onClick) def onChanged(val): t = bisect_left(ts, val) frame_seq.cur = t line_ani = animation.FuncAnimation(fig, ani_func, frames=frame_seq, interval=max(5000 / frames, 20), blit=False) fig.__line_ani = line_ani slider.on_changed(onChanged) slider.drawon = False # lines(0) fig.__slider = slider fig.__line_ani = line_ani return line_ani
def __init__(self, rfs): self.rfs = rfs fig= plt.figure() ax = fig.add_subplot(211) ax2 = fig.add_subplot(223, projection='3d') ax3 = fig.add_subplot(224) self.ax3d = ax2 self.ax2d = ax3 self.fig = fig # fig.subplots_adjust(wspace=0.7) # fig.subplots_adjust(hspace=0.5) #plt.suptitle('RFS for DOF {}'.format(self.rfs.dofs[0])) fig.tight_layout() fig.subplots_adjust(bottom=0.2) # plot acceleration signal t = np.arange(self.rfs.ns)/self.rfs.fs ax.plot(t, self.rfs.ydd,'-k') #ax.plot(t, self.rfs.y,'-k') ax.set_xlabel('Time (s)') ax.set_ylabel(r'Acceleration ($m/s^2$)') ax.set_title('Acceleration, for DOF {}'.format(self.rfs.dofs[0])) #¤ back to rectangular settings ymin, ymax = ax.get_ylim() xmin, xmax = ax.get_xlim() xx = [xmin,xmax] # Mouse should be within 2% of line. self.epsilon = np.diff(xx) * 0.02 # Set initial selection and add rectangle start_sel = 0.45 width_sel = 0.1 x1 = xx[0] + start_sel * np.diff(xx) dx = width_sel * np.diff(xx) rect = patches.Rectangle((x1, ymin), dx, ymax-ymin, alpha=0.4,fc='y') ax.add_patch(rect) # ax position rect [left, bottom, width, height] in fractions of figure # width and height. # checkbutton showing stiffness or damping buttonax = fig.add_axes([0.8, 0.02, 0.15, 0.06]) #, fc='white') button = CheckButtons(buttonax, ('Damping',), (False,)) button.on_clicked(self.checkbox_clicked) # set slider sliderax = fig.add_axes([0.1, 0.02, 0.6, 0.03], fc='white') slider = Slider(sliderax, 'Tol', 0, 1.0, valinit= 0.05) slider.on_changed(self.slider_update) slider.drawon = True canvas = rect.figure.canvas self.canvas = canvas self.rect = rect self.axes = rect.axes self.ind = None self.show_damped = False # show rfs for the initial selection self.update_rfs() canvas.mpl_connect('button_press_event', self.button_press_callback) canvas.mpl_connect('button_release_event', self.button_release_callback) canvas.mpl_connect('motion_notify_event', self.motion_notify_callback) plt.show()
def slide_phantom(shot, camera='phantom2', sub=20, blur=3, interval=50, pixel_t_hist=None): """ Slide through Phantom frames while displaying last closed flux surface and relevant timeseries plots. Parameters shot: int, shot number camera: string e.g. 'phantom' (outboard midplane GPI), 'phantom2' (X-point GPI) sub: number of frames to use in average subtraction. 20 works well. blur: extent of Gaussian blur, 1, 2, or 3 advisable interval: delay in ms between frames during play pixel_t_hist: (x,y) to show time history of that pixel instead of H-alpha. """ time = acquire.gpi_series(shot, camera, 'time') frames = acquire.video(shot, camera) frame_count = frames.shape[0] # Plot GPI and LCFS gs = gridspec.GridSpec(3, 1, height_ratios=[3, 1, 1]) fig, ax = plt.subplots() plt.subplots_adjust(bottom=0.25, hspace=.43, top=.96) plt.subplot(gs[0]) im = plt.imshow(frames[0], origin='lower', cmap=plt.cm.gist_heat) plt.colorbar() plt.axis('off') # Plot H_alpha or pixel timeseries plt.subplot(gs[1]).locator_params(axis='y', nbins=4) if pixel_t_hist: plt.title('Pixel time history') plt.plot(time, frames[:, pixel_t_hist[0], pixel_t_hist[1]].swapaxes(0, 1)) else: plt.title('H-alpha') time_ha2, ha2 = process.time_crop(acquire.time_ha2(shot), time) plt.plot(time_ha2, ha2) vl1 = plt.axvline(time[0], color='r') plt.xlim([time[0], time[-1]]) # Plot line average density time_dens, dens = process.time_crop(acquire.time_dens(shot), time) plt.subplot(gs[2]).locator_params(axis='y', nbins=4) plt.title('Line Average Density') plt.plot(time_dens, dens) vl2 = plt.axvline(time[0], color='r') plt.xlabel('Time (s)') plt.xlim([time[0], time[-1]]) curr_frame = 0 def update(val): global frames, curr_frame val = int(val) curr_frame = val curr_time = time[val] slider.valtext.set_text('%d (t=%.5f s)' % (val, curr_time)) if val < frame_count: im.set_array(frames[val]) vl1.set_xdata(curr_time) vl2.set_xdata(curr_time) fig.canvas.draw_idle() # Slider settings slide_area = plt.axes([0.10, 0.1, 0.65, 0.03]) slider = Slider(slide_area, 'Frame', 0, frame_count-1, valinit=0) slider.drawon = True slider.valfmt = '%d' slider.on_changed(update) def init(): global curr_frame curr_frame = int(curr_frame) start_frame = curr_frame im.set_data(frames[curr_frame]) vl1.set_xdata(time[curr_frame]) vl2.set_xdata(time[curr_frame]) for i in xrange(curr_frame, curr_frame + 200, 1): slider.set_val(i) vl1.set_xdata(time[i]) vl2.set_xdata(time[i]) slider.set_val(start_frame) return [im, vl1, vl2] def animate(i): im.set_array(frames[i]) vl1.set_xdata(time[i]) vl2.set_xdata(time[i]) return [im, vl1, vl2] def play(event): anim = animation.FuncAnimation(fig, animate, init_func=init, frames=100, interval=interval, blit=False) def forward(event): global curr_frame slider.set_val(curr_frame + 1) def backward(event): global curr_frame slider.set_val(curr_frame - 1) # Time button settings play_button_area = plt.axes([0.45, 0.025, 0.1, 0.04]) play_button = Button(play_button_area, 'Play') play_button.on_clicked(play) forward_button_area = plt.axes([0.56, 0.05, 0.04, 0.025]) forward_button = Button(forward_button_area, '>') forward_button.on_clicked(forward) back_button_area = plt.axes([0.56, 0.015, 0.04, 0.025]) back_button = Button(back_button_area, '<') back_button.on_clicked(backward) def apply_filter(label): global frames if label == 'Orig': frames = acquire.video(shot, camera) elif label == 'Sub %d' % sub: frames = acquire.video(shot, camera, sub=sub) elif label == 'Blur %d' % blur: frames = acquire.video(shot, camera, sub=sub, blur=blur) elif label == 'Sobel': frames = acquire.video(shot, camera, sub=sub, blur=blur, sobel=True) update(curr_frame) im.autoscale() def recolor(event): im.autoscale() def cmap_change(label): global curr_frame if label == 'Red': im.cmap = plt.cm.gist_heat if label == 'Gray': im.cmap = plt.cm.gray update(curr_frame) im.autoscale() # Image button settings left = .3 bottom = .79 filter_radio_area = plt.axes([left, bottom, .1, .12]) filter_radio = RadioButtons(filter_radio_area, ('Orig', 'Sub %d' % sub, 'Blur %d' % blur, 'Sobel')) filter_radio.on_clicked(apply_filter) cmap_radio_area = plt.axes([left, bottom-.08, .1, .07]) cmap_radio = RadioButtons(cmap_radio_area, ('Red', 'Gray')) cmap_radio.on_clicked(cmap_change) recolor_button_area = plt.axes([left, bottom-.13, 0.1, 0.04]) recolor_button = Button(recolor_button_area, 'Recolor') recolor_button.on_clicked(recolor) # Black magic to fix strange global variable error apply_filter('Orig') plt.show()
def slide_corr(frames, pixel, other_pixels=None): """ Display correlation values for a pixel with a slider for lag time. Parameters frames: NumPy array of correlations with dimension (no. lags, x pixels, y pixels) pixel: (x, y) to mark with a circle other_pixels: array of (x, y) values of pixels to mark """ frame_count = frames.shape[0] # Initial plotting fig = plt.figure() ax = fig.add_subplot(1, 1, 1) plt.subplots_adjust(bottom=0.25) im = plt.imshow(frames[frame_count/2], origin='bottom', cmap=plt.cm.RdBu_r, vmin=-1, vmax=1) circ = plt.Circle(pixel[::-1], radius=1, edgecolor='r', fill=False) ax.add_patch(circ) if other_pixels: t_hists_r = [o[0] for o in other_pixels] t_hists_z = [o[1] for o in other_pixels] else: t_hists_r = acquire.gpi_series(1150611004, 'phantom2', 'hist_xpix') t_hists_z = acquire.gpi_series(1150611004, 'phantom2', 'hist_ypix') for pos in zip(t_hists_r, t_hists_z): ax.add_patch(plt.Circle(pos, radius=1, edgecolor='b', fill=False)) plt.colorbar() plt.title('Correlation for pixel (%d, %d)' % pixel) plt.xlabel('Pixel y coordinate') plt.ylabel('Pixel x coordinate') # Slider and button settings slide_area = plt.axes([0.10, 0.1, 0.65, 0.03]) slider = Slider(slide_area, 'Lag', -frame_count/2, frame_count/2, valinit=0) slider.drawon = True slider.valfmt = '%d' play_button_area = plt.axes([0.45, 0.025, 0.1, 0.04]) play_button = Button(play_button_area, 'Play') curr_frame = 0 def update(val): global curr_frame curr_frame = val + frame_count/2 slider.valtext.set_text('%d' % val) if curr_frame < frame_count: im.set_array(frames[curr_frame]) fig.canvas.draw_idle() slider.on_changed(update) def init(): global curr_frame curr_frame = int(curr_frame) im.set_data(frames[curr_frame]) for i in xrange(curr_frame, curr_frame + 200, 2): slider.set_val(i) return [im] def animate(i): im.set_array(frames[i]) return [im] def play(event): anim = animation.FuncAnimation(fig, animate, init_func=init, frames= frame_count, interval=0, blit=False) play_button.on_clicked(play) plt.show()