当前位置: 首页>>代码示例>>Python>>正文


Python RectangleSelector.update方法代码示例

本文整理汇总了Python中matplotlib.widgets.RectangleSelector.update方法的典型用法代码示例。如果您正苦于以下问题:Python RectangleSelector.update方法的具体用法?Python RectangleSelector.update怎么用?Python RectangleSelector.update使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在matplotlib.widgets.RectangleSelector的用法示例。


在下文中一共展示了RectangleSelector.update方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: ImageView

# 需要导入模块: from matplotlib.widgets import RectangleSelector [as 别名]
# 或者: from matplotlib.widgets.RectangleSelector import update [as 别名]

#.........这里部分代码省略.........
        check_disable_mpl_callbacks()

    def set_data(self, data, bands=None, **kwargs):
        '''Sets the data to be shown in the RGB channels.
        
        Arguments:

            `data` (ndarray or SpyImage):

                If `data` has more than 3 bands, the `bands` argument can be
                used to specify which 3 bands to display. `data` will be
                passed to `get_rgb` prior to display.

            `bands` (3-tuple of int):

                Indices of the 3 bands to display from `data`.

        Keyword Arguments:

            Any valid keyword for `get_rgb` or `matplotlib.imshow` can be
            given.
        '''
        from .graphics import _get_rgb_kwargs

        self.data = data
        self.bands = bands

        rgb_kwargs = {}
        for k in _get_rgb_kwargs:
            if k in kwargs:
                rgb_kwargs[k] = kwargs.pop(k)
        self.set_rgb_options(**rgb_kwargs)

        self._update_data_rgb()

        if self._image_shape is None:
            self._image_shape = data.shape[:2]
        elif data.shape[:2] != self._image_shape:
            raise ValueError('Image shape is inconsistent with previously ' \
                             'set data.')
        self.imshow_data_kwargs.update(kwargs)
        if 'interpolation' in self.imshow_data_kwargs:
            self.interpolation = self.imshow_data_kwargs['interpolation']
            self.imshow_data_kwargs.pop('interpolation')

        if len(kwargs) > 0 and self.is_shown:
            msg = 'Keyword args to set_data only have an effect if ' \
              'given before the image is shown.'
            warnings.warn(UserWarning(msg))
        if self.is_shown:
            self.refresh()

    def set_rgb_options(self, **kwargs):
        '''Sets parameters affecting RGB display of data.

        Accepts any keyword supported by :func:`~spectral.graphics.graphics.get_rgb`.
        '''
        from .graphics import _get_rgb_kwargs

        for k in kwargs:
            if k not in _get_rgb_kwargs:
                raise ValueError('Unexpected keyword: {0}'.format(k))
        self.rgb_kwargs = kwargs.copy()
        if self.is_shown:
            self._update_data_rgb()
            self.refresh()
开发者ID:spectralpython,项目名称:spectral,代码行数:70,代码来源:spypylab.py

示例2: InteractiveCut

