1

I am writing a program where I have multiple figures that are plotting data from multiple sources in different forms (e.g. x vs. y, x/y vs. x, etc.). I am trying to introduce some element of interaction where the user can select certain points on one figure (whether they are from one series or multiple series) and for those selected points, the points are changed to a user-selected color on all figures. I've tried to implement pick events and the lasso selector to do the job, but I can't manage to interact with multiple figures, collections, and axes simultaneously. I've included some test code that I am using to just try to get this to work and as you will see, the interaction has some serious issues! Thanks for the help!

#plot picker

import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np

class SelectPoint(object):


   def __init__(self, ax, collection, colorother='#ffffff'):
    self.canvas = ax.figure.canvas
    self.collection = collection
    self.colorother = colorother
    self.colorbyitem=colorbyitem
    self.canvas.mpl_connect('pick_event', self.on_pick)

   def on_pick(self, event):
    self.thisparticle=event.artist

    self.label=self.thisparticle.get_label()
    self.index=event.ind[0]
    print self.index
    print self.colorbyitem[self.label][self.index]
    self.colorbyitem[self.label][self.index]='#ff8000'
    self.collection.set_color(self.colorbyitem[self.label])
    self.collection.set_edgecolor('#000000')
    self.canvas.draw_idle()

colors=['#0000ff','#0000ff','#0000ff','#0000ff','#0000ff']
colors1=['#00ffff','#00ffff','#00ffff','#00ffff','#00ffff']

x1=[1,2,3,4,5]
y1=[1,2,3,4,5]
x1=np.array(x1)
y1=np.array(y1)
colors=np.array(colors)
colors1=np.array(colors1)


colorbyitem={}


item='23532_10'
item2='23555_04'
colorbyitem[item]=colors
colorbyitem[item2]=colors1


x=[1,2,3,4,5]
y=[1,2,3,4,5]
x=np.array(x)
y=np.array(y)

subplot_kw = dict(xlim=(0, 5), ylim=(0, 5), autoscale_on=False)

fig5v4, ax=plt.subplots(subplot_kw=subplot_kw)
fig5v6, ax1=plt.subplots(subplot_kw=subplot_kw)

coll=ax.scatter(x,y, c=colors, s=75, picker=1, label=item)
coll1=ax1.scatter(x1, y1, c=colors, s=75, picker=1, label=item)

coll3=ax.scatter(1.1*y, .9*x, c=colors1, s=75, picker=1, label=item2)

coll4=ax1.scatter(1.1*y, .9*x, c=colors1, s=75, picker=1, label=item2)


selector=SelectPoint(ax, coll)
selector2=SelectPoint(ax1, coll1)   

selector3=SelectPoint(ax, coll3)
selector4=SelectPoint(ax1, coll4)
plt.show()

raw_input()
Ben D.
  • 31
  • 5
  • Start by fixing the identation, I'm not sure what is part of the class SelectPoint and what isn't. – tglaria Dec 11 '15 at 13:29
  • @tglaria valid point...fixed! – Ben D. Dec 11 '15 at 13:47
  • Are you running this from interpreter or what? Shouldn't this run from a GUI (to detect mouse clicks)? – tglaria Dec 11 '15 at 13:53
  • 1
    @tglaria the main objective is to detect which point in a scatter plot is clicked on and then color that point and associated points on other plots. The figures detect the pick events. I have been able to do it with one figure, one axis, and one collection. That's relatively simple. But i can't get the interactions to work between multiple figure instances – Ben D. Dec 11 '15 at 14:00
  • Ok, I don't get (yet) how that getting event in matplot works but I have some ideas/questions: Why are you using diferent figures for the plots (at least on my computer Python 2.7)? I would put all the figures of one collection to order them. In my experiments, you're not updating all the plots, only the one that's been clicked. Is that the error you're getting? – tglaria Dec 11 '15 at 14:56
  • @tglaria i clearly don't quite understand it either. in the program I am writing, I need to produce multiple plots. Each of these plots represents different data from certain "collections" of data. For example, say I have samples A & B (represented by item and item2 in my snippet above). For each of these samples, let's say they each have 5 subsamples (e.g. A1, A2, etc.). Each of the subsamples then has data from several sets of measurements (e.g. meas1, meas2, meas3). So on figure 1 for example, I need meas1 vs. meas2 for all subsamples of sample A and B. On figure 2, meas3 vs. meas4. Etc. – Ben D. Dec 11 '15 at 15:45
  • 1
    You can attach the same callback function to multiple `canvas`, you will just have to do the book keeping to keep track of which `ax` you are working with. I would move the setup/registration to a method on `SelectPoint` called `add_axs` or some such. The `event` object know what `ax` in is from. – tacaswell Dec 13 '15 at 05:17
  • 1
    What I would try is call the draw function `self.canvas.draw_idle()` on every figure (no matter which figure is clicked). – tglaria Dec 14 '15 at 12:18
  • thanks @tcaswell! that worked. much appreciated – Ben D. Jan 06 '16 at 17:32

0 Answers0