Hello everybody,
<br/><br/><br/>I am trying to combine mouse button events with a pick event in scatter plot. Different functionalities take place, based on the mouse button pressed each time. I want each functionality to be triggered with a combination of mouse buttons events and/or key press events. When the left mouse button is pressed matplotlib's Lasso widget is called and with the included points functionality 1 takes place. When Shift+LMB are pressed a Lasso is drawn and functionality 2 takes place with the included points. When Alt+LMB are pressed a Lasso is drawn and with the included points functionality 3 takes place. Last, but not least, when I press the RMB a pick event is triggered and the index of the selected point in the scatter plot is given.
<br/><br/>Since I added the pick event, the aforementioned functionalities work correctly until a pick event is triggered. When it is triggered it seems that the canvas gets locked and I can not use any other functionality. I don't get the index of the selected points and I do not get any errors.
<br/><br/><br/>----------------Code-------------------------
<br/><br/>import logging
<br/>import matplotlib
<br/>from matplotlib.widgets import Lasso
<br/>from matplotlib.colors import colorConverter
<br/>from matplotlib.collections import RegularPolyCollection
<br/>from matplotlib import path
<br/>import numpy as np
<br/>import matplotlib.pyplot as plt
<br/>from numpy.random import rand
<br/><br/>logger = logging.getLogger()
<br/>logger.setLevel(logging.DEBUG)
<br/><br/><br/>class Datum(object):
<br/> colorin = colorConverter.to_rgba('red')
<br/> colorShift = colorConverter.to_rgba('cyan')
<br/> colorCtrl = colorConverter.to_rgba('pink')
<br/> colorout = colorConverter.to_rgba('blue')
<br/><br/> def __init__(self, x, y, include=False):
<br/> self.x = x
<br/> self.y = y
<br/> if include:
<br/> self.color = self.colorin
<br/> else:
<br/> self.color = self.colorout
<br/><br/><br/>class LassoManager(object):
<br/> def __init__(self, ax, data):
<br/> self.axes = ax
<br/> self.canvas = ax.figure.canvas
<br/> self.data = data
<br/><br/> self.Nxy = len(data)
<br/><br/> facecolors = [d.color for d in data]
<br/> self.xys = [(d.x, d.y) for d in data]
<br/> fig = ax.figure
<br/> self.collection = RegularPolyCollection(fig.dpi, 6, sizes=(100,),facecolors=facecolors, offsets = self.xys, transOffset = ax.transData)
<br/><br/> ax.add_collection(self.collection)
<br/><br/> self.pick=self.canvas.mpl_connect('pick_event', self.onpick)
<br/> self.cid = self.canvas.mpl_connect('button_press_event', self.onpress)
<br/> self.keyPress = self.canvas.mpl_connect('key_press_event', self.onKeyPress)
<br/> self.keyRelease = self.canvas.mpl_connect('key_release_event', self.onKeyRelease)
<br/> self.lasso = None
<br/> self.shiftKey = False
<br/> self.ctrlKey = False
<br/><br/> def callback(self, verts):
<br/> logging.debug('in LassoManager.callback(). Shift: %s, Ctrl: %s' % (self.shiftKey, self.ctrlKey))
<br/> facecolors = self.collection.get_facecolors()
<br/> p = path.Path(verts)
<br/> ind = p.contains_points(self.xys)
<br/> for i in range(len(self.xys)):
<br/> if ind[i]:
<br/> if self.shiftKey:
<br/> facecolors[i] = Datum.colorShift
<br/> elif self.ctrlKey:
<br/> facecolors[i] = Datum.colorCtrl
<br/> else:
<br/> facecolors[i] = Datum.colorin
<br/> print self.xys[i]
<br/> else:
<br/> facecolors[i] = Datum.colorout
<br/><br/> self.canvas.draw_idle()
<br/> self.canvas.widgetlock.release(self.lasso)
<br/> del self.lasso
<br/><br/> def onpress(self, event):
<br/> if self.canvas.widgetlock.locked(): return
<br/> if event.inaxes is None: return
<br/> self.lasso = Lasso(event.inaxes, (event.xdata, event.ydata), self.callback)
<br/> # acquire a lock on the widget drawing
<br/> self.canvas.widgetlock(self.lasso)
<br/><br/> def onKeyPress(self, event):
<br/> logging.debug('in LassoManager.onKeyPress(). Event received: %s (key: %s)' % (event, event.key))
<br/> if event.key == 'alt':
<br/> self.ctrlKey = True
<br/> if event.key == 'shift':
<br/> self.shiftKey = True
<br/><br/> def onKeyRelease(self, event):
<br/> logging.debug('in LassoManager.onKeyRelease(). Event received: %s (key: %s)' % (event, event.key))
<br/> if event.key == 'alt':
<br/> self.ctrlKey = False
<br/> if event.key == 'shift':
<br/> self.shiftKey = False
<br/><br/><br/> def onpick(self,event):
<br/><br/> if event.mouseevent.button == 3:
<br/><br/> index = event.ind
<br/> print('onpick3 scatter:', index)
<br/><br/> if __name__ == '__main__':
<br/><br/> data = [Datum(*xy) for xy in rand(100, 2)]
<br/><br/> ax = plt.axes(xlim=(0,1), ylim=(0,1), autoscale_on=False)
<br/> lman = LassoManager(ax, data)
<br/><br/> plt.show()
<br/><br/>------------------End of Code-----------------------------
<br/><br/>Any suggestions on what might be causing this malfunction? What am I doing wrong?
<br/><br/>
<hr noshade="noshade" size="1" color="#cccccc" />
<div style="color:#444; font: 12px tahoma,geneva,helvetica,arial,sans-serif;">
<div style="font-weight:bold">If you reply to this email, your message will be added to the discussion below:</div>
<a href="http://matplotlib.1069221.n5.nabble.com/Problem-Can-t-combine-mouse-button-events-and-pick-events-in-a-scatter-plot-tp46112.html">http://matplotlib.1069221.n5.nabble.com/Problem-Can-t-combine-mouse-button-events-and-pick-events-in-a-scatter-plot-tp46112.html</a>
</div>
<div style="color:#666; font: 11px tahoma,geneva,helvetica,arial,sans-serif;margin-top:.4em">
This email was sent by <a href="http://matplotlib.1069221.n5.nabble.com/template/NamlServlet.jtp?macro=user_nodes&user=4612">joh</a> (via Nabble)<br/>
To receive all replies by email, <a href="http://matplotlib.1069221.n5.nabble.com/template/NamlServlet.jtp?macro=subscribe_by_code&node=46112&code=bWF0cGxvdGxpYi11c2Vyc0BweXRob24ub3JnfDQ2MTEyfC0xNjQ0NjkzNDk1">subscribe to this discussion</a><br/>
</div>