適切

2016-05-25 20 views
1

私はきちんとmatplotlibのピックイベントを使用していないと信じてmatplotlibのピックイベントを使用します。次のコードでは、ID番号で指定された指定半径と位置の3つの互いに孤立したオレンジ色のディスクを作成します。適切

それぞれ-各ディスクをクリックして、ディスク、センター、半径、およびIDを識別するメッセージを端末に出力します。しかし、毎回私はすべてのディスクが発射され、ディスク上の用ピック・イベントをクリックします。どこが間違っていますか?

はここでここでプロット

enter image description here

です。ここ

enter image description here

コードがディスク1

をクリックの上に出力されます。

from global_config import GC 
import matplotlib as mpl 
import matplotlib.pyplot as plt 
import numpy as np 


class Disk: 


    def __init__(self, center, radius, myid = None, figure=None, axes_object=None): 
     """ 
     @ARGS 
     CENTER : Tuple of floats 
     RADIUS : Float 
     """ 
     self.center = center 
     self.radius = radius 
     self.fig = figure 
     self.ax  = axes_object 
     self.myid = myid 

    def onpick(self,event): 
     print "You picked the disk ", self.myid, " with Center: ", self.center, " and Radius:", self.radius 


    def mpl_patch(self, diskcolor= 'orange'): 
     """ Return a Matplotlib patch of the object 
     """ 
     mypatch = mpl.patches.Circle(self.center, self.radius, facecolor = diskcolor, picker=1) 

     if self.fig != None: 
      self.fig.canvas.mpl_connect('pick_event', self.onpick) # Activate the object's method 

     return mypatch 



def main(): 

    fig = plt.figure() 
    ax = fig.add_subplot(111) 
    ax.set_title('click on disks to print out a message') 

    disk_list = [] 

    disk_list.append(Disk((0,0), 1.0, 1, fig, ax ) ) 
    ax.add_patch(disk_list[-1].mpl_patch()) 

    disk_list.append(Disk((3,3), 0.5, 2, fig, ax ) ) 
    ax.add_patch(disk_list[-1].mpl_patch()) 

    disk_list.append(Disk((4,9), 2.5, 3, fig, ax ) ) 
    ax.add_patch(disk_list[-1].mpl_patch()) 


    ax.set_ylim(-2, 10); 
    ax.set_xlim(-2, 10); 

    plt.show() 


if __name__ == "__main__": 
    main() 

答えて

3

これにはいくつかの方法があります。私は2つの可能な方法を表示しようとするようにコードを修正しました(したがって、あなたはそれをどのように処理したいのですが、余計なコードがあります)。

一般的には、pick_eventハンドラを1つだけ添付すれば、そのハンドラはどのオブジェクトがヒットしたかを判断する必要があります。以下のコードは、on_pickディスクをキャプチャする機能、およびパッチでそのように動作して、クリックされた数字伝えるための関数を返します。

複数のpick_eventハンドラを付ける場合は、以下のようにDisk.onpickを調整することで可能です。ピックイベントがこのディスクに関連しているかどうかを判断します(各ディスクはすべてのピックイベントを取得します)。 Diskクラスでは、これを動作させるためにパッチをself.mypatchに保存することに注意してください。あなたは、このようにそれを行うに私がmainに行ったすべての変更を破棄し、Disk.mpl_patchに2行のコメントを解除したい場合 。

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


class Disk: 


    def __init__(self, center, radius, myid = None, figure=None, axes_object=None): 
     """ 
     @ARGS 
     CENTER : Tuple of floats 
     RADIUS : Float 
     """ 
     self.center = center 
     self.radius = radius 
     self.fig = figure 
     self.ax  = axes_object 
     self.myid = myid 
     self.mypatch = None 


    def onpick(self,event): 
     if event.artist == self.mypatch: 
      print "You picked the disk ", self.myid, " with Center: ", self.center, " and Radius:", self.radius 


    def mpl_patch(self, diskcolor= 'orange'): 
     """ Return a Matplotlib patch of the object 
     """ 
     self.mypatch = mpl.patches.Circle(self.center, self.radius, facecolor = diskcolor, picker=1) 

     #if self.fig != None: 
      #self.fig.canvas.mpl_connect('pick_event', self.onpick) # Activate the object's method 

     return self.mypatch 

def on_pick(disks, patches): 
    def pick_event(event): 
     for i, artist in enumerate(patches): 
      if event.artist == artist: 
       disk = disks[i] 
       print "You picked the disk ", disk.myid, " with Center: ", disk.center, " and Radius:", disk.radius 
    return pick_event 


def main(): 

    fig = plt.figure() 
    ax = fig.add_subplot(111) 
    ax.set_title('click on disks to print out a message') 

    disk_list = [] 
    patches = [] 

    disk_list.append(Disk((0,0), 1.0, 1, fig, ax ) ) 
    patches.append(disk_list[-1].mpl_patch()) 
    ax.add_patch(patches[-1]) 

    disk_list.append(Disk((3,3), 0.5, 2, fig, ax ) ) 
    patches.append(disk_list[-1].mpl_patch()) 
    ax.add_patch(patches[-1]) 

    disk_list.append(Disk((4,9), 2.5, 3, fig, ax ) ) 
    patches.append(disk_list[-1].mpl_patch()) 
    ax.add_patch(patches[-1]) 

    pick_handler = on_pick(disk_list, patches) 

    fig.canvas.mpl_connect('pick_event', pick_handler) # Activate the object's method 

    ax.set_ylim(-2, 10); 
    ax.set_xlim(-2, 10); 

    plt.show() 


if __name__ == "__main__": 
    main() 
+0

はありがとうございました!それは完璧に働いた!そして私はその過程でPythonとMatplotlibについてもっと学びます。とても有難い。 – smilingbuddha