本文整理汇总了Python中matplotlib.widgets.RectangleSelector类的典型用法代码示例。如果您正苦于以下问题:Python RectangleSelector类的具体用法?Python RectangleSelector怎么用?Python RectangleSelector使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了RectangleSelector类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
def __init__(self, ax, on_move=None, on_release=None, on_enter=None, maxdist=10, rect_props=None):
CanvasToolBase.__init__(self, ax, on_move=on_move, on_enter=on_enter, on_release=on_release)
props = dict(edgecolor=None, facecolor="r", alpha=0.15)
props.update(rect_props if rect_props is not None else {})
if props["edgecolor"] is None:
props["edgecolor"] = props["facecolor"]
RectangleSelector.__init__(self, ax, lambda *args: None, rectprops=props, useblit=self.useblit)
# Alias rectangle attribute, which is initialized in RectangleSelector.
self._rect = self.to_draw
self._rect.set_animated(True)
self.maxdist = maxdist
self.active_handle = None
self._extents_on_press = None
if on_enter is None:
def on_enter(extents):
print("(xmin=%.3g, xmax=%.3g, ymin=%.3g, ymax=%.3g)" % extents)
self.callback_on_enter = on_enter
props = dict(mec=props["edgecolor"])
self._corner_order = ["NW", "NE", "SE", "SW"]
xc, yc = self.corners
self._corner_handles = ToolHandles(ax, xc, yc, marker_props=props)
self._edge_order = ["W", "N", "E", "S"]
xe, ye = self.edge_centers
self._edge_handles = ToolHandles(ax, xe, ye, marker="s", marker_props=props)
self._artists = [self._rect, self._corner_handles.artist, self._edge_handles.artist]
示例2: SeriesWidget
class SeriesWidget(MplWidget):
def __init__(self, parent=None):
MplWidget.__init__(self, parent)
def draw(self):
(self.coord, series, title, xlabel, ylabel, showLegend) = self.inputPorts
colors = pylab.cm.jet(np.linspace(0,1, series.values.shape[0]))
pylab.clf()
pylab.title(title)
ll = pylab.plot(series.values.T, linewidth=1)
for pos, _ids in enumerate(series.ids):
ll[pos].set_color(colors[pos])
if _ids in self.selectedIds:
ll[pos].set_linewidth(3)
pylab.xlabel(xlabel)
pylab.ylabel(ylabel)
if showLegend:
pylab.legend(pylab.gca().get_lines(),
series.labels,
numpoints=1, prop=dict(size='small'), loc='upper right')
self.figManager.canvas.draw()
self.rectSelector = RectangleSelector(pylab.gca(), self.onselect, drawtype='box',
rectprops=dict(alpha=0.4, facecolor='yellow'))
self.rectSelector.set_active(True)
def updateSelection(self, selectedIds):
self.selectedIds = selectedIds
self.updateContents();
def onselect(self, eclick, erelease):
pass
示例3: PeakFinder
class PeakFinder(object):
def __init__(self, ax, canvas):
self.rectProps = dict(facecolor='red', edgecolor = 'white',
alpha=0.5, fill=True)
self.indicatorProps = dict(facecolor='white', edgecolor='black', alpha=0.5, fill=True)
self.__selector = RectangleSelector(ax, self.onSelect, drawtype='box', rectprops=self.rectProps)
self.__axes = ax
self.__canvas = canvas
def onSelect(self, epress, erelease):
start = map(int, (epress.xdata, epress.ydata))
stop = map(int, (erelease.xdata, erelease.ydata))
###################
ax = self.__axes
dataMatrix = ax.get_axes().get_images()[0].get_array()
clipMatrix = dataMatrix[start[1]:(stop[1]+1), start[0]:(stop[0]+1)]
peakPos = nonzero(clipMatrix == clipMatrix.max())
peakPos = (peakPos[1][0] + start[0], peakPos[0][0] + start[1])
print peakPos
circle = Circle(peakPos, 4, **self.indicatorProps)
ax.add_patch(circle)
self.__canvas.show()
###################
def activate(self):
self.__selector.set_active(True)
def deactivate(self):
self.__selector.set_active(False)
@property
def isActivate(self):
return self.__selector.active
示例4: __init__
def __init__(self, lines, drawtype='box',
minspanx=None, minspany=None, useblit=False,
lineprops=None, rectprops=None, spancoords='data',
button=None, maxdist=10, marker_props=None,
interactive=False, state_modifier_keys=None):
self.verbose = True
self.lines = flatten(lines)
ax = self.lines[0].axes
RectangleSelector.__init__( self, ax, self.select_lines, drawtype,
minspanx, minspany, useblit,
lineprops, rectprops, spancoords,
button, maxdist, marker_props,
interactive, state_modifier_keys)
hprops = dict(linewidth=10, alpha=0.5, linestyle='-') # marker='s'
self.selection = [ np.zeros(l.get_xdata().shape, bool)
for l in self.lines ]
#Create Line2D for highlighting selected sections
self.highlighted = []
for line in self.lines:
hline, = ax.plot([], [], color=line.get_color(), **hprops)
self.highlighted.append( hline )
self.artists.append( hline ) #enable blitting for the highlighted segments
示例5: __init__
def __init__(
self,
ax,
onselect,
button=None,
minspanx=None,
minspany=None,
useblit=True,
lineprops=None,
rectprops=dict(facecolor="red", edgecolor="black", alpha=0.5, fill=True),
proxy=5,
):
RectangleSelector.__init__(
self,
ax=ax,
onselect=onselect,
drawtype="box",
spancoords="data",
minspanx=minspanx,
minspany=minspany,
useblit=useblit,
lineprops=lineprops,
rectprops=rectprops,
button=button,
)
self.fixedSize = None
self.prevEvents = None
self.proxy = max(
self.ax.transData.transform_point((proxy / 100, proxy / 100)) - self.ax.transData.transform_point((0, 0))
)
示例6: onmove
def onmove(self, ev):
if self.eventpress is None or self.ignore(ev):
return
if self.fixedSize and self.prevEvents:
# Panning mode. Modify the existing ROI. Do the shift.
ev.xdata += self.wdata
ev.ydata += self.hdata
self.eventpress.xdata = ev.xdata - 2 * self.wdata
self.eventpress.ydata = ev.ydata - 2 * self.hdata
RectangleSelector.onmove(self, ev)
示例7: _press
def _press(self, event):
if event.button == 1:
pass
if event.button == 2:
self.restart()
self.canvas.draw() #TODO: blit
return
RectangleSelector._press(self, event)
示例8: on_mouse_press
def on_mouse_press(self, event):
if event.button != 1 or not self.ax.in_axes(event):
return
self._set_active_handle(event)
if self.active_handle is None:
# Clear previous rectangle before drawing new rectangle.
self.set_visible(False)
self.redraw()
self.set_visible(True)
RectangleSelector.press(self, event)
示例9: SelectFromCollection
class SelectFromCollection(object):
"""Select indices from a matplotlib collection using `LassoSelector`.
Selected indices are saved in the `ind` attribute. This tool highlights
selected points by fading them out (i.e., reducing their alpha values).
If your collection has alpha < 1, this tool will permanently alter them.
Note that this tool selects collection objects based on their *origins*
(i.e., `offsets`).
Parameters
----------
ax : :class:`~matplotlib.axes.Axes`
Axes to interact with.
collection : :class:`matplotlib.collections.Collection` subclass
Collection you want to select from.
alpha_other : 0 <= float <= 1
To highlight a selection, this tool sets all selected points to an
alpha value of 1 and non-selected points to `alpha_other`.
"""
def __init__(self, ax, collection, alpha_other=0.3):
self.canvas = ax.figure.canvas
self.collection = collection
self.alpha_other = alpha_other
self.xys = collection.get_offsets()
self.Npts = len(self.xys)
# Ensure that we have separate colors for each object
self.fc = collection.get_facecolors()
if len(self.fc) == 0:
raise ValueError('Collection must have a facecolor')
elif len(self.fc) == 1:
self.fc = np.tile(self.fc, self.Npts).reshape(self.Npts, -1)
self.lasso = RectangleSelector(ax, onselect=self.onselect) # Sprememba glede na originalno kodo
self.ind = []
def onselect(self, verts):
path = Path(verts)
self.ind = np.nonzero([path.contains_point(xy) for xy in self.xys])[0]
self.fc[:, -1] = self.alpha_other
self.fc[self.ind, -1] = 1
self.collection.set_facecolors(self.fc)
self.canvas.draw_idle()
def disconnect(self):
self.lasso.disconnect_events()
self.fc[:, -1] = 1
self.collection.set_facecolors(self.fc)
self.canvas.draw_idle()
示例10: on_mouse_release
def on_mouse_release(self, event):
if event.button != 1:
return
if not self.ax.in_axes(event):
self.eventpress = None
return
RectangleSelector.release(self, event)
self._extents_on_press = None
# Undo hiding of rectangle and redraw.
self.set_visible(True)
self.redraw()
self.callback_on_release(self.geometry)
示例11: press
def press(self, ev):
if self.ignore(ev):
return
h = self.close_to_handles(ev)
if not self.fixedSize and self.prevEvents and h:
# Not fixed size and active roi.
# Clicked on the corner -> modify mode
x, y = self.opposite_corner(h)
self.to_draw.set_visible(self.visible)
self.eventpress = ev
self.eventpress.xdata = x
self.eventpress.ydata = y
return False
else:
RectangleSelector.press(self, ev)
示例12: release
def release(self, ev):
if self.eventpress is None or self.ignore(ev):
return
if self.fixedSize and self.prevEvents:
# Panning mode. Modify the existing ROI. Do the shift.
ev.xdata += self.wdata
ev.ydata += self.hdata
self.eventpress.xdata = ev.xdata - 2 * self.wdata
self.eventpress.ydata = ev.ydata - 2 * self.hdata
self.prevEvents = (self.eventpress, ev)
pe, re = self.prevEvents
self.wdata = (pe.xdata - re.xdata) / 2
self.hdata = (pe.ydata - re.ydata) / 2
RectangleSelector.release(self, ev)
示例13: __init__
def __init__(self, ax, canvas):
self.rectProps = dict(facecolor='red', edgecolor = 'white',
alpha=0.5, fill=True)
self.indicatorProps = dict(facecolor='white', edgecolor='black', alpha=0.5, fill=True)
self.__selector = RectangleSelector(ax, self.onSelect, drawtype='box', rectprops=self.rectProps)
self.__axes = ax
self.__canvas = canvas
示例14: __init__
def __init__(self,artist):
self.artist = artist
self.selector = RectangleSelector(self.artist.axes,self.on_select,
button=3, minspanx=5, minspany=5, spancoords='pixels',
rectprops = dict(facecolor='red', edgecolor = 'red',
alpha=0.3, fill=True))
self.coords = []
示例15: __init__
def __init__(self, viewer, on_move=None, on_release=None, on_enter=None,
maxdist=10, rect_props=None):
self._rect = None
props = dict(edgecolor=None, facecolor='r', alpha=0.15)
props.update(rect_props if rect_props is not None else {})
if props['edgecolor'] is None:
props['edgecolor'] = props['facecolor']
RectangleSelector.__init__(self, viewer.ax, lambda *args: None,
rectprops=props)
CanvasToolBase.__init__(self, viewer, on_move=on_move,
on_enter=on_enter, on_release=on_release)
# Events are handled by the viewer
try:
self.disconnect_events()
except AttributeError:
# disconnect the events manually (hack for older mpl versions)
[self.canvas.mpl_disconnect(i) for i in range(10)]
# Alias rectangle attribute, which is initialized in RectangleSelector.
self._rect = self.to_draw
self._rect.set_animated(True)
self.maxdist = maxdist
self.active_handle = None
self._extents_on_press = None
if on_enter is None:
def on_enter(extents):
print("(xmin=%.3g, xmax=%.3g, ymin=%.3g, ymax=%.3g)" % extents)
self.callback_on_enter = on_enter
props = dict(mec=props['edgecolor'])
self._corner_order = ['NW', 'NE', 'SE', 'SW']
xc, yc = self.corners
self._corner_handles = ToolHandles(self.ax, xc, yc, marker_props=props)
self._edge_order = ['W', 'N', 'E', 'S']
xe, ye = self.edge_centers
self._edge_handles = ToolHandles(self.ax, xe, ye, marker='s',
marker_props=props)
self.artists = [self._rect,
self._corner_handles.artist,
self._edge_handles.artist]
viewer.add_tool(self)