def create_figure(self): self.figure = utils.create_figure( window_title=("Figure " + self.title if self.title else None)) canvas = self.figure.canvas self._background = canvas.copy_from_bbox(self.figure.bbox) self.figure.canvas.mpl_connect('draw_event', self._on_draw) utils.on_figure_window_close(self.figure, self.close)
def plot(self, auto_update_plot = True): """Plots the current spectrum to the screen and a map with a cursor to explore the SI. """ # If new coordinates are assigned self.spectrum.plot() _plot = self.spectrum._plot l1 = _plot.spectrum_plot.ax_lines[0] color = l1.line.get_color() l1.line_properties_helper(color, 'scatter') l1.set_properties() l2 = hyperspy.drawing.spectrum.SpectrumLine() l2.data_function = self._model2plot l2.line_properties_helper('blue', 'line') # Add the line to the figure _plot.spectrum_plot.add_line(l2) l2.plot() self.connect_parameters2update_plot() on_figure_window_close(_plot.spectrum_plot.figure, self.disconnect_parameters2update_plot) self.set_auto_update_plot(True) self._plot = self.spectrum._plot
def plot_signal(self): if self.signal_plot is not None: self.signal_plot.plot() return # Create the figure self.xlabel = '%s' % str(self.axes_manager.signal_axes[0]) if self.axes_manager.signal_axes[0].units is not Undefined: self.xlabel += ' (%s)' % self.axes_manager.signal_axes[0].units self.ylabel = 'Intensity' self.axis = self.axes_manager.signal_axes[0].axis sf = spectrum.SpectrumFigure(title=self.signal_title + " Signal") sf.xlabel = self.xlabel sf.ylabel = self.ylabel sf.axis = self.axis sf.create_axis() sf.axes_manager = self.axes_manager self.signal_plot = sf # Create a line to the left axis with the default indices sl = spectrum.SpectrumLine() sl.autoscale = True sl.data_function = self.signal_data_function sl.plot_indices = True if self.pointer is not None: color = self.pointer.color else: color = 'red' sl.set_line_properties(color=color, type='step') # Add the line to the figure sf.add_line(sl) # If the data is complex create a line in the left axis with the # default coordinates sl = spectrum.SpectrumLine() sl.data_function = self.signal_data_function sl.plot_coordinates = True sl.get_complex = any(np.iscomplex(sl.data_function())) if sl.get_complex: sl.set_line_properties(color="blue", type='step') # Add extra line to the figure sf.add_line(sl) self.signal_plot = sf sf.plot() if self.navigator_plot is not None and sf.figure is not None: utils.on_figure_window_close(self.navigator_plot.figure, self._on_navigator_plot_closing) utils.on_figure_window_close(sf.figure, self.close_navigator_plot) self._key_nav_cid = \ self.signal_plot.figure.canvas.mpl_connect( 'key_press_event', self.axes_manager.key_navigator) self._key_nav_cid = \ self.navigator_plot.figure.canvas.mpl_connect( 'key_press_event', self.axes_manager.key_navigator) self.signal_plot.figure.canvas.mpl_connect( 'key_press_event', self.key2switch_right_pointer) self.navigator_plot.figure.canvas.mpl_connect( 'key_press_event', self.key2switch_right_pointer)
def create_figure(self, max_size=8, min_size=2): if self.plot_scalebar is True: wfactor = 1.1 else: wfactor = 1 height = abs(self._extent[3] - self._extent[2]) * self._aspect width = abs(self._extent[1] - self._extent[0]) figsize = np.array((width * wfactor, height)) * max_size / max((width * wfactor, height)) self.figure = utils.create_figure( window_title=("Figure " + self.title if self.title else None), figsize=figsize.clip(min_size, max_size) ) self.figure.canvas.mpl_connect("draw_event", self._on_draw) utils.on_figure_window_close(self.figure, self.close)
def enable_adjust_position(self, components=None, fix_them=True, show_label=True): """Allow changing the *x* position of component by dragging a vertical line that is plotted in the signal model figure Parameters ---------- components : {None, list of components} If None, the position of all the active components of the model that has a well defined *x* position with a value in the axis range will get a position adjustment line. Otherwise the feature is added only to the given components. The components can be specified by name, index or themselves. fix_them : bool If True the position parameter of the components will be temporarily fixed until adjust position is disable. This can be useful to iteratively adjust the component positions and fit the model. show_label : bool, optional If True, a label showing the component name is added to the plot next to the vertical line. See also -------- disable_adjust_position """ if (self._plot is None or self._plot.is_active() is False): self.plot() if self._position_widgets: self.disable_adjust_position() on_figure_window_close(self._plot.signal_plot.figure, self.disable_adjust_position) if components: components = [self._get_component(x) for x in components] else: self._adjust_position_all = (fix_them, show_label) components = components if components else self if not components: # The model does not have components so we do nothing return components = [ component for component in components if component.active ] for component in components: self._make_position_adjuster(component, fix_them, show_label)
def plot_signal(self): if self.signal_plot is not None: self.signal_plot.plot() return # Create the figure self.xlabel = '%s (%s)' % ( self.axes_manager.signal_axes[0].name, self.axes_manager.signal_axes[0].units) self.ylabel = 'Intensity' self.axis = self.axes_manager.signal_axes[0].axis sf = spectrum.SpectrumFigure() sf.xlabel = self.xlabel sf.ylabel = self.ylabel sf.title = self.signal_title sf.axis = self.axis sf.create_axis() sf.axes_manager = self.axes_manager self.signal_plot = sf # Create a line to the left axis with the default indices sl = spectrum.SpectrumLine() sl.data_function = self.signal_data_function sl.plot_indices = True if self.pointer is not None: color = self.pointer.color else: color = 'red' sl.line_properties_helper(color, 'step') # Add the line to the figure sf.add_line(sl) self.signal_plot = sf sf.plot() if self.navigator_plot is not None and sf.figure is not None: utils.on_figure_window_close(self.navigator_plot.figure, self._disconnect) utils.on_figure_window_close(sf.figure, self.close_navigator_plot) self._key_nav_cid = \ self.signal_plot.figure.canvas.mpl_connect( 'key_press_event', self.axes_manager.key_navigator) self._key_nav_cid = \ self.navigator_plot.figure.canvas.mpl_connect( 'key_press_event', self.axes_manager.key_navigator) self.signal_plot.figure.canvas.mpl_connect( 'key_press_event', self.key2switch_right_pointer) self.navigator_plot.figure.canvas.mpl_connect( 'key_press_event', self.key2switch_right_pointer)
def create_figure(self, max_size=8, min_size=2): if self.scalebar is True: wfactor = 1.1 else: wfactor = 1 height = abs(self._extent[3] - self._extent[2]) * self._aspect width = abs(self._extent[1] - self._extent[0]) figsize = np.array((width * wfactor, height)) * max_size / max( (width * wfactor, height)) self.figure = utils.create_figure( window_title=("Figure " + self.title if self.title else None), figsize=figsize.clip(min_size, max_size)) self.figure.canvas.mpl_connect('draw_event', self._on_draw) utils.on_figure_window_close(self.figure, self._on_close)
def enable_adjust_position( self, components=None, fix_them=True, show_label=True): """Allow changing the *x* position of component by dragging a vertical line that is plotted in the signal model figure Parameters ---------- components : {None, list of components} If None, the position of all the active components of the model that has a well defined *x* position with a value in the axis range will get a position adjustment line. Otherwise the feature is added only to the given components. The components can be specified by name, index or themselves. fix_them : bool If True the position parameter of the components will be temporarily fixed until adjust position is disable. This can be useful to iteratively adjust the component positions and fit the model. show_label : bool, optional If True, a label showing the component name is added to the plot next to the vertical line. See also -------- disable_adjust_position """ if (self._plot is None or self._plot.is_active() is False): self.plot() if self._position_widgets: self.disable_adjust_position() on_figure_window_close(self._plot.signal_plot.figure, self.disable_adjust_position) if components: components = [self._get_component(x) for x in components] else: self._adjust_position_all = (fix_them, show_label) components = components if components else self if not components: # The model does not have components so we do nothing return components = [ component for component in components if component.active] for component in components: self._make_position_adjuster(component, fix_them, show_label)
def create_figure(self, **kwargs): """Create matplotlib figure Parameters ---------- **kwargs All keyword arguments are passed to ``plt.figure``. """ self.figure = utils.create_figure( window_title="Figure " + self.title if self.title else None, **kwargs) utils.on_figure_window_close(self.figure, self._on_close) if self.figure.canvas.supports_blit: self._draw_event_cid = self.figure.canvas.mpl_connect( 'draw_event', self._on_blit_draw)
def create_figure(self, **kwargs): """Create matplotlib figure Parameters ---------- **kwargs All keyword arguments are passed to ``plt.figure``. """ self.figure = utils.create_figure(window_title="Figure " + self.title if self.title else None, **kwargs) utils.on_figure_window_close(self.figure, self._on_close) if self.figure.canvas.supports_blit: self._draw_event_cid = self.figure.canvas.mpl_connect( 'draw_event', self._on_blit_draw)
def virtual_aperture(self, signal=None, annulus=False, navigate=False): ui = self.ui if signal is None: signal = ui.get_selected_signal() dd = np.array([ a.high_value + a.low_value for a in signal.axes_manager.signal_axes ]) / 2.0 r = hs.roi.CircleROI(dd[0], dd[1], signal.axes_manager.signal_axes[0].scale * 3) if annulus: r.r_inner = signal.axes_manager.signal_axes[0].scale * 2 s_virtual = r.interactive(signal, None, axes=signal.axes_manager.signal_axes) s_nav = hs.interactive(s_virtual.mean, s_virtual.events.data_changed, axis=s_virtual.axes_manager.signal_axes) s_nav.axes_manager.set_signal_dimension(2) if navigate: signal.plot(navigator=s_nav) signal._plot.navigator_plot.update() s_nav.events.data_changed.connect( signal._plot.navigator_plot.update, []) utils.on_figure_window_close(signal._plot.navigator_plot.figure, partial(self._on_close, r)) else: s_nav.plot() utils.on_figure_window_close(s_nav._plot.signal_plot.figure, partial(self._on_close, r)) if navigate: r.add_widget(signal, axes=signal.axes_manager.signal_axes, color='darkorange') else: r.add_widget(signal, axes=signal.axes_manager.signal_axes) self._rois.append(r) self.record_code("<p>.virtual_aperture(navigate=%s)" % navigate)
def virtual_aperture(self, signal=None, annulus=False, navigate=False): ui = self.ui if signal is None: signal = ui.get_selected_signal() dd = np.array([a.high_value + a.low_value for a in signal.axes_manager.signal_axes]) / 2.0 r = hs.roi.CircleROI(dd[0], dd[1], signal.axes_manager.signal_axes[0].scale*3) if annulus: r.r_inner = signal.axes_manager.signal_axes[0].scale*2 s_virtual = r.interactive(signal, None, axes=signal.axes_manager.signal_axes) s_nav = hs.interactive( s_virtual.mean, s_virtual.events.data_changed, axis=s_virtual.axes_manager.signal_axes) s_nav.axes_manager.set_signal_dimension(2) if navigate: signal.plot(navigator=s_nav) signal._plot.navigator_plot.update() s_nav.events.data_changed.connect( signal._plot.navigator_plot.update, []) utils.on_figure_window_close( signal._plot.navigator_plot.figure, partial(self._on_close, r)) else: s_nav.plot() utils.on_figure_window_close( s_nav._plot.signal_plot.figure, partial(self._on_close, r)) if navigate: r.add_widget(signal, axes=signal.axes_manager.signal_axes, color='darkorange') else: r.add_widget(signal, axes=signal.axes_manager.signal_axes) self._rois.append(r) self.record_code("<p>.virtual_aperture(navigate=%s)" % navigate)
def plot_signal(self): if self.signal_plot is not None: self.signal_plot.plot() return imf = image.ImagePlot() imf.axes_manager = self.axes_manager imf.data_function = self.signal_data_function imf.title = self.signal_title + " Signal" imf.xaxis, imf.yaxis = self.axes_manager.signal_axes imf.plot_colorbar = True imf.plot() self.signal_plot = imf if self.navigator_plot is not None and imf.figure is not None: utils.on_figure_window_close(self.navigator_plot.figure, self.close_navigator_plot) utils.on_figure_window_close(imf.figure, self.close_navigator_plot) self._key_nav_cid = \ self.signal_plot.figure.canvas.mpl_connect( 'key_press_event', self.axes_manager.key_navigator) self._key_nav_cid = \ self.navigator_plot.figure.canvas.mpl_connect( 'key_press_event', self.axes_manager.key_navigator)
def plot(self, plot_components=False): """Plots the current spectrum to the screen and a map with a cursor to explore the SI. Parameters ---------- plot_components : bool If True, add a line per component to the signal figure. """ # If new coordinates are assigned self.spectrum.plot() _plot = self.spectrum._plot l1 = _plot.signal_plot.ax_lines[0] color = l1.line.get_color() l1.set_line_properties(color=color, type='scatter') l2 = hyperspy.drawing.spectrum.SpectrumLine() l2.data_function = self._model2plot l2.set_line_properties(color='blue', type='line') # Add the line to the figure _plot.signal_plot.add_line(l2) l2.plot() on_figure_window_close(_plot.signal_plot.figure, self._close_plot) self._model_line = l2 self._plot = self.spectrum._plot self._connect_parameters2update_plot(self) if plot_components is True: self.enable_plot_components() else: # If we were plotted before, make sure we reset state here self.disable_plot_components() # If we were plotted before, make sure we reset state here self.disable_adjust_position()
def plot_signal(self): if self.signal_plot is not None: self.signal_plot.plot() return imf = image.ImagePlot() imf.axes_manager = self.axes_manager imf.data_function = self.signal_data_function imf.title = self.signal_title + " Signal" imf.xaxis, imf.yaxis = self.axes_manager.signal_axes imf.plot_colorbar = True imf.plot() self.signal_plot = imf if self.navigator_plot is not None and imf.figure is not None: utils.on_figure_window_close(self.navigator_plot.figure, self.close_navigator_plot) utils.on_figure_window_close( imf.figure, self.close_navigator_plot) self._key_nav_cid = \ self.signal_plot.figure.canvas.mpl_connect( 'key_press_event', self.axes_manager.key_navigator) self._key_nav_cid = \ self.navigator_plot.figure.canvas.mpl_connect( 'key_press_event', self.axes_manager.key_navigator)
def connect(self, ax): """Connect to the matplotlib Axes' events. """ on_figure_window_close(ax.figure, self.close) if self._navigating: self.connect_navigate()
def create_figure(self): self.figure = utils.create_figure( window_title="Figure " + self.title if self.title else None) utils.on_figure_window_close(self.figure, self.close) self.figure.canvas.mpl_connect('draw_event', self._on_draw)
def create_figure(self): self.figure = utils.create_figure(window_title="Figure " + self.title if self.title else None) utils.on_figure_window_close(self.figure, self.close) self.figure.canvas.mpl_connect('draw_event', self._on_draw)
def plot_signal(self, colorbar=True, scalebar=True, scalebar_color="white", axes_ticks=None, auto_contrast=True, saturated_pixels=0.2, vmin=None, vmax=None, no_nans=False, **kwargs ): """Plot image. Parameters ---------- colorbar : bool, optional If true, a colorbar is plotted for non-RGB images. scalebar : bool, optional If True and the units and scale of the x and y axes are the same a scale bar is plotted. scalebar_color : str, optional A valid MPL color string; will be used as the scalebar color. axes_ticks : {None, bool}, optional If True, plot the axes ticks. If None axes_ticks are only plotted when the scale bar is not plotted. If False the axes ticks are never plotted. auto_contrast : bool, optional If True, the contrast is stretched for each image using the `saturated_pixels` value. Default True saturated_pixels: scalar The percentage of pixels that are left out of the bounds. For example, the low and high bounds of a value of 1 are the 0.5% and 99.5% percentiles. It must be in the [0, 100] range. vmin, vmax : scalar, optional `vmin` and `vmax` are used to normalize luminance data. If `auto_contrast` is True (i.e. default) these values are ignore. no_nans : bool, optional If True, set nans to zero for plotting. **kwargs, optional Additional key word arguments passed to matplotlib.imshow() """ if self.signal_plot is not None: self.signal_plot.plot(**kwargs) return imf = image.ImagePlot() imf.axes_manager = self.axes_manager imf.data_function = self.signal_data_function imf.title = self.signal_title + " Signal" imf.xaxis, imf.yaxis = self.axes_manager.signal_axes imf.colorbar = colorbar imf.scalebar = scalebar imf.axes_ticks = axes_ticks imf.vmin, imf.vmax = vmin, vmax imf.saturated_pixels = saturated_pixels imf.no_nans = no_nans imf.scalebar_color = scalebar_color imf.auto_contrast = auto_contrast imf.plot(**kwargs) self.signal_plot = imf if self.navigator_plot is not None and imf.figure is not None: utils.on_figure_window_close(self.navigator_plot.figure, self.close_navigator_plot) utils.on_figure_window_close( imf.figure, self.close_navigator_plot) self._key_nav_cid = \ self.signal_plot.figure.canvas.mpl_connect( 'key_press_event', self.axes_manager.key_navigator) self._key_nav_cid = \ self.navigator_plot.figure.canvas.mpl_connect( 'key_press_event', self.axes_manager.key_navigator)
def plot_signal(self): if self.signal_plot is not None: self.signal_plot.plot() return # Create the figure self.xlabel = '%s' % str(self.axes_manager.signal_axes[0]) if self.axes_manager.signal_axes[0].units is not Undefined: self.xlabel += ' (%s)' % self.axes_manager.signal_axes[0].units self.ylabel = 'Intensity' self.axis = self.axes_manager.signal_axes[0] sf = spectrum.SpectrumFigure(title=self.signal_title + " Signal") sf.xlabel = self.xlabel sf.ylabel = self.ylabel sf.axis = self.axis sf.create_axis() sf.axes_manager = self.axes_manager self.signal_plot = sf # Create a line to the left axis with the default indices sl = spectrum.SpectrumLine() sl.autoscale = True sl.data_function = self.signal_data_function sl.plot_indices = True if self.pointer is not None: color = self.pointer.color else: color = 'red' sl.set_line_properties(color=color, type='step') # Add the line to the figure sf.add_line(sl) # If the data is complex create a line in the left axis with the # default coordinates sl = spectrum.SpectrumLine() sl.data_function = self.signal_data_function sl.plot_coordinates = True sl.get_complex = any(np.iscomplex(sl.data_function())) if sl.get_complex: sl.set_line_properties(color="blue", type='step') # Add extra line to the figure sf.add_line(sl) self.signal_plot = sf sf.plot() if self.navigator_plot is not None and sf.figure is not None: utils.on_figure_window_close(self.navigator_plot.figure, self._on_navigator_plot_closing) utils.on_figure_window_close(sf.figure, self.close_navigator_plot) self._key_nav_cid = \ self.signal_plot.figure.canvas.mpl_connect( 'key_press_event', self.axes_manager.key_navigator) self._key_nav_cid = \ self.navigator_plot.figure.canvas.mpl_connect( 'key_press_event', self.axes_manager.key_navigator) self.signal_plot.figure.canvas.mpl_connect( 'key_press_event', self.key2switch_right_pointer) self.navigator_plot.figure.canvas.mpl_connect( 'key_press_event', self.key2switch_right_pointer)
def plot_signal(self, colorbar=True, scalebar=True, scalebar_color="white", axes_ticks=None, auto_contrast=True, saturated_pixels=0.2, vmin=None, vmax=None, no_nans=False, centre_colormap="auto", **kwargs ): """Plot image. Parameters ---------- colorbar : bool, optional If true, a colorbar is plotted for non-RGB images. scalebar : bool, optional If True and the units and scale of the x and y axes are the same a scale bar is plotted. scalebar_color : str, optional A valid MPL color string; will be used as the scalebar color. axes_ticks : {None, bool}, optional If True, plot the axes ticks. If None axes_ticks are only plotted when the scale bar is not plotted. If False the axes ticks are never plotted. auto_contrast : bool, optional If True, the contrast is stretched for each image using the `saturated_pixels` value. Default True saturated_pixels: scalar The percentage of pixels that are left out of the bounds. For example, the low and high bounds of a value of 1 are the 0.5% and 99.5% percentiles. It must be in the [0, 100] range. vmin, vmax : scalar, optional `vmin` and `vmax` are used to normalize luminance data. If `auto_contrast` is True (i.e. default) these values are ignore. no_nans : bool, optional If True, set nans to zero for plotting. **kwargs, optional Additional key word arguments passed to matplotlib.imshow() """ if self.signal_plot is not None: self.signal_plot.plot(**kwargs) return imf = image.ImagePlot() imf.axes_manager = self.axes_manager imf.data_function = self.signal_data_function imf.title = self.signal_title + " Signal" imf.xaxis, imf.yaxis = self.axes_manager.signal_axes imf.colorbar = colorbar imf.scalebar = scalebar imf.axes_ticks = axes_ticks imf.vmin, imf.vmax = vmin, vmax imf.saturated_pixels = saturated_pixels imf.no_nans = no_nans imf.scalebar_color = scalebar_color imf.auto_contrast = auto_contrast imf.centre_colormap = centre_colormap imf.plot(**kwargs) self.signal_plot = imf if self.navigator_plot is not None and imf.figure is not None: utils.on_figure_window_close(self.navigator_plot.figure, self.close_navigator_plot) utils.on_figure_window_close( imf.figure, self.close_navigator_plot) self._key_nav_cid = \ self.signal_plot.figure.canvas.mpl_connect( 'key_press_event', self.axes_manager.key_navigator) self._key_nav_cid = \ self.navigator_plot.figure.canvas.mpl_connect( 'key_press_event', self.axes_manager.key_navigator)
def enable_adjust_position(self, components=None, fix_them=True): """Allow changing the *x* position of component by dragging a vertical line that is plotted in the signal model figure Parameters ---------- components : {None, list of components} If None, the position of all the active components of the model that has a well defined *x* position with a value in the axis range will get a position adjustment line. Otherwise the feature is added only to the given components. fix_them : bool If True the position parameter of the components will be temporarily fixed until adjust position is disable. This can be useful to iteratively adjust the component positions and fit the model. See also -------- disable_adjust_position """ if (self._plot is None or self._plot.is_active() is False): self.plot() if self._position_widgets: self.disable_adjust_position() on_figure_window_close(self._plot.signal_plot.figure, self.disable_adjust_position) components = components if components else self if not components: # The model does not have components so we do nothing return components = [ component for component in components if component.active] axis_dict = self.axes_manager.signal_axes[0].get_axis_dictionary() axis_dict['index_in_array'] = 0 for component in self: if (component._position is not None and not component._position.twin): set_value = component._position._setvalue get_value = component._position._getvalue else: continue # Create an AxesManager for the widget am = AxesManager([axis_dict,]) am.axes[0].navigate = True try: am.axes[0].value = get_value() except TraitError: # The value is outside of the axis range continue # Create the vertical line and labels self._position_widgets.extend(( DraggableVerticalLine(am), DraggableLabel(am),)) # Store the component to reset its twin when disabling # adjust position self._position_widgets[-1].component = component w = self._position_widgets[-1] w.string = component._get_short_description().replace( ' component', '') w.add_axes(self._plot.signal_plot.ax) self._position_widgets[-2].add_axes( self._plot.signal_plot.ax) # Create widget -> parameter connection am.axes[0].continuous_value = True am.axes[0].on_trait_change(set_value, 'value') # Create parameter -> widget connection # This is done with a duck typing trick # We disguise the AxesManager axis of Parameter by adding # the _twin attribute am.axes[0]._twins = set() component._position.twin = am.axes[0]