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


Python Rectangle.get_bbox方法代码示例

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


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

示例1: close

# 需要导入模块: from matplotlib.patches import Rectangle [as 别名]
# 或者: from matplotlib.patches.Rectangle import get_bbox [as 别名]
class click_window:
   '''An interactive window.  Given an axis instance and a start point
   (x0,y0), draw a dynamic rectangle that follows the mouse until
   the close() function is called (which returns the coordinates of
   the final rectangle.  Useful or selecting out square regions.'''

   def __init__(self, ax, x0, y0):
      self.ax = ax
      self.x0 = x0
      self.y0 = y0
      self.rect = Rectangle((x0,y0), width=0, height=0, alpha=0.1)
      ax.add_artist(self.rect)

   def connect(self):
      self.cidmotion = self.rect.figure.canvas.mpl_connect(
            'motion_notify_event', self.on_motion)

   def on_motion(self, event):
      # Have we left the axes?
      if event.inaxes != self.rect.axes:  return

      self.rect.set_width(event.xdata - self.x0)
      self.rect.set_height(event.ydata - self.y0)
      self.ax.figure.canvas.draw()

   def close(self):
      self.rect.figure.canvas.mpl_disconnect(self.cidmotion)
      extent = self.rect.get_bbox().get_points()
      self.rect.remove()
      self.ax.figure.canvas.draw()
      return(list(ravel(extent)))
开发者ID:obscode,项目名称:snpy,代码行数:33,代码来源:plot_sne_mpl.py

示例2: Brusher

# 需要导入模块: from matplotlib.patches import Rectangle [as 别名]
# 或者: from matplotlib.patches.Rectangle import get_bbox [as 别名]

#.........这里部分代码省略.........


    def button_press(self, event):
        ''' handles a mouse click '''
        # if clicked outside of axes, ignore
        if not event.inaxes: return

        # remove old brush object if it's there
        if self.brush:
            self.brush.remove()
            self.unbrush_me()
            plt.draw()
    
        # start a brush here
        self.button_down = True
        self.start_xy    = (event.xdata,event.ydata)
        self.brush       = Rectangle( self.start_xy, 0,0, facecolor='gray', alpha=.15 )
        self.brush_axis  = event.inaxes
        
        # add brush to axis
        event.inaxes.add_patch(self.brush)
        plt.draw()
        
        
    def mouse_motion(self, event):
        ''' handles the mouse moving around '''
        # if we haven't already clicked and started a brush, ignore the motion
        if not (self.button_down and self.brush): return
        # if we've moved the mouse outside the starting axis, ignore
        if self.brush_axis != event.inaxes: return

        # otherwise, track the motion and update the brush
        self.brush.set_width( abs(self.start_xy[0] - event.xdata) )
        self.brush.set_height( abs(self.start_xy[1] - event.ydata) )
        self.brush.set_x( min(self.start_xy[0], event.xdata) )
        self.brush.set_y( min(self.start_xy[1], event.ydata) )
        plt.draw()


    def button_release(self, event):
        ''' handles a mouse button release '''
        self.button_down = False
        
        # if we didn't start with a brush, ignore
        if not self.brush: return
        
        # if we just clicked without drawing, remove the brush
        if (event.xdata, event.ydata) == self.start_xy:
            self.unbrush_me()
            plt.draw()
            return
        
        # if we stopped inside the same axis, update the brush one last time
        if event.inaxes == self.brush_axis:
            self.brush.set_width( abs(self.start_xy[0] - event.xdata) )
            self.brush.set_height( abs(self.start_xy[1] - event.ydata) )
            self.brush.set_x( min(self.start_xy[0], event.xdata) )
            self.brush.set_y( min(self.start_xy[1], event.ydata) )
        
        self.brush_me()
        plt.draw()
        
        
    def brush_me(self):
        ''' apply the new brush to data in all axes '''
        # figure out the indices of the data inside the brush
        i_brush_axis = self.axl.index(self.brush_axis)
        ax_data = self.data[i_brush_axis]
        bbox    = self.brush.get_bbox()
        boolean_brushed = []
        for i in range(len(ax_data[0])):
            if bbox.contains( ax_data[0,i], ax_data[1,i] ):
                boolean_brushed.append(True)
            else:
                boolean_brushed.append(False)
        boolean_brushed  = np.array(boolean_brushed)
        # boolean_brushed is an array of T,F values indicating whether the datapoint at
        #  that index should be 'brushed' (i.e. keep its color) or not (made gray)
        
        for i, ax in enumerate(self.axl):
            plotted_things = ax.collections[0]
            # now set the facecolor of all points not brushed to gray
            #  new_fc is a 2d array, the 0th axis corresponding to individual points,
            #  and the 1st axis corresponding to color values
            new_fc = plotted_things.get_facecolors()
            new_fc[~boolean_brushed] = self.unbrushed_color
            new_fc[boolean_brushed]  = self.brushed_color
    
    
    def unbrush_me(self):
        ''' paint everything the brushed color '''
        # build an always-true boolean array and brush to that
        boolean_brushed = []
        for i in range(len(self.data[0][0])):
            boolean_brushed.append(True)
        boolean_brushed = np.array(boolean_brushed)
        for i, ax in enumerate(self.axl):
            plotted_things = ax.collections[0]
            new_fc = plotted_things.get_facecolors()
            new_fc[boolean_brushed]  = self.brushed_color
开发者ID:matarhaller,项目名称:Seminar,代码行数:104,代码来源:brusher.py


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