## matplotlib-users

 [Matplotlib-users] Suggestion On Handling Plots From: - 2005-09-09 21:54:15 ```Hello NG, in my app, I plot the position of some oil wells represented by dots in a x-y plot. The coordinates of the positions are integers. I use the command: myaxes.plot(xc, yc, "ko") where xc, yc are the wells' coordinates. After some calculations, I would like to do these 2 things in the most *efficient* or *intelligent* way: 1) Depending on the value of a certain parameter, every well (dot) should have a color (there are only 5 colors that a well may assume). For example, if this parameter has a value less than 0.1, a well is painted green, if it has a value greater than 0.1 but smaller than 1, a well is painted blue and so on. At the moment, the only way I can think about to implement such a thing is to do: for ii in xrange(len(xc)): axdata.plot([xc[i]], [yc[i]], 'ko') which means defining a new Lines2D object for every well. Next, it is easier to assign a different colour to each well. 2) When the user presses the mouse "near" a well, I would like to be able to: a) Verify that the mouse click was "really" near a well and not somewhere else, where there are no wells; b) If a) is True, retrieving which wells was clicked (which Lines2D was clicked). At the moment, using the plot command: self.mp = myaxes.plot (xc, yc, "ko") I do these steps: def mouse_press(self, event): # Check if the click is inside the axes if event.inaxes is None: return xvalue, yvalue = event.xdata, event.ydata # Round to integer the click coordinates xc, yc = int(round(xvalue)), int(round(yvalue)) # get xdata and ydata for all the wells xdata = self.mp[0][0]. get_xdata() ydata = self.mp[0][0].get_ydata() if xc in xdata: # find which wells have X coordinates equals to xc indxs = [x[0] for x in enumerate(xdata) if x [1] == xc] if yc in ydata: # find which wells have Y coordinates equals to yc indys = [x[0] for x in enumerate(ydata) if x[1] == yc] lenx = len(indxs) leny = len(indys) # create 2 lists that can be compared in length if lenx < leny: indxs.extend([-9999]*(leny-lenx)) elif leny < lenx: indys.extend([-9999]*(lenx-leny)) tmp = [] # find the intersections between the 2 lists for xcoord in indxs: if xcoord in indys: tmp. append(indys.index(xcoord)) # ==> The click matches the well if len(tmp) > 0 if len(tmp) > 0: ind = tmp[0] # get wellnames from other sources currentwell = self. wellnames[indys[ind]] Thank you for every suggestion. Great work with matplotlib! Andrea. ```
 Re: [Matplotlib-users] Suggestion On Handling Plots From: John Hunter - 2005-09-10 02:38:29 ```>>>>> "andrea" == andrea gavana@... it writes: andrea> I do these steps: andrea> def mouse_press(self, event): You code is badly wrapped, which makes it really hard to read and test. Could you post again being careful to make sure that the lines don't wrap? Also, see the recent wiki entry "Interactive Plotting" which addresses related issues, particularly picking points in a scatter plot. JDH ```