本文整理汇总了Python中matplotlib.backend_bases.MouseEvent方法的典型用法代码示例。如果您正苦于以下问题:Python backend_bases.MouseEvent方法的具体用法?Python backend_bases.MouseEvent怎么用?Python backend_bases.MouseEvent使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类matplotlib.backend_bases
的用法示例。
在下文中一共展示了backend_bases.MouseEvent方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _process_event
# 需要导入模块: from matplotlib import backend_bases [as 别名]
# 或者: from matplotlib.backend_bases import MouseEvent [as 别名]
def _process_event(name, ax, coords, *args):
ax.viewLim # unstale viewLim.
if name == "__mouse_click__":
# So that the dragging callbacks don't go crazy.
_process_event("button_press_event", ax, coords, *args)
_process_event("button_release_event", ax, coords, *args)
return
display_coords = ax.transData.transform(coords)
if name in ["button_press_event", "button_release_event",
"motion_notify_event", "scroll_event"]:
event = MouseEvent(name, ax.figure.canvas, *display_coords, *args)
elif name in ["key_press_event", "key_release_event"]:
event = KeyEvent(name, ax.figure.canvas, *args, *display_coords)
else:
raise ValueError(f"Unknown event name {name!r}")
ax.figure.canvas.callbacks.process(name, event)
示例2: get_picked_element
# 需要导入模块: from matplotlib import backend_bases [as 别名]
# 或者: from matplotlib.backend_bases import MouseEvent [as 别名]
def get_picked_element(self, event: MouseEvent, element: Artist = None, picked_element: Artist = None, last_selected: Artist = None):
""" get the picked element that an event refers to.
To implement selection of elements at the back with multiple clicks.
"""
# start with the figure
if element is None:
element = self.figure
finished = False
# iterate over all children
for child in sorted(element.get_children(), key=lambda x: x.get_zorder()):
# check if the element is contained in the event and has an active dragger
#if child.contains(event)[0] and ((getattr(child, "_draggable", None) and getattr(child, "_draggable",
# None).connected) or isinstance(child, GrabberGeneric) or isinstance(child, GrabbableRectangleSelection)):
if child.get_visible() and child.contains(event)[0] and (child.pickable() or isinstance(child, GrabberGeneric)) and not (child.get_label() is not None and child.get_label().startswith("_")):
# if the element is the last selected, finish the search
if child == last_selected:
return picked_element, True
# use this element as the current best matching element
picked_element = child
# iterate over the children's children
picked_element, finished = self.get_picked_element(event, child, picked_element, last_selected=last_selected)
# if the subcall wants to finish, just break the loop
if finished:
break
return picked_element, finished
示例3: button_press_event0
# 需要导入模块: from matplotlib import backend_bases [as 别名]
# 或者: from matplotlib.backend_bases import MouseEvent [as 别名]
def button_press_event0(self, event: MouseEvent):
""" when the mouse button is pressed """
if event.button == 1:
last = self.selection.targets[-1] if len(self.selection.targets) else None
contained = np.any([t.target.contains(event)[0] for t in self.selection.targets])
# recursively iterate over all elements
picked_element, _ = self.get_picked_element(event, last_selected=last if event.dblclick else None)
# if the element is a grabber, store it
if isinstance(picked_element, GrabberGeneric):
self.grab_element = picked_element
# if not, we want to keep our selected element, if the click was in the area of the selected element
elif len(self.selection.targets) == 0 or not contained or event.dblclick:
self.select_element(picked_element, event)
contained = True
# if we have a grabber, notify it
if self.grab_element:
self.grab_element.button_press_event(event)
# if not, notify the selected element
elif contained:
self.selection.button_press_event(event)
示例4: on_motion
# 需要导入模块: from matplotlib import backend_bases [as 别名]
# 或者: from matplotlib.backend_bases import MouseEvent [as 别名]
def on_motion(self, evt: MouseEvent):
""" callback when the object is moved """
if self.got_artist:
self.movedEvent(evt)
self.moved = True
示例5: button_press_event
# 需要导入模块: from matplotlib import backend_bases [as 别名]
# 或者: from matplotlib.backend_bases import MouseEvent [as 别名]
def button_press_event(self, evt: MouseEvent):
""" when the mouse is pressed """
self.got_artist = True
self.moved = False
self._c1 = self.figure.canvas.mpl_connect('motion_notify_event', self.on_motion)
self.clickedEvent(evt)
示例6: button_release_event
# 需要导入模块: from matplotlib import backend_bases [as 别名]
# 或者: from matplotlib.backend_bases import MouseEvent [as 别名]
def button_release_event(self, event: MouseEvent):
""" when the mouse is released """
if self.got_artist:
self.got_artist = False
self.figure.canvas.mpl_disconnect(self._c1)
self.releasedEvent(event)
示例7: clickedEvent
# 需要导入模块: from matplotlib import backend_bases [as 别名]
# 或者: from matplotlib.backend_bases import MouseEvent [as 别名]
def clickedEvent(self, event: MouseEvent):
""" when the mouse is clicked """
self.parent.start_move()
self.mouse_xy = (event.x, event.y)
for s in self.snaps:
s.remove()
self.snaps = []
self.snaps = getSnaps(self.targets, self.dir, no_height=self.no_height)
示例8: releasedEvent
# 需要导入模块: from matplotlib import backend_bases [as 别名]
# 或者: from matplotlib.backend_bases import MouseEvent [as 别名]
def releasedEvent(self, event: MouseEvent):
""" when the mouse is released """
for snap in self.snaps:
snap.remove()
self.snaps = []
self.parent.end_move()
示例9: button_release_event0
# 需要导入模块: from matplotlib import backend_bases [as 别名]
# 或者: from matplotlib.backend_bases import MouseEvent [as 别名]
def button_release_event0(self, event: MouseEvent):
""" when the mouse button is released """
# release the grabber
if self.grab_element:
self.grab_element.button_release_event(event)
self.grab_element = None
# or notify the selected element
elif len(self.selection.targets):
self.selection.button_release_event(event)
示例10: select_element
# 需要导入模块: from matplotlib import backend_bases [as 别名]
# 或者: from matplotlib.backend_bases import MouseEvent [as 别名]
def select_element(self, element: Artist, event: MouseEvent = None):
""" select an artist in a figure """
# do nothing if it is already selected
if element == self.selected_element:
return
# if there was was previously selected element, deselect it
if self.selected_element is not None:
self.on_deselect(event)
# if there is a new element, select it
self.on_select(element, event)
self.selected_element = element
self.figure.canvas.draw()
示例11: on_deselect
# 需要导入模块: from matplotlib import backend_bases [as 别名]
# 或者: from matplotlib.backend_bases import MouseEvent [as 别名]
def on_deselect(self, event: MouseEvent):
""" deselect currently selected artists"""
modifier = "shift" in event.key.split("+") if event is not None and event.key is not None else False
# only if the modifier key is not used
if not modifier:
self.selection.clear_targets()
示例12: applyOffset
# 需要导入模块: from matplotlib import backend_bases [as 别名]
# 或者: from matplotlib.backend_bases import MouseEvent [as 别名]
def applyOffset(self, pos: (float, float), event: MouseEvent):
self.set_xy((self.ox+pos[0], self.oy+pos[1]))
示例13: test_contains
# 需要导入模块: from matplotlib import backend_bases [as 别名]
# 或者: from matplotlib.backend_bases import MouseEvent [as 别名]
def test_contains():
import matplotlib.backend_bases as mbackend
fig = plt.figure()
ax = plt.axes()
mevent = mbackend.MouseEvent(
'button_press_event', fig.canvas, 0.5, 0.5, 1, None)
xs = np.linspace(0.25, 0.75, 30)
ys = np.linspace(0.25, 0.75, 30)
xs, ys = np.meshgrid(xs, ys)
txt = plt.text(
0.48, 0.52, 'hello world', ha='center', fontsize=30, rotation=30)
# uncomment to draw the text's bounding box
# txt.set_bbox(dict(edgecolor='black', facecolor='none'))
# draw the text. This is important, as the contains method can only work
# when a renderer exists.
plt.draw()
for x, y in zip(xs.flat, ys.flat):
mevent.x, mevent.y = plt.gca().transAxes.transform_point([x, y])
contains, _ = txt.contains(mevent)
color = 'yellow' if contains else 'red'
# capture the viewLim, plot a point, and reset the viewLim
vl = ax.viewLim.frozen()
ax.plot(x, y, 'o', color=color)
ax.viewLim.set(vl)
示例14: test_contains
# 需要导入模块: from matplotlib import backend_bases [as 别名]
# 或者: from matplotlib.backend_bases import MouseEvent [as 别名]
def test_contains():
import matplotlib.backend_bases as mbackend
fig = plt.figure()
ax = plt.axes()
mevent = mbackend.MouseEvent(
'button_press_event', fig.canvas, 0.5, 0.5, 1, None)
xs = np.linspace(0.25, 0.75, 30)
ys = np.linspace(0.25, 0.75, 30)
xs, ys = np.meshgrid(xs, ys)
txt = plt.text(
0.48, 0.52, 'hello world', ha='center', fontsize=30, rotation=30)
# uncomment to draw the text's bounding box
# txt.set_bbox(dict(edgecolor='black', facecolor='none'))
# draw the text. This is important, as the contains method can only work
# when a renderer exists.
fig.canvas.draw()
for x, y in zip(xs.flat, ys.flat):
mevent.x, mevent.y = plt.gca().transAxes.transform_point([x, y])
contains, _ = txt.contains(mevent)
color = 'yellow' if contains else 'red'
# capture the viewLim, plot a point, and reset the viewLim
vl = ax.viewLim.frozen()
ax.plot(x, y, 'o', color=color)
ax.viewLim.set(vl)
示例15: __on_click
# 需要导入模块: from matplotlib import backend_bases [as 别名]
# 或者: from matplotlib.backend_bases import MouseEvent [as 别名]
def __on_click(self, event: "MouseEvent"):
if event.button == MouseButton.MIDDLE:
self.__clear_clicked_elements()
return
if event.button not in [MouseButton.LEFT, MouseButton.RIGHT]:
return
for rect in self.__ax.patches:
if not rect.contains(event)[0]:
continue
# rect is the rectangle we clicked on!
self.__clicked_elements[event.button] = rect.element
self.__update_text()
return