Python matplotlib.backend_bases.MouseEvent() Examples
The following are 20
code examples of matplotlib.backend_bases.MouseEvent().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
matplotlib.backend_bases
, or try the search function
.
Example #1
Source File: drag_helper.py From pylustrator with GNU General Public License v3.0 | 6 votes |
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
Example #2
Source File: drag_helper.py From pylustrator with GNU General Public License v3.0 | 6 votes |
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)
Example #3
Source File: test_mplcursors.py From mplcursors with MIT License | 6 votes |
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)
Example #4
Source File: drag_helper.py From pylustrator with GNU General Public License v3.0 | 5 votes |
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()
Example #5
Source File: test_text.py From twitter-stock-recommendation with MIT License | 5 votes |
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)
Example #6
Source File: main.py From py-pdf-parser with MIT License | 5 votes |
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
Example #7
Source File: test_text.py From coffeegrindsize with MIT License | 5 votes |
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)
Example #8
Source File: test_text.py From python3_ios with BSD 3-Clause "New" or "Revised" License | 5 votes |
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)
Example #9
Source File: test_text.py From neural-network-animation with MIT License | 5 votes |
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)
Example #10
Source File: drag_helper.py From pylustrator with GNU General Public License v3.0 | 5 votes |
def applyOffset(self, pos: (float, float), event: MouseEvent): self.set_xy((self.ox+pos[0], self.oy+pos[1]))
Example #11
Source File: drag_helper.py From pylustrator with GNU General Public License v3.0 | 5 votes |
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()
Example #12
Source File: drag_helper.py From pylustrator with GNU General Public License v3.0 | 5 votes |
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)
Example #13
Source File: drag_helper.py From pylustrator with GNU General Public License v3.0 | 5 votes |
def releasedEvent(self, event: MouseEvent): """ when the mouse is released """ for snap in self.snaps: snap.remove() self.snaps = [] self.parent.end_move()
Example #14
Source File: drag_helper.py From pylustrator with GNU General Public License v3.0 | 5 votes |
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)
Example #15
Source File: drag_helper.py From pylustrator with GNU General Public License v3.0 | 5 votes |
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)
Example #16
Source File: drag_helper.py From pylustrator with GNU General Public License v3.0 | 5 votes |
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)
Example #17
Source File: drag_helper.py From pylustrator with GNU General Public License v3.0 | 5 votes |
def on_motion(self, evt: MouseEvent): """ callback when the object is moved """ if self.got_artist: self.movedEvent(evt) self.moved = True
Example #18
Source File: test_image.py From python3_ios with BSD 3-Clause "New" or "Revised" License | 4 votes |
def test_cursor_data(): from matplotlib.backend_bases import MouseEvent fig, ax = plt.subplots() im = ax.imshow(np.arange(100).reshape(10, 10), origin='upper') x, y = 4, 4 xdisp, ydisp = ax.transData.transform_point([x, y]) event = MouseEvent('motion_notify_event', fig.canvas, xdisp, ydisp) assert im.get_cursor_data(event) == 44 # Now try for a point outside the image # Tests issue #4957 x, y = 10.1, 4 xdisp, ydisp = ax.transData.transform_point([x, y]) event = MouseEvent('motion_notify_event', fig.canvas, xdisp, ydisp) assert im.get_cursor_data(event) is None # Hmm, something is wrong here... I get 0, not None... # But, this works further down in the tests with extents flipped #x, y = 0.1, -0.1 #xdisp, ydisp = ax.transData.transform_point([x, y]) #event = MouseEvent('motion_notify_event', fig.canvas, xdisp, ydisp) #z = im.get_cursor_data(event) #assert z is None, "Did not get None, got %d" % z ax.clear() # Now try with the extents flipped. im = ax.imshow(np.arange(100).reshape(10, 10), origin='lower') x, y = 4, 4 xdisp, ydisp = ax.transData.transform_point([x, y]) event = MouseEvent('motion_notify_event', fig.canvas, xdisp, ydisp) assert im.get_cursor_data(event) == 44 fig, ax = plt.subplots() im = ax.imshow(np.arange(100).reshape(10, 10), extent=[0, 0.5, 0, 0.5]) x, y = 0.25, 0.25 xdisp, ydisp = ax.transData.transform_point([x, y]) event = MouseEvent('motion_notify_event', fig.canvas, xdisp, ydisp) assert im.get_cursor_data(event) == 55 # Now try for a point outside the image # Tests issue #4957 x, y = 0.75, 0.25 xdisp, ydisp = ax.transData.transform_point([x, y]) event = MouseEvent('motion_notify_event', fig.canvas, xdisp, ydisp) assert im.get_cursor_data(event) is None x, y = 0.01, -0.01 xdisp, ydisp = ax.transData.transform_point([x, y]) event = MouseEvent('motion_notify_event', fig.canvas, xdisp, ydisp) assert im.get_cursor_data(event) is None
Example #19
Source File: test_image.py From coffeegrindsize with MIT License | 4 votes |
def test_cursor_data(): from matplotlib.backend_bases import MouseEvent fig, ax = plt.subplots() im = ax.imshow(np.arange(100).reshape(10, 10), origin='upper') x, y = 4, 4 xdisp, ydisp = ax.transData.transform_point([x, y]) event = MouseEvent('motion_notify_event', fig.canvas, xdisp, ydisp) assert im.get_cursor_data(event) == 44 # Now try for a point outside the image # Tests issue #4957 x, y = 10.1, 4 xdisp, ydisp = ax.transData.transform_point([x, y]) event = MouseEvent('motion_notify_event', fig.canvas, xdisp, ydisp) assert im.get_cursor_data(event) is None # Hmm, something is wrong here... I get 0, not None... # But, this works further down in the tests with extents flipped #x, y = 0.1, -0.1 #xdisp, ydisp = ax.transData.transform_point([x, y]) #event = MouseEvent('motion_notify_event', fig.canvas, xdisp, ydisp) #z = im.get_cursor_data(event) #assert z is None, "Did not get None, got %d" % z ax.clear() # Now try with the extents flipped. im = ax.imshow(np.arange(100).reshape(10, 10), origin='lower') x, y = 4, 4 xdisp, ydisp = ax.transData.transform_point([x, y]) event = MouseEvent('motion_notify_event', fig.canvas, xdisp, ydisp) assert im.get_cursor_data(event) == 44 fig, ax = plt.subplots() im = ax.imshow(np.arange(100).reshape(10, 10), extent=[0, 0.5, 0, 0.5]) x, y = 0.25, 0.25 xdisp, ydisp = ax.transData.transform_point([x, y]) event = MouseEvent('motion_notify_event', fig.canvas, xdisp, ydisp) assert im.get_cursor_data(event) == 55 # Now try for a point outside the image # Tests issue #4957 x, y = 0.75, 0.25 xdisp, ydisp = ax.transData.transform_point([x, y]) event = MouseEvent('motion_notify_event', fig.canvas, xdisp, ydisp) assert im.get_cursor_data(event) is None x, y = 0.01, -0.01 xdisp, ydisp = ax.transData.transform_point([x, y]) event = MouseEvent('motion_notify_event', fig.canvas, xdisp, ydisp) assert im.get_cursor_data(event) is None
Example #20
Source File: test_image.py From twitter-stock-recommendation with MIT License | 4 votes |
def test_cursor_data(): from matplotlib.backend_bases import MouseEvent fig, ax = plt.subplots() im = ax.imshow(np.arange(100).reshape(10, 10), origin='upper') x, y = 4, 4 xdisp, ydisp = ax.transData.transform_point([x, y]) event = MouseEvent('motion_notify_event', fig.canvas, xdisp, ydisp) assert im.get_cursor_data(event) == 44 # Now try for a point outside the image # Tests issue #4957 x, y = 10.1, 4 xdisp, ydisp = ax.transData.transform_point([x, y]) event = MouseEvent('motion_notify_event', fig.canvas, xdisp, ydisp) assert im.get_cursor_data(event) is None # Hmm, something is wrong here... I get 0, not None... # But, this works further down in the tests with extents flipped #x, y = 0.1, -0.1 #xdisp, ydisp = ax.transData.transform_point([x, y]) #event = MouseEvent('motion_notify_event', fig.canvas, xdisp, ydisp) #z = im.get_cursor_data(event) #assert z is None, "Did not get None, got %d" % z ax.clear() # Now try with the extents flipped. im = ax.imshow(np.arange(100).reshape(10, 10), origin='lower') x, y = 4, 4 xdisp, ydisp = ax.transData.transform_point([x, y]) event = MouseEvent('motion_notify_event', fig.canvas, xdisp, ydisp) assert im.get_cursor_data(event) == 44 fig, ax = plt.subplots() im = ax.imshow(np.arange(100).reshape(10, 10), extent=[0, 0.5, 0, 0.5]) x, y = 0.25, 0.25 xdisp, ydisp = ax.transData.transform_point([x, y]) event = MouseEvent('motion_notify_event', fig.canvas, xdisp, ydisp) assert im.get_cursor_data(event) == 55 # Now try for a point outside the image # Tests issue #4957 x, y = 0.75, 0.25 xdisp, ydisp = ax.transData.transform_point([x, y]) event = MouseEvent('motion_notify_event', fig.canvas, xdisp, ydisp) assert im.get_cursor_data(event) is None x, y = 0.01, -0.01 xdisp, ydisp = ax.transData.transform_point([x, y]) event = MouseEvent('motion_notify_event', fig.canvas, xdisp, ydisp) assert im.get_cursor_data(event) is None