本文整理汇总了Python中matplotlib.patches.Rectangle.remove方法的典型用法代码示例。如果您正苦于以下问题:Python Rectangle.remove方法的具体用法?Python Rectangle.remove怎么用?Python Rectangle.remove使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类matplotlib.patches.Rectangle
的用法示例。
在下文中一共展示了Rectangle.remove方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from matplotlib.patches import Rectangle [as 别名]
# 或者: from matplotlib.patches.Rectangle import remove [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: close
# 需要导入模块: from matplotlib.patches import Rectangle [as 别名]
# 或者: from matplotlib.patches.Rectangle import remove [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)))
示例3: _on_figure_motion
# 需要导入模块: from matplotlib.patches import Rectangle [as 别名]
# 或者: from matplotlib.patches.Rectangle import remove [as 别名]
def _on_figure_motion(self, event):
if event.inaxes == None:
return
self._motion_wait -= 1
if self._motion_wait > 0:
return
x0, y0, x1, y1 = event.inaxes.dataLim.bounds
number_of_points = len(event.inaxes.lines[-1].get_ydata())
index = int(round((number_of_points-1) * (event.xdata-x0)/x1))
if len(self._data[0]) < index + 1:
return
if self._background is None:
self._background = self._figure.canvas.copy_from_bbox(self._graphs[0].bbox)
# restore the clean slate background
self._figure.canvas.restore_region(self._background)
polygon = None
if self._select_start == None:
linev = self._graphs[-1].axvline(x=event.xdata, linewidth=1, color="#000000", alpha=0.5)
lineh = self._graphs[7].axhline(y=event.ydata, linewidth=1, color="#000000", alpha=0.5)
self._graphs[-1].draw_artist(linev)
self._graphs[-1].draw_artist(lineh)
else:
width = abs(event.xdata - self._select_start)
start = self._select_start
if (event.xdata < start):
start = event.xdata
if width < 20:
col = "#aa4444"
else:
col = "#888888"
polygon = Rectangle((start, 0), width, y1 + 10000, facecolor=col, alpha=0.5)
self._graphs[-1].add_patch(polygon)
self._graphs[-1].draw_artist(polygon)
self._figure.canvas.blit(self._graphs[-1].bbox)
if self._select_start == None:
linev.remove()
lineh.remove()
if polygon != None:
polygon.remove()
for i in xrange(0, 8):
if (i < 2):
val = str(self._data[i][index])
else:
val = str(int(self._data[i][index]))
self._mouse_texts[i].setText(val)
示例4: plot_window
# 需要导入模块: from matplotlib.patches import Rectangle [as 别名]
# 或者: from matplotlib.patches.Rectangle import remove [as 别名]
def plot_window(self, component, starttime, endtime, window_weight):
if component == "Z":
axis = self.plot_axis_z
elif component == "N":
axis = self.plot_axis_n
elif component == "E":
axis = self.plot_axis_e
else:
raise NotImplementedError
trace = self.data["synthetics"][0]
ymin, ymax = axis.get_ylim()
xmin = starttime - trace.stats.starttime
width = endtime - starttime
height = ymax - ymin
rect = Rectangle((xmin, ymin), width, height, facecolor="0.6",
alpha=0.5, edgecolor="0.5", picker=True)
axis.add_patch(rect)
attached_text = axis.text(
x=xmin + 0.02 * width, y=ymax - 0.02 * height,
s=str(window_weight), verticalalignment="top",
horizontalalignment="left", color="0.4", weight=1000)
# Monkey patch to trigger text removal as soon as the rectangle is
# removed.
def remove():
super(Rectangle, rect).remove()
attached_text.remove()
rect.remove = remove
示例5: Picker_plot
# 需要导入模块: from matplotlib.patches import Rectangle [as 别名]
# 或者: from matplotlib.patches.Rectangle import remove [as 别名]
class Picker_plot (object):
"""
plot data so that a user can select points
Parameters
-----------
ax : matplotlib ax instance
data : Nx2 data array
"""
def __init__ (self, ax, data, inliers=None):
self.ax = ax
self.data = data
self.ax.figure.canvas.mpl_connect ('pick_event', self.on_pick)
self.ax.figure.canvas.mpl_connect ('button_press_event', self.on_click)
self.ax.figure.canvas.mpl_connect ('motion_notify_event', self.on_motion_notify)
self.ax.figure.canvas.mpl_connect ('button_release_event', self.on_release)
data_range = range (data.shape[0])
if not inliers is None:
self.inliers = list (inliers)
self.outliers = list (np.setdiff1d (data_range, inliers))
else:
self.inliers = data_range
self.outliers = []
self.line = None
self.oline = None
self.rect = None
self.x0 = None
self.y0 = None
self.draw ()
def draw (self):
if not self.line:
self.line, = self.ax.plot (self.data[:,0], self.data[:,1], 'bo', picker=3)
if self.oline:
self.oline.remove ()
self.oline = None
self.oline, = self.ax.plot (self.data[self.outliers,0],
self.data[self.outliers,1], 'ro')
def update_data (self, index):
for ind in index:
if ind in self.outliers:
print '{Picker_plot} user removed index:', ind
self.inliers.append (ind)
self.outliers.remove (ind)
else:
print '{Picker_plot} user added index:', ind
self.outliers.append (ind)
self.inliers.remove (ind)
self.draw ()
self.ax.figure.canvas.draw ()
def on_pick (self, event):
if event.artist != self.line: return
self.update_data (event.ind)
def on_click (self, event):
toolbar = plt.get_current_fig_manager ().toolbar
if toolbar.mode != '': return
self.x0, self.y0 = event.xdata, event.ydata
def on_motion_notify (self, event):
if not self.x0 or not event.inaxes: return
self.x1, self.y1 = event.xdata, event.ydata
if self.rect:
self.rect.remove ()
self.rect = None
self.rect = Rectangle ([self.x0,self.y0], event.xdata-self.x0,
event.ydata-self.y0, color='k', fc='none',lw=1)
self.ax.add_artist (self.rect)
self.ax.figure.canvas.draw ()
def on_release (self, event):
if not self.x0: return
if event.inaxes:
self.x1, self.y1 = event.xdata, event.ydata
x0 = min (self.x0, self.x1)
y0 = min (self.y0, self.y1)
x1 = max (self.x0, self.x1)
y1 = max (self.y0, self.y1)
self.x0,self.y0 = None, None
self.x1,self.y1 = None, None
if self.rect:
self.rect.remove ()
self.rect = None
ind = np.where ((self.data[:,0]>x0) & (self.data[:,0]<x1) & (self.data[:,1]>y0) & (self.data[:,1]<y1))[0]
self.update_data (ind)
示例6: ImagePanel
# 需要导入模块: from matplotlib.patches import Rectangle [as 别名]
# 或者: from matplotlib.patches.Rectangle import remove [as 别名]
#.........这里部分代码省略.........
def drawRectangle_onRelease(self, event):
self.xp1 = event.x
self.yp1 = event.y
self.x1 = event.xdata
self.y1 = event.ydata
self.rectangle.set_width(self.x1-self.x0)
self.rectangle.set_height(self.y1-self.y0)
self.rectangle.set_xy((self.x0, self.y0))
self.rectangle.set_linestyle('solid')
self.canvas.draw()
self.ispressed = False
self.canvas.mpl_disconnect(self.bpe)
self.canvas.mpl_disconnect(self.bre)
self.canvas.mpl_disconnect(self.mne)
print(self.xp0, self.yp0, self.xp1, self.yp1)
self.inform('<DrawRectangle>')
return (self.xp0, self.yp0, self.xp1, self.yp1)
def getRectanglePoints(self):
return (self.xp0, self.yp0, self.xp1, self.yp1)
def drawRectangle_onMotion(self, event):
if self.ispressed is True:
self.x1 = event.xdata
self.y1 = event.ydata
self.rectangle.set_width(self.x1-self.x0)
self.rectangle.set_height(self.y1-self.y0)
self.rectangle.set_xy((self.x0, self.y0))
self.rectangle.set_linestyle('dashed')
self.canvas.draw()
def deleteRectangle(self):
print('Delete rectangle!')
self.rectangle.remove()
self.canvas.draw()
self.inform('<DeleteRectangle>')
def zoomIn(self):
print('Zoom in!')
print(np.shape(self.images))
self.images = self.images[:,10:-10, 10:-10]
self.show_images()
self.nZoom = self.nZoom+1
def zoomOut(self):
print('ZoomOut!')
if np.shape(self.images) != np.shape(self.original_img):
if self.nZoom>1:
self.images = self.original_img[:,(self.nZoom-1)*10:-(self.nZoom-1)*10, (self.nZoom-1)*10:-(self.nZoom-1)*10]
self.show_images()
self.nZoom = self.nZoom-1
else:
self.images = self.original_img
self.nZoom = 0
def resetZoom(self):
print('Reset zoom!')
self.images = self.original_img
self.show_images()
self.nZoom = 0
def histogram(self):
print('Histogram!')
示例7: __init__
# 需要导入模块: from matplotlib.patches import Rectangle [as 别名]
# 或者: from matplotlib.patches.Rectangle import remove [as 别名]
#.........这里部分代码省略.........
self.bAStext.set('Add Source')
self.canvas.mpl_disconnect(self.pressid)
elif self.activeButton==self.buttonMask:
self.bAMtext.set('Add Mask')
self.canvas.mpl_disconnect(self.pressid)
self.canvas.mpl_disconnect(self.moveid)
self.canvas.mpl_disconnect(self.releaseid)
self.pressid = None
self.releaseid = None
self.activeButton = None
def addMask(self,loaded=False):
from matplotlib.patches import Rectangle
if loaded and self.parent.mask is not None:
import numpy
y,x = numpy.where(self.parent.mask==1)
x0,x1,y0,y1 = x.min(),x.max(),y.min(),y.max()
self.rubberBox = Rectangle((x0,y0),x1-x0,y1-y0,fc='none',ec='w')
self.a1.add_patch(self.rubberBox)
self.canvas.draw()
return
if self.activeButton==self.buttonMask:
self.deactivateButtons()
return
self.deactivateButtons()
self.xmask = None
def onPress(event):
axes = event.inaxes
if axes==self.a1:
self.xmask = event.xdata
self.ymask = event.ydata
if self.rubberBox is not None:
self.rubberBox.remove()
self.rubberBox = None
def onMove(event):
if self.xmask is None:
return
axes = event.inaxes
if axes==self.a1:
x,y = event.xdata,event.ydata
dx = x-self.xmask
dy = y-self.ymask
if self.rubberBox is None:
self.rubberBox = Rectangle((self.xmask,self.ymask),
dx,dy,fc='none',ec='w')
self.a1.add_patch(self.rubberBox)
else:
self.rubberBox.set_height(dy)
self.rubberBox.set_width(dx)
self.canvas.draw()
def onRelease(event):
dy = int(self.rubberBox.get_height())
dx = int(self.rubberBox.get_width())
x0,y0 = int(self.xmask),int(self.ymask)
x1,y1 = x0+dx,y0+dy
self.parent.mask = self.parent.imgs[0]*0
self.parent.mask[y0:y1,x0:x1] = 1
self.parent.mask = self.parent.mask==1
self.deactivateButtons()
self.pressid = self.canvas.mpl_connect('button_press_event',onPress)
self.moveid = self.canvas.mpl_connect('motion_notify_event',onMove)
self.releaseid = self.canvas.mpl_connect('button_release_event',onRelease)
self.bAMtext.set('Cancel')
self.activeButton = self.buttonMask
示例8: RectROI
# 需要导入模块: from matplotlib.patches import Rectangle [as 别名]
# 或者: from matplotlib.patches.Rectangle import remove [as 别名]
class RectROI(ROI):
def __init__(self, ax, fig, canvas, red=0.5, green=0.5, blue=0.5):
ROI.__init__(self, ax, fig, canvas)
self.x0 = 0
self.y0 = 0
self.x1 = 0
self.y1 = 0
self.line_color = (red, green, blue)
self.rect = None
return
def button_press_callback(self, event):
if event.inaxes:
if event.button == 1: # If you press the left mouse button
if self.rect is None:
self.x0 = event.xdata
self.y0 = event.ydata
return
def button_release_callback(self, event):
# When the user releases the mouse button, make sure the ROI line
# no longer moves with the mouse.
if event.button == 1:
if self.rect is None:
self.x1 = event.xdata
self.y1 = event.ydata
width = self.x1 - self.x0
height = self.y1 - self.y0
self.rect = Rectangle((self.x0, self.y0), width, height, color=self.line_color, fill=False, picker=True,
visible=True, figure=self.fig)
ax = event.inaxes
ax.add_patch(self.rect)
self.fig.canvas.draw()
self.grab_line = None
return
def motion_notify_callback(self, event):
'''
This is called when the user moves the mouse over the plot.
It will change the size or position of the ROI.
'''
if event.inaxes:
if (event.button == 1) and (not (self.grab_line is None)):
# Change the position of the bottom right corner of the ROI
# as the mouse is dragged across the image.
self.x1 = event.xdata
self.y1 = event.ydata
width = self.x1 - self.x0
height = self.y1 - self.y0
self.rect.set_width(width)
self.rect.set_height(height)
self.fig.canvas.draw()
if (event.button == 3) and (not (self.grab_line is None)):
# Change the position of the top left corner of the ROI
# as the mouse is dragged across the image.
self.x0 = event.xdata
self.y0 = event.ydata
self.rect.set_xy((self.x0, self.y0))
self.fig.canvas.draw()
return
def object_picked_callback(self, event):
# Set the line grabbed to the object that is clicked on.
contains, attrd = self.rect.contains(event.mouseevent)
if contains:
self.grab_line = event.artist
return
def AddLines(self):
if self.rect is not None:
self.ax.add_patch(self.rect)
self.ax.figure.canvas.draw()
return
def RemoveLines(self):
try:
self.rect.remove()
self.fig.canvas.draw()
except AttributeError:
return
return
def GetDimensions(self):
dim_list = []
#.........这里部分代码省略.........
示例9: WindowSelectionRectangle
# 需要导入模块: from matplotlib.patches import Rectangle [as 别名]
# 或者: from matplotlib.patches.Rectangle import remove [as 别名]
class WindowSelectionRectangle(object):
def __init__(self, event, axis, on_window_selection_callback):
self.axis = axis
if event.inaxes != self.axis:
return
# Store the axes it has been initialized in.
self.axes = event.inaxes
ymin, ymax = self.axes.get_ylim()
self.min_x = event.xdata
self.intial_selection_active = True
self.rect = Rectangle((event.xdata, ymin), 0, ymax - ymin,
facecolor="0.3", alpha=0.5, edgecolor="0.5")
self.axes.add_patch(self.rect)
# Get the canvas.
self.canvas = self.rect.figure.canvas
# Use blittig for fast animations.
self.rect.set_animated(True)
self.background = self.canvas.copy_from_bbox(self.rect.axes.bbox)
self._connect()
self.on_window_selection_callback = on_window_selection_callback
def _connect(self):
"""
Connect to the necessary events.
"""
self.conn_button_press = self.rect.figure.canvas.mpl_connect(
'button_press_event', self.on_button_press)
self.conn_button_release = self.rect.figure.canvas.mpl_connect(
'button_release_event', self.on_button_release)
self.conn_mouse_motion = self.rect.figure.canvas.mpl_connect(
'motion_notify_event', self.on_mouse_motion)
def on_button_press(self, event):
pass
def on_button_release(self, event):
if event.inaxes != self.axis:
return
if event.button != 1:
return
# turn off the rect animation property and reset the background
self.rect.set_animated(False)
self.background = None
self.intial_selection_active = False
self.canvas.draw()
x = self.rect.get_x()
width = self.rect.get_width()
if width < 0:
x = x + width
width = abs(width)
self.rect.remove()
self.on_window_selection_callback(x, width, self.axis)
def on_mouse_motion(self, event):
if event.button != 1 or \
self.intial_selection_active is not True:
return
if event.xdata is not None:
self.rect.set_width(event.xdata - self.min_x)
# restore the background region
self.canvas.restore_region(self.background)
# redraw just the current rectangle
self.axes.draw_artist(self.rect)
# blit just the redrawn area
self.canvas.blit(self.axes.bbox)
示例10: __init__
# 需要导入模块: from matplotlib.patches import Rectangle [as 别名]
# 或者: from matplotlib.patches.Rectangle import remove [as 别名]
class ItemArtist:
def __init__(self, position, state):
self.position = position
indx = state.positions.index(position)
self.top = -state.tops[indx]
self.top_line, = pylab.plot([0,width], 2*[self.top], c='b')
self.bottom = -state.bottoms[indx]
self.bottom_line, = pylab.plot([0,width], 2*[self.bottom], c='b')
self.edge = -state.edges[indx]
self.edge_line, = pylab.plot([0,width], 2*[self.edge], c='g')
self.label = Text(width/2, (self.top+self.bottom)/2,
str(position), va='center', ha='center')
self.axes = pylab.gca()
self.axes.add_artist(self.label)
self.src_box = None
self.exp_box = None
self._check_boxes(state)
def _check_boxes(self, state):
if self.position == state.src:
if self.src_box == None:
self.src_box = Rectangle((0, self.bottom), width,
self.top - self.bottom, fill=True, ec=None, fc='0.7')
self.axes.add_patch(self.src_box)
else:
self.src_box.set_y(self.bottom)
self.src_box.set_height(self.top - self.bottom)
elif self.position == state.exp1:
if state.exp1 < state.src:
gap_bottom = self.top - state.exp1_gap
else:
gap_bottom = self.bottom
if self.exp_box == None:
self.exp_box = Rectangle((0,gap_bottom), width,
state.exp1_gap, fill=True, ec=None, fc='0.7')
self.axes.add_patch(self.exp_box)
else:
self.exp_box.set_y(gap_bottom)
self.exp_box.set_height(state.exp1_gap)
elif self.position == state.exp2:
if state.exp2 < state.src:
gap_bottom = self.top - state.exp2_gap
else:
gap_bottom = self.bottom
if self.exp_box == None:
self.exp_box = Rectangle((0,gap_bottom), width, state.exp2_gap,
fill=True, ec=None, fc='0.7')
self.axes.add_patch(self.exp_box)
else:
self.exp_box.set_y(gap_bottom)
self.exp_box.set_height(state.exp2_gap)
else:
if self.src_box != None:
self.src_box.remove()
self.src_box = None
if self.exp_box != None:
self.exp_box.remove()
self.exp_box = None
def inState(self, state):
return self.position in state.positions
def update(self, position, state):
moved = False
if position != self.position:
self.position = position
self.label.set_text(str(position))
indx = state.positions.index(self.position)
old_top = self.top
self.top = -state.tops[indx]
if old_top != self.top:
self.top_line.set_ydata(2*[self.top])
moved = True
old_bottom = self.bottom
self.bottom = -state.bottoms[indx]
if old_bottom != self.bottom:
self.bottom_line.set_ydata(2*[self.bottom])
moved = True
old_edge = self.edge
self.edge = -state.edges[indx]
#.........这里部分代码省略.........