def build(self): global fig, ax, textinput1, textinput2, textinput3, textinput4, canvas fig,ax = plt.subplots(1) plt.imshow(img) box = BoxLayout() canvas = FigureCanvas(figure = fig) box.add_widget(canvas) fig.canvas.mpl_connect('button_press_event', onclick) # some widgets textinput1 = TextInput(text = "x1", multiline = False, size_hint = (0.7, None), height = 60) textinput1.bind(on_text_validate = on_enter) textinput2 = TextInput(text = "y1", multiline = False, size_hint = (0.7, None), height = 60) textinput2.bind(on_text_validate = on_enter) textinput3 = TextInput(text = "x2", multiline = False, size_hint = (0.7, None), height = 60) textinput3.bind(on_text_validate = on_enter) textinput4 = TextInput(text = "y2", multiline = False, size_hint = (0.7, None), height = 60) textinput4.bind(on_text_validate = on_enter) box.add_widget(textinput1) box.add_widget(textinput2) box.add_widget(textinput3) box.add_widget(textinput4) button1 = Button(text = 'update', size_hint = (0.7, None), height = 60) box.add_widget(button1) button1.bind(on_press = updateGraph) button2 = Button(text = 'recover', size_hint = (0.7, None), height = 60) box.add_widget(button2) button2.bind(on_press = recoverGraph) canvas.draw() return box
def init_plots(self): fig = plt.figure(facecolor=(0.3, 0.3, 0.3)) plt.subplots_adjust(left=0.05, right=0.98, wspace=0.2) for side_i in range(0, len(self.sides)): ## side_i, get it? ax = fig.add_subplot(1, 3, side_i + 1) ax.spines['bottom'].set_color('white') ax.spines['top'].set_color('white') ax.spines['left'].set_color('white') ax.spines['right'].set_color('white') ax.tick_params(length=0, color='white', labelsize=8, labelcolor='white') ax.set_title(self.sides[side_i] + ' Recruitment Curve', color='w', pad=2) ax.set_facecolor((0.3, 0.3, 0.3)) ax.set_xticks([]) #np.arange(0, 10000, 1000)) ax.set_yticks([]) #np.arange(0, 10000, 1000)) for m in self.muscles: ax.plot([], '.', label=m) leg = ax.legend(self.muscles, facecolor=(0.3, 0.3, 0.3), edgecolor='white') plt.setp(leg.get_texts(), color='white') pulse_ax = fig.add_subplot(1, 3, 3) pulse_ax.spines['bottom'].set_color('white') pulse_ax.spines['top'].set_color('white') pulse_ax.spines['left'].set_color('white') pulse_ax.spines['right'].set_color('white') pulse_ax.tick_params(length=0, color='white', labelsize=8, labelcolor='white') pulse_ax.set_title('EMG Stim Pulses', color='w', pad=2) pulse_ax.set_facecolor((0.3, 0.3, 0.3)) pulse_ax.set_xticks([]) pulse_ax.set_yticks([]) for i in range(0, NUM_PLOT_PULSES): pulse_ax.plot([], '-', linewidth=1, color="#0471A6") pulse_ax.plot([], '-', linewidth=1, color="#FB3640") fig_handle = FigureCanvas(fig) fig_handle.blit() fig_handle.draw() self.add_widget(fig_handle) self.recruitmentHandle = fig_handle
class RootWidget(BoxLayout): def __init__(self, rec = None, **kwargs): #--------------- UI Constuctions --------------------- super(RootWidget, self).__init__(**kwargs) # Set up the widgets orientations self.padding = 10 self.orientation = 'vertical' # Play the recorder self.rec = rec self.rec.stream_init(playback = True) self.playing = True # Set up the plot canvas fig = plt.figure() ax = fig.add_subplot(111) ax.set_ylim(-5e4,5e4) self.line = ax.plot(range(len(self.rec.signal_data)), self.rec.signal_data)[0] self.canvas_widget = FigureCanvas(fig) self.add_widget(self.canvas_widget) # Set up the button btn = Button(text='Switch') btn.bind(on_press = self.toggle_rec) self.add_widget(btn) btn.size_hint_y = 0.1 # Set up live update plot event = Clock.schedule_interval(self.update_line, 1 / 60.) #--------------- UI Callback Methods --------------------- def update_line(self,dt): self.line.set_ydata(self.rec.signal_data) self.canvas_widget.draw() def toggle_rec(self,*args): if self.playing: self.rec.stream_stop() else: self.rec.stream_start() self.playing = not self.playing
class GUIGraph: graphs = {} oscope_default_state = { 'T': { 'plot_type': 'T', 'xlabel': 'Time [s]', 'ylabel': 'ADC count', 'xlim': [0, 0.01], 'ylim': [-1., 1.], 'autoscale': False, 'ylog': False, 'xlog': False }, 'F': { 'plot_type': 'F', 'xlabel': 'Frequency [Hz]', 'ylabel': '[V/sqrt(Hz)]', 'xlim': [0, 5e6], 'ylim': [1e-11, 60000], 'autoscale': True, 'ylog': False, 'xlog': False }, 'active_graphs': [] } def __init__(self, ch_id, plot_info, show=False, label='Noname', **kwargs): self.ch_id = ch_id self.show = show self.label = label self.fig = plt.figure() self.wid = FigureCanvas(self.fig) self.ax = self.fig.add_subplot(111, **kwargs) self.line = self.ax.plot(range(100), [1 / 2] * 100, label=self.label)[0] self.ax.set_xlabel(plot_info.xlabel) self.ax.set_ylabel(plot_info.ylabel) self.ax.legend() self.ax.grid(True) self.carrier_ch_n = int(ch_id[1]) if int(ch_id[0]) < 2 else None self.plot_info = plot_info self._old_results = [] def update_data(self): if self.ch_id not in carrier.results: # Logger.critical('No data found') return if carrier.results[self.ch_id] is self._old_results: return if self.plot_info.plot_type == 'T': x_data, y_data, y_max, ts = carrier.results[self.ch_id] elif self.plot_info.plot_type == 'F': x_data, y_data, xargmax, y_max = carrier.results[self.ch_id] self.line.set_xdata(x_data) self.line.set_ydata(y_data) self._old_results = carrier.results[self.ch_id] if not self.plot_info.autoscale: self.ax.set_xlim(self.plot_info.xlim) self.ax.set_ylim(self.plot_info.ylim) else: self.ax.relim() self.ax.autoscale() self.wid.draw() self.wid.flush_events() def set_plot_active(self, b): self.show = b @staticmethod def load_settings(settings_file='oscope_state.json'): try: with open(settings_file, 'r') as jf: oscope_state = json.load(jf) except IOError as e: Logger.warning( f'{e} occured while reading settings_file, loading default settings' ) oscope_state = GUIGraph.oscope_default_state except json.JSONDecodeError as e: Logger.warning( f'{e} occured while reading settings_file, loading default settings' ) oscope_state = GUIGraph.oscope_default_state return oscope_state @staticmethod def save_settings(settings_file='oscope_state.json'): GUIGraph.oscope_state['T'] = dataclasses.asdict( g_plot_type_limits['T']) GUIGraph.oscope_state['F'] = dataclasses.asdict( g_plot_type_limits['F']) GUIGraph.oscope_state['active_graphs'] = [ g.ch_id for g in GUIGraph.graphs.values() if g.show ] try: with open(settings_file, 'w') as jf: json.dump(GUIGraph.oscope_state, jf) except IOError: Logger.warning('State of oscope couldn\'t be saved') @staticmethod def setup_gui_graphs(carrier): GUIGraph.oscope_state = GUIGraph.load_settings() # Load possible types of limits: For now T for time domain, # and F for frequency g_plot_type_limits['T'] = GUIGraphLimits(**GUIGraph.oscope_state['T']) g_plot_type_limits['F'] = GUIGraphLimits(**GUIGraph.oscope_state['F']) # Create 2 GUI graphs for each channel of data coming from the carrier # 2 graphs, 1 for T and 1 for F for i in range(carrier.n_channels): ch_id = "0" + str(i) GUIGraph.graphs[ch_id] = GUIGraph( ch_id, g_plot_type_limits['T'], show=ch_id in GUIGraph.oscope_state['active_graphs'], label='CH{}-TimeDomain'.format(i)) ch_id = "1" + str(i) GUIGraph.graphs[ch_id] = GUIGraph( ch_id, g_plot_type_limits['F'], show=ch_id in GUIGraph.oscope_state['active_graphs'], label='CH{}-Frequency'.format(i)) ch_id = "3" GUIGraph.graphs[ch_id] = GUIGraph( ch_id, g_plot_type_limits['F'], show=ch_id in GUIGraph.oscope_state['active_graphs'], label='CSD')