本文整理汇总了Python中matplotlib.patches.Rectangle.set_height方法的典型用法代码示例。如果您正苦于以下问题:Python Rectangle.set_height方法的具体用法?Python Rectangle.set_height怎么用?Python Rectangle.set_height使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类matplotlib.patches.Rectangle
的用法示例。
在下文中一共展示了Rectangle.set_height方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from matplotlib.patches import Rectangle [as 别名]
# 或者: from matplotlib.patches.Rectangle import set_height [as 别名]
class click_yrange:
'''An interactive yrange selector. Given an axis and a starting
y0 location, draw a full-width rectange that follows the mouise.
Similar to click_window, but more appropriate for selecting out
a y-range.'''
def __init__(self, ax, y0):
self.ax = ax
self.y0 = y0
x0,x1 = ax.get_xbound()
self.rect = Rectangle((x0,y0), width=(x1-x0), 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_height(event.ydata - self.y0)
self.ax.figure.canvas.draw()
def close(self):
self.rect.figure.canvas.mpl_disconnect(self.cidmotion)
self.rect.remove()
self.ax.figure.canvas.draw()
return(self.y0, self.rect.get_y()+self.rect.get_height())
示例2: Annotate
# 需要导入模块: from matplotlib.patches import Rectangle [as 别名]
# 或者: from matplotlib.patches.Rectangle import set_height [as 别名]
class Annotate(object):
def __init__(self, ax):
self.ax = ax
self.rect = Rectangle((0,0), 1, 1, fill=False)
self.x0 = None
self.y0 = None
self.x1 = None
self.y1 = None
self.ax.add_patch(self.rect)
self.ax.figure.canvas.mpl_connect('button_press_event', self.on_press)
self.ax.figure.canvas.mpl_connect('button_release_event', self.on_release)
def on_press(self, event):
print 'press'
self.x0 = event.xdata
self.y0 = event.ydata
def on_release(self, event):
print 'release'
self.x1 = event.xdata
self.y1 = event.ydata
self.rect.set_width(self.x1 - self.x0)
self.rect.set_height(self.y1 - self.y0)
self.rect.set_xy((self.x0, self.y0))
self.ax.figure.canvas.draw()
self.get_graph_bound()
#self.bounds = ColorBounds(Cb_min=self.x0, Cr_min=self.y0, Cb_max=self.x1, Cr_max=self.y1)
self.bounds = ColorBounds(Cr_min=self.x0, Yp_min=self.y0, Cr_max=self.x1, Yp_max=self.y1)
def get_graph_bound(self):
print(self.x0, self.y0, self.x1, self.y1)
示例3: Selector
# 需要导入模块: from matplotlib.patches import Rectangle [as 别名]
# 或者: from matplotlib.patches.Rectangle import set_height [as 别名]
class Selector(object):
def __init__(self, ax):
self.ax = ax
self.x0 = None
self.y0 = None
self.rect = Rectangle((0,0), 1, 1, fill=False)
self.ax.add_patch(self.rect)
self.ax.figure.canvas.mpl_connect('button_press_event', self.on_press)
self.ax.figure.canvas.mpl_connect('button_release_event', self.on_release)
def on_press(self, event):
self.x0 = event.xdata
self.y0 = event.ydata
def on_release(self, event):
x1 = event.xdata
y1 = event.ydata
self.rect.set_width(x1 - self.x0)
self.rect.set_height(y1 - self.y0)
self.rect.set_xy((self.x0, self.y0))
self.ax.figure.canvas.draw()
x_min = min(self.x0, x1)
x_max = max(self.x0, x1)
y_min = min(self.y0, y1)
y_max = max(self.y0, y1)
self.xybounds = (x_min, x_max, y_min, y_max)
示例4: LaneSelector
# 需要导入模块: from matplotlib.patches import Rectangle [as 别名]
# 或者: from matplotlib.patches.Rectangle import set_height [as 别名]
class LaneSelector(object):
def __init__(self, cb):
self.ax = gca()
self.rect = Rectangle((0,0), 1, 1, alpha=0.2, color='red')
self.x0 = None
self.y0 = None
self.x1 = None
self.y1 = None
self.pressed = False
self.cb = cb
self.ax.add_patch(self.rect)
self.ax.figure.canvas.mpl_connect('button_press_event', self.on_press)
self.ax.figure.canvas.mpl_connect('button_release_event', self.on_release)
self.ax.figure.canvas.mpl_connect('motion_notify_event', self.on_motion)
def on_press(self, event):
self.x0 = event.xdata
self.y0 = event.ydata
self.pressed = True
def on_release(self, event):
self.pressed = False
if self.x0 != self.x1 and self.y0 != self.y1:
self.cb(self.x0, self.y0, self.x1, self.y1)
def on_motion(self, event):
if self.pressed:
self.x1 = event.xdata
self.y1 = event.ydata
self.rect.set_width(self.x1 - self.x0)
self.rect.set_height(self.y1 - self.y0)
self.rect.set_xy((self.x0, self.y0))
self.ax.figure.canvas.draw()
示例5: close
# 需要导入模块: from matplotlib.patches import Rectangle [as 别名]
# 或者: from matplotlib.patches.Rectangle import set_height [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)))
示例6: resizeGraphics
# 需要导入模块: from matplotlib.patches import Rectangle [as 别名]
# 或者: from matplotlib.patches.Rectangle import set_height [as 别名]
def resizeGraphics(self, width, height):
""" It is time to set a magnitude to our currently normalized
lines, and send them to the figure. Here we assume that
__normLines & __normPatches are already fully populated. """
self.__fig.lines = [] # clear all old lines from figure
self.__fig.patches = [] # clear all old patches from figure
self.__xsz = width
self.__ysz = height
# scale each text item
for t in self.__fig.texts:
t.set_size(self.getTextPointSize(t.gkiTextSzFactor, width, height))
# scale each line, then apply it to the figure
for nrln in self.__normLines:
ll = Line2D([], [])
ll.update_from(nrln)
ll.set_data(nrln.get_xdata(True)*self.__xsz,
nrln.get_ydata(True)*self.__ysz)
self.__fig.lines.append(ll)
# scale each patch, then apply it to the figure
for nrpa in self.__normPatches:
rr = Rectangle((0,0),0,0)
rr.update_from(nrpa)
rr.set_x(nrpa.get_x()*self.__xsz)
rr.set_y(nrpa.get_y()*self.__ysz)
rr.set_width(nrpa.get_width()*self.__xsz)
rr.set_height(nrpa.get_height()*self.__ysz)
self.__fig.patches.append(rr)
示例7: Annotate
# 需要导入模块: from matplotlib.patches import Rectangle [as 别名]
# 或者: from matplotlib.patches.Rectangle import set_height [as 别名]
class Annotate(object):
def __init__(self):
self.ax = plt.gca()
self.rect = Rectangle((0,0), 1, 1)
self.x0 = None
self.y0 = None
self.x1 = None
self.y1 = None
self.ax.add_patch(self.rect)
self.ax.figure.canvas.mpl_connect('button_press_event', self.on_press)
self.ax.figure.canvas.mpl_connect('button_release_event', self.on_release)
def on_press(self, event):
print 'press'
self.x0 = event.xdata
self.y0 = event.ydata
def on_release(self, event):
print 'release'
self.x1 = event.xdata
self.y1 = event.ydata
self.rect.set_width(self.x1 - self.x0)
self.rect.set_height(self.y1 - self.y0)
self.rect.set_xy((self.x0, self.y0))
self.ax.figure.canvas.draw()
示例8: MplYRangeROI
# 需要导入模块: from matplotlib.patches import Rectangle [as 别名]
# 或者: from matplotlib.patches.Rectangle import set_height [as 别名]
class MplYRangeROI(AbstractMplRoi):
def __init__(self, ax):
"""
:param ax: A matplotlib Axes object to attach the graphical ROI to
"""
AbstractMplRoi.__init__(self, ax)
self._xi = None
self.plot_opts = {'edgecolor': PATCH_COLOR, 'facecolor': PATCH_COLOR,
'alpha': 0.3}
trans = blended_transform_factory(self._ax.transAxes,
self._ax.transData)
self._patch = Rectangle((0., 0.), 1., 1., transform=trans)
self._patch.set_zorder(100)
self._setup_patch()
def _setup_patch(self):
self._ax.add_patch(self._patch)
self._patch.set_visible(False)
self._sync_patch()
def _roi_factory(self):
return YRangeROI()
def start_selection(self, event):
if event.inaxes != self._ax:
return
self._roi.reset()
self._roi.set_range(event.ydata, event.ydata)
self._xi = event.ydata
self._mid_selection = True
self._sync_patch()
def update_selection(self, event):
if not self._mid_selection or event.inaxes != self._ax:
return
self._roi.set_range(min(event.ydata, self._xi),
max(event.ydata, self._xi))
self._sync_patch()
def finalize_selection(self, event):
self._mid_selection = False
self._patch.set_visible(False)
self._draw()
def _sync_patch(self):
if self._roi.defined():
rng = self._roi.range()
self._patch.set_xy((0, rng[0]))
self._patch.set_height(rng[1] - rng[0])
self._patch.set_width(1)
self._patch.set(**self.plot_opts)
self._patch.set_visible(True)
else:
self._patch.set_visible(False)
self._draw()
示例9: Annotate
# 需要导入模块: from matplotlib.patches import Rectangle [as 别名]
# 或者: from matplotlib.patches.Rectangle import set_height [as 别名]
class Annotate(object):
def __init__(self):
self.ax = plt.gca()
#self.ax = ax
#self.fig = fig
self.rect = Rectangle((0,0), 1, 1, facecolor='None', edgecolor='green')
self.x0 = None
self.y0 = None
self.x1 = None
self.y1 = None
self.is_pressed = False
self.ax.add_patch(self.rect)
#self.fig.canvas.mpl_connect('button_press_event',self.on_press)
#self.fig.canvas.mpl_connect('button_release_event', self.on_release)
#self.fig.canvas.mpl_connect('motion_notify_event', self.on_motion)
self.ax.figure.canvas.mpl_connect('button_press_event', self.on_press)
self.ax.figure.canvas.mpl_connect('button_release_event', self.on_release)
self.ax.figure.canvas.mpl_connect('motion_notify_event', self.on_motion)
self.ax.figure.canvas.draw()
def on_press(self, event):
print 'press'
self.is_pressed = True
self.x0 = event.xdata
self.y0 = event.ydata
print self.x0, self.y0
#self.rect.set_width(self.x1 - self.x0)
#self.rect.set_height(self.y1 - self.y0)
#self.rect.set_xy((self.x0, self.y0))
#self.rect.set_linestyle('dashed')
#self.fig.canvas.draw()
#self.ax.figure.canvas.draw()
def on_motion(self,event):
print 'motion'
if self.is_pressed is True:
return
self.x1 = event.xdata
self.y1 = event.ydata
self.rect.set_width(self.x1 - self.x0)
self.rect.set_height(self.y1 - self.y0)
self.rect.set_xy((self.x0, self.y0))
self.rect.set_linestyle('dashed')
#self.fig.canvas.draw()
self.ax.figure.canvas.draw()
def on_release(self, event):
print 'release'
self.is_pressed = False
self.x1 = event.xdata
self.y1 = event.ydata
self.rect.set_width(self.x1 - self.x0)
self.rect.set_height(self.y1 - self.y0)
self.rect.set_xy((self.x0, self.y0))
self.rect.set_linestyle('solid')
#self.fig.canvas.draw()
self.ax.figure.canvas.draw()
#time.sleep(3)
#plt.close()
print self.x0,self.y0,self.x1,self.y1
示例10: RoiRect
# 需要导入模块: from matplotlib.patches import Rectangle [as 别名]
# 或者: from matplotlib.patches.Rectangle import set_height [as 别名]
class RoiRect(object):
''' Class for getting a mouse drawn rectangle
Based on the example from:
http://matplotlib.org/users/event_handling.html#draggable-rectangle-exercise
Note that:
* It makes only one roi
'''
def __init__(self):
self.ax = plt.gca()
self.rect = Rectangle((0,0), 1, 1,fc='none', ec='r')
self.x0 = None
self.y0 = None
self.x1 = None
self.y1 = None
self.ax.add_patch(self.rect)
self.ax.figure.canvas.mpl_connect('button_press_event', self.on_press)
self.ax.figure.canvas.mpl_connect('button_release_event', self.on_release)
self.ax.figure.canvas.mpl_connect('motion_notify_event', self.on_motion)
def on_press(self, event):
print 'press'
self.x0 = event.xdata
self.y0 = event.ydata
self.rect.set_linestyle('dashed')
self.set = False
def on_release(self, event):
print 'release'
self.x1 = event.xdata
self.y1 = event.ydata
self.rect.set_width(self.x1 - self.x0)
self.rect.set_height(self.y1 - self.y0)
self.rect.set_xy((self.x0, self.y0))
self.rect.set_linestyle('solid')
self.ax.figure.canvas.draw()
self.set = True
self.ax.figure.canvas.mpl_disconnect(self.on_press)
self.ax.figure.canvas.mpl_disconnect(self.on_release)
self.ax.figure.canvas.mpl_disconnect(self.on_motion)
def on_motion(self, event):
# on motion will move the rect if the mouse
if self.x0 is None: return
if self.set: return
# if event.inaxes != self.rect.axes: return
self.x1 = event.xdata
self.y1 = event.ydata
self.rect.set_width(self.x1 - self.x0)
self.rect.set_height(self.y1 - self.y0)
self.rect.set_xy((self.x0, self.y0))
self.ax.figure.canvas.draw()
示例11: RectangleRegionSelector
# 需要导入模块: from matplotlib.patches import Rectangle [as 别名]
# 或者: from matplotlib.patches.Rectangle import set_height [as 别名]
class RectangleRegionSelector(object):
isPressed = False
def __init__(self):
self.ax = plt.gca()
self.rect = Rectangle((0, 0), 1, 1, color='b', fill=False)
self.x0 = None
self.y0 = None
self.x1 = None
self.y1 = None
self.ax.add_patch(self.rect)
self.ax.figure.canvas.mpl_connect('button_press_event', self.on_press)
self.ax.figure.canvas.mpl_connect('button_release_event',
self.on_release)
self.ax.figure.canvas.mpl_connect('motion_notify_event',
self.on_motion)
def on_press(self, event):
self.x0 = event.xdata
self.y0 = event.ydata
self.isPressed = True
def on_motion(self, event):
if self.isPressed:
self.x1 = event.xdata
self.y1 = event.ydata
self.draw_rect()
def on_release(self, event):
self.isPressed = False
if event.xdata is not None and event.ydata is not None:
self.x1 = event.xdata
self.y1 = event.ydata
self.draw_rect()
else:
print "Mouse must be released within the axes, try again."
def draw_rect(self):
self.rect.set_width(self.x1 - self.x0)
self.rect.set_height(self.y1 - self.y0)
self.rect.set_xy((self.x0, self.y0))
self.ax.figure.canvas.draw()
def asXYWH(self):
return (min(self.x0, self.x1), min(self.y0, self.y1),
abs(self.x1-self.x0), abs(self.y1-self.y0))
示例12: rectSelection
# 需要导入模块: from matplotlib.patches import Rectangle [as 别名]
# 或者: from matplotlib.patches.Rectangle import set_height [as 别名]
class rectSelection(GuiSelection):
"""Interactive selection of a rectangular region on the axis.
Used by hist2d_alex().
"""
def on_press_draw(self):
if 'r' in self.__dict__:
self.r.set_height(0)
self.r.set_width(0)
self.r.set_xy((self.xs, self.ys))
self.e.height = 0
self.e.width = 0
self.e.center = (self.xs, self.ys)
else:
self.r = Rectangle(xy=(self.xs, self.ys), height=0, width=0,
fill=False, lw=2, alpha=0.5, color='blue')
self.e = Ellipse(xy=(self.xs, self.ys), height=0, width=0,
fill=False, lw=2, alpha=0.6, color='blue')
self.ax.add_artist(self.r)
self.ax.add_artist(self.e)
self.r.set_clip_box(self.ax.bbox)
self.r.set_zorder(10)
self.e.set_clip_box(self.ax.bbox)
self.e.set_zorder(10)
def on_motion_draw(self):
self.r.set_height(self.ye - self.ys)
self.r.set_width(self.xe - self.xs)
self.e.height = (self.ye - self.ys)
self.e.width = (self.xe - self.xs)
self.e.center = (np.mean([self.xs, self.xe]),
np.mean([self.ys, self.ye]))
self.fig.canvas.draw()
def on_release_print(self):
# This is the only custom method for hist2d_alex()
E1, E2 = min((self.xs, self.xe)), max((self.xs, self.xe))
S1, S2 = min((self.ys, self.ye)), max((self.ys, self.ye))
self.selection = dict(E1=E1, E2=E2, S1=S1, S2=S2)
pprint("Selection: \nE1=%.2f, E2=%.2f, S1=%.2f, S2=%.2f\n" %\
(E1,E2,S1,S2))
示例13: Annotate
# 需要导入模块: from matplotlib.patches import Rectangle [as 别名]
# 或者: from matplotlib.patches.Rectangle import set_height [as 别名]
class Annotate(object):
def __init__(self,ax=None):
if ax==None:
self.ax = plt.gca()
else:
self.ax = ax
self.rect = Rectangle((0,0), 0, 0, facecolor='yellow', edgecolor='yellow', alpha=.5, \
zorder=10, clip_on=False)
self.x0 = None
self.y0 = None
self.x1 = None
self.y1 = None
self.is_pressed = False
self.ax.add_patch(self.rect)
self.ax.figure.canvas.mpl_connect('button_press_event', self.on_press)
self.ax.figure.canvas.mpl_connect('button_release_event', self.on_release)
self.ax.figure.canvas.mpl_connect('motion_notify_event', self.on_motion)
def on_press(self, event):
self.is_pressed = True
print 'press'
self.x0 = event.xdata
self.y0 = event.ydata
self.x1 = event.xdata
self.y1 = event.ydata
if not np.any( [c==None for c in [self.x0,self.y0,self.x1,self.y1]] ):
self.rect.set_width(self.x1 - self.x0)
self.rect.set_height(self.y1 - self.y0)
self.rect.set_xy((self.x0, self.y0))
self.rect.set_linestyle('dashed')
self.ax.figure.canvas.draw()
def on_motion(self,event):
if self.is_pressed is True:
self.x1 = event.xdata
self.y1 = event.ydata
if not np.any( [c==None for c in [self.x0,self.y0,self.x1,self.y1]] ):
self.rect.set_width(self.x1 - self.x0)
self.rect.set_height(self.y1 - self.y0)
self.rect.set_xy((self.x0, self.y0))
self.rect.set_linestyle('dashed')
self.ax.figure.canvas.draw()
def on_release(self, event):
self.is_pressed = False
print 'release'
self.x1 = event.xdata
self.y1 = event.ydata
if not np.any( [c==None for c in [self.x0,self.y0,self.x1,self.y1]] ):
self.rect.set_width(self.x1 - self.x0)
self.rect.set_height(self.y1 - self.y0)
self.rect.set_xy((self.x0, self.y0))
self.rect.set_linestyle('solid')
self.ax.figure.canvas.draw()
print self.x0,self.y0,self.x1,self.y1
if self.x0==self.x1 and self.y0==self.y1:
print type(self.__bases__)
示例14: MplRectangularROI
# 需要导入模块: from matplotlib.patches import Rectangle [as 别名]
# 或者: from matplotlib.patches.Rectangle import set_height [as 别名]
class MplRectangularROI(AbstractMplRoi):
"""
A subclass of RectangularROI that also renders the ROI to a plot
*Attributes*:
plot_opts:
Dictionary instance
A dictionary of plot keywords that are passed to
the patch representing the ROI. These control
the visual properties of the ROI
"""
def __init__(self, axes):
"""
:param axes: A matplotlib Axes object to attach the graphical ROI to
"""
AbstractMplRoi.__init__(self, axes)
self._xi = None
self._yi = None
self.plot_opts = {'edgecolor': PATCH_COLOR, 'facecolor': PATCH_COLOR,
'alpha': 0.3}
self._patch = Rectangle((0., 0.), 1., 1.)
self._patch.set_zorder(100)
self._setup_patch()
def _setup_patch(self):
self._axes.add_patch(self._patch)
self._patch.set_visible(False)
self._sync_patch()
def _roi_factory(self):
return RectangularROI()
def start_selection(self, event):
if event.inaxes != self._axes:
return False
if event.key == SCRUBBING_KEY:
if not self._roi.defined():
return False
elif not self._roi.contains(event.xdata, event.ydata):
return False
self._roi_store()
self._xi = event.xdata
self._yi = event.ydata
if event.key == SCRUBBING_KEY:
self._scrubbing = True
self._cx, self._cy = self._roi.center()
else:
self.reset()
self._roi.update_limits(event.xdata, event.ydata,
event.xdata, event.ydata)
self._mid_selection = True
self._sync_patch()
def update_selection(self, event):
if not self._mid_selection or event.inaxes != self._axes:
return False
if event.key == SCRUBBING_KEY:
if not self._roi.defined():
return False
if self._scrubbing:
self._roi.move_to(self._cx + event.xdata - self._xi,
self._cy + event.ydata - self._yi)
else:
self._roi.update_limits(min(event.xdata, self._xi),
min(event.ydata, self._yi),
max(event.xdata, self._xi),
max(event.ydata, self._yi))
self._sync_patch()
def finalize_selection(self, event):
self._scrubbing = False
self._mid_selection = False
self._patch.set_visible(False)
self._draw()
def _sync_patch(self):
if self._roi.defined():
corner = self._roi.corner()
width = self._roi.width()
height = self._roi.height()
self._patch.set_xy(corner)
self._patch.set_width(width)
self._patch.set_height(height)
#.........这里部分代码省略.........
示例15: Annotate
# 需要导入模块: from matplotlib.patches import Rectangle [as 别名]
# 或者: from matplotlib.patches.Rectangle import set_height [as 别名]
class Annotate(object):
def __init__(self, cloud, selection_shape, z_color=False, unlabeled_color = (1,0,0), labeled_color = (0,0,1)):
self.selection_shape = selection_shape
self.unlabeled_color = np.array(unlabeled_color)
self.labeled_color = np.array(labeled_color)
assert not np.all(unlabeled_color == labeled_color)
self.fig = plt.figure('label rope ends', figsize=(24,12))
self.fig.clear()
self.ax = self.fig.add_subplot(111, aspect='equal')
if cloud.shape[1] == 6:
self.cloud = cloud
else:
self.cloud = np.c_[cloud[:,:3], np.tile(self.unlabeled_color, (cloud.shape[0],1))]
if z_color:
z_min = self.cloud[:,2].min()
z_max = self.cloud[:,2].max()
self.z_color = np.empty((len(self.cloud),3))
for i in range(len(self.cloud)):
f = (self.cloud[i,2] - z_min) / (z_max - z_min)
self.z_color[i,:] = np.array(colorsys.hsv_to_rgb(f,1,1))
else:
self.z_color = None
self.scatters = []
self.rescatter_cloud()
self.ax.autoscale(False)
if self.selection_shape == 'rectangle' or self.selection_shape == 'square':
self.rect = Rectangle((0,0), 0, 0, facecolor='None', edgecolor='green', linestyle='dashed')
self.ax.add_patch(self.rect)
elif self.selection_shape == 'circle':
self.circle = Circle((0,0), 0, facecolor='None', edgecolor='green', linestyle='dashed')
self.ax.add_patch(self.circle)
self.x0 = None
self.y0 = None
self.x1 = None
self.y1 = None
self.fig.canvas.mpl_connect('button_press_event', self.on_press)
self.fig.canvas.mpl_connect('button_release_event', self.on_release)
self.motion_notify_cid = None
self.fig.canvas.mpl_connect('key_press_event', self.on_key)
plt.show()
def rescatter_cloud(self):
for scatter in self.scatters:
scatter.remove()
self.scatters = []
if self.z_color is not None:
ul_inds = np.absolute((self.cloud[:,3:] - self.unlabeled_color)).sum(axis=1) == 0 # unlabeled inds
self.scatters.append(plt.scatter(self.cloud[ul_inds,0], self.cloud[ul_inds,1], c=self.z_color[ul_inds,:], edgecolors=self.z_color[ul_inds,:], marker=',', s=5))
self.scatters.append(plt.scatter(self.cloud[~ul_inds,0], self.cloud[~ul_inds,1], c=self.cloud[~ul_inds,3:], edgecolors=self.cloud[~ul_inds,3:], marker='o', s=20))
else:
self.scatters.append(plt.scatter(self.cloud[:,0], self.cloud[:,1], c=self.cloud[:,3:], edgecolors=self.cloud[:,3:], marker=',', s=5))
def on_press(self, event):
if event.xdata < self.ax.get_xlim()[0] or event.xdata > self.ax.get_xlim()[1] or \
event.ydata < self.ax.get_ylim()[0] or event.ydata > self.ax.get_ylim()[1]:
return
self.x0 = event.xdata
self.y0 = event.ydata
self.motion_notify_cid = self.fig.canvas.mpl_connect('motion_notify_event', self.on_motion)
def on_release(self, event):
if self.motion_notify_cid:
self.fig.canvas.mpl_disconnect(self.motion_notify_cid)
self.motion_notify_cid = None
if self.selection_shape == 'rectangle' or self.selection_shape == 'square':
x0 = min(self.x0, self.x1)
x1 = max(self.x0, self.x1)
y0 = min(self.y0, self.y1)
y1 = max(self.y0, self.y1)
inside_rect_inds = (x0 <= self.cloud[:,0]) * (self.cloud[:,0] <= x1) * (y0 <= self.cloud[:,1]) * (self.cloud[:,1] <= y1)
num_pts_inside_rect = inside_rect_inds.sum()
self.cloud[inside_rect_inds,3:] = np.tile(self.labeled_color, (num_pts_inside_rect,1))
elif self.selection_shape == 'circle':
inside_circle_inds = np.apply_along_axis(np.linalg.norm, 1, self.cloud[:,:2] - np.array(self.circle.center)) <= self.circle.get_radius()
num_pts_inside_circle = inside_circle_inds.sum()
self.cloud[inside_circle_inds,3:] = np.tile(self.labeled_color, (num_pts_inside_circle,1))
self.rescatter_cloud()
plt.draw()
def on_motion(self, event):
if event.xdata < self.ax.get_xlim()[0] or event.xdata > self.ax.get_xlim()[1] or \
event.ydata < self.ax.get_ylim()[0] or event.ydata > self.ax.get_ylim()[1]:
return
if self.selection_shape == 'rectangle':
self.x1 = event.xdata
self.y1 = event.ydata
elif self.selection_shape == 'circle' or self.selection_shape == 'square':
side = max(abs(event.xdata - self.x0), abs(event.ydata - self.y0))
self.x1 = self.x0 + np.sign(event.xdata - self.x0) * side
self.y1 = self.y0 + np.sign(event.ydata - self.y0) * side
if self.selection_shape == 'rectangle' or self.selection_shape == 'square':
self.rect.set_width(self.x1 - self.x0)
self.rect.set_height(self.y1 - self.y0)
self.rect.set_xy((self.x0, self.y0))
#.........这里部分代码省略.........