# 需要导入模块: from matplotlib.widgets import RectangleSelector [as 别名]
# 或者: from matplotlib.widgets.RectangleSelector import update [as 别名]
class InteractiveCut(object):

    def __init__(self, slice_plot, canvas, ws_title):
        self.slice_plot = slice_plot
        self._canvas = canvas
        self._ws_title = ws_title
        self._en_unit = slice_plot.get_slice_cache().energy_axis.e_unit
        self._en_from_meV = EnergyUnits(self._en_unit).factor_from_meV()

        self.horizontal = None
        self.connect_event = [None, None, None, None]
        # We need to access the CutPlotterPresenter instance of the particular CutPlot (window) we are using
        # But there is no way to get without changing the active category then calling the GlobalFigureManager.
        # So we create a new temporary here. After the first time we plot a 1D plot, the correct category is set
        # and we can get the correct CutPlot instance and its CutPlotterPresenter
        self._cut_plotter_presenter = CutPlotterPresenter()
        self._is_initial_cut_plotter_presenter = True
        self._rect_pos_cache = [0, 0, 0, 0, 0, 0]
        self.rect = RectangleSelector(self._canvas.figure.gca(), self.plot_from_mouse_event,
                                      drawtype='box', useblit=True,
                                      button=[1, 3], spancoords='pixels', interactive=True)

        self.connect_event[3] = self._canvas.mpl_connect('draw_event', self.redraw_rectangle)
        self._canvas.draw()

    def plot_from_mouse_event(self, eclick, erelease):
        # Make axis orientation sticky, until user selects entirely new rectangle.
        rect_pos = [eclick.x, eclick.y, erelease.x, erelease.y,
                    abs(erelease.x - eclick.x), abs(erelease.y - eclick.y)]
        rectangle_changed = all([abs(rect_pos[i] - self._rect_pos_cache[i]) > 0.1 for i in range(6)])
        if rectangle_changed:
            self.horizontal = abs(erelease.x - eclick.x) > abs(erelease.y - eclick.y)
        self.plot_cut(eclick.xdata, erelease.xdata, eclick.ydata, erelease.ydata)
        self.connect_event[2] = self._canvas.mpl_connect('button_press_event', self.clicked)
        self._rect_pos_cache = rect_pos

    def plot_cut(self, x1, x2, y1, y2, store=False):
        if x2 > x1 and y2 > y1:
            ax, integration_start, integration_end = self.get_cut_parameters((x1, y1), (x2, y2))
            units = self._canvas.figure.gca().get_yaxis().units if self.horizontal else \
                self._canvas.figure.gca().get_xaxis().units
            integration_axis = Axis(units, integration_start, integration_end, 0, self._en_unit)
            cut = Cut(ax, integration_axis, None, None)
            self._cut_plotter_presenter.plot_interactive_cut(str(self._ws_title), cut, store)
            self._cut_plotter_presenter.set_is_icut(True)
            if self._is_initial_cut_plotter_presenter:
                # First time we've plotted a 1D cut - get the true CutPlotterPresenter
                from mslice.plotting.pyplot import GlobalFigureManager
                self._cut_plotter_presenter = GlobalFigureManager.get_active_figure().plot_handler._cut_plotter_presenter
                self._is_initial_cut_plotter_presenter = False
                GlobalFigureManager.disable_make_current()
            self._cut_plotter_presenter.store_icut(self)

    def get_cut_parameters(self, pos1, pos2):
        start = pos1[not self.horizontal]
        end = pos2[not self.horizontal]
        units = self._canvas.figure.gca().get_xaxis().units if self.horizontal else \
            self._canvas.figure.gca().get_yaxis().units
        step = get_limits(get_workspace_handle(self._ws_title), units)[2] * self._en_from_meV
        ax = Axis(units, start, end, step, self._en_unit)
        integration_start = pos1[self.horizontal]
        integration_end = pos2[self.horizontal]
        return ax, integration_start, integration_end

    def clicked(self, event):
        self.connect_event[0] = self._canvas.mpl_connect('motion_notify_event',
                                                         lambda x: self.plot_cut(*self.rect.extents))
        self.connect_event[1] = self._canvas.mpl_connect('button_release_event', self.end_drag)

    def end_drag(self, event):
        self._canvas.mpl_disconnect(self.connect_event[0])
        self._canvas.mpl_disconnect(self.connect_event[1])

    def redraw_rectangle(self, event):
        if self.rect.active:
            self.rect.update()

    def save_cut(self):
        x1, x2, y1, y2 = self.rect.extents
        self.plot_cut(x1, x2, y1, y2, store=True)
        self.update_workspaces()
        ax, integration_start, integration_end = self.get_cut_parameters((x1, y1), (x2, y2))
        return output_workspace_name(str(self._ws_title), integration_start, integration_end)

    def update_workspaces(self):
        self.slice_plot.update_workspaces()

    def clear(self):
        self._cut_plotter_presenter.set_is_icut(False)
        self.rect.set_active(False)
        for event in self.connect_event:
            self._canvas.mpl_disconnect(event)
        self._canvas.draw()

    def flip_axis(self):
        self.horizontal = not self.horizontal
        self.plot_cut(*self.rect.extents)

    def window_closing(self):
        self.slice_plot.toggle_interactive_cuts()
#.........这里部分代码省略.........
开发者ID:mantidproject,项目名称:mslice,代码行数:103,代码来源:interactive_cut.py


注:本文中的matplotlib.widgets.RectangleSelector.update方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。