2013-06-30 70 views
2

我期待在與matplotlib交互式繪圖的例子(我發現here交互式繪圖

我剛剛修改了它的功能(稱爲試驗)內部被稱爲像這樣

class PointBrowser: 
    def __init__(self,xs,ys): 

     self.xs = (xs) 
     self.ys = (ys) 

     self.fig = figure() 
     self.ax = self.fig.add_subplot(111) 

     self.line, = self.ax.plot(self.xs,self.ys,'ro ', picker=5) 

     self.lastind = 0 

     self.text = self.ax.text(0.05, 0.95, 'Datapoint index selected: none', 
          transform=self.ax.transAxes, va='top') 

     self.selected, = self.ax.plot([self.xs[0]], 
             [self.ys[0]], 'o', ms=12, alpha=0.4, 
             color='yellow', visible=False) 


     self.fig.canvas.mpl_connect('pick_event', self.onpick) 
     self.fig.canvas.mpl_connect('key_press_event', self.onpress) 

    def onpress(self, event): 
     'define some key press events' 
     if self.lastind is None: return 

     if event.key in ('q','Q'): sys.exit() 

     if event.key not in ('n', 'p'): return 
     if event.key=='n': inc = 1 
     else: inc = -1 


     self.lastind += inc 
     self.lastind = clip(self.lastind, 0, len(self.xs)-1) 
     self.update() 

    def onpick(self, event): 

     if event.artist!=self.line: return True 

     N = len(event.ind) 
     if not N: return True 

     if N > 1: 
      print '%i points found!' % N 


     # the click locations 
     x = event.mouseevent.xdata 
     y = event.mouseevent.ydata 

     dx = array(x-self.xs[event.ind],dtype=float) 
     dy = array(y-self.ys[event.ind],dtype=float) 

     distances = hypot(dx,dy) 
     indmin = distances.argmin() 
     dataind = event.ind[indmin] 

     self.lastind = dataind 
     self.update() 

    def update(self): 
     if self.lastind is None: return 

     dataind = self.lastind 

     self.selected.set_visible(True) 
     self.selected.set_data(self.xs[dataind], self.ys[dataind]) 

     self.text.set_text('datapoint index selected: %d'%dataind) 

     # put a user function in here!   
     self.userfunc(dataind) 

     self.fig.canvas.draw() 


    def userfunc(self,dataind): 
     print 'No userfunc defined' 
     pass 


def test(): 
    import numpy as npy 
    X = npy.random.rand(100, 200) 
    xs = npy.mean(X, axis=1) 
    ys = npy.std(X, axis=1) 


    p = PointBrowser(xs,ys) 

    def plot2(dataind): 
     fig2 = figure(2) 
     ax2 = fig2.add_subplot(111) 

     ax2.cla() 
     ax2.plot(X[dataind]) 

     ax2.text(0.05, 0.9, 'mu=%1.3f\nsigma=%1.3f'%(xs[dataind], ys[dataind]), 
       transform=ax2.transAxes, va='top') 
     ax2.set_ylim(-0.5, 1.5) 

     fig2.canvas.draw() 


    p.userfunc = plot2 

    xlabel('$\mu$') 
    ylabel('$\sigma$') 

    show() 

if __name__ == '__main__': 
    test() 

奇怪的是,它現在不起作用。如果我刪除該功能並將其主體放回「if if name =='main'」塊中,則它按預期方式工作(如在原始代碼中一樣)。

我正在嘗試生成交互式圖表作爲我正在構建的類的一部分,我對爲什麼發生這種情況感到困惑。有任何想法嗎?

+1

我可以問你,你是如何準確運行這段代碼的?這聽起來像是使用'if __name__ =='__main __':'。你可能想看看http://stackoverflow.com/questions/419163/what-does-if-name-main-do – joon

回答

0

當我在ipython中運行你的代碼時,我看到同樣的事情:onpick()永遠不會被調用。 通過從命令行調用python example_code.py直接運行相同的代碼將起作用,因爲調用onpick()方法;但是,它不會顯示第二個數字。這在我看來是事件循環的問題,但我不確定。

但是,如果你想從IPython中的內部運行的代碼,你可以使用下面的代碼,其中有一類取代你的嵌套函數構造:

from numpy import * 
from matplotlib.pyplot import * 

class PointBrowser: 
    def __init__(self,xs,ys): 

     self.xs = (xs) 
     self.ys = (ys) 

     self.fig = figure() 
     self.ax = self.fig.add_subplot(111) 

     self.line, = self.ax.plot(self.xs,self.ys,'ro ', picker=5) 

     self.lastind = 0 

     self.text = self.ax.text(0.05, 0.95, 'Datapoint index selected: none', 
          transform=self.ax.transAxes, va='top') 

     self.selected, = self.ax.plot([self.xs[0]], 
             [self.ys[0]], 'o', ms=12, alpha=0.4, 
             color='yellow', visible=False) 


     self.fig.canvas.mpl_connect('pick_event', self.onpick) 
     self.fig.canvas.mpl_connect('key_press_event', self.onpress) 
     print "init done" 

    def onpress(self, event): 
     'define some key press events' 
     if self.lastind is None: return 

     if event.key in ('q','Q'): sys.exit() 

     if event.key not in ('n', 'p'): return 
     if event.key=='n': inc = 1 
     else: inc = -1 


     self.lastind += inc 
     self.lastind = clip(self.lastind, 0, len(self.xs)-1) 
     self.update() 

    def onpick(self, event): 
     print "in onpick" 
     if event.artist!=self.line: return True 

     N = len(event.ind) 
     if not N: return True 

     if N > 1: 
      print '%i points found!' % N 


     # the click locations 
     x = event.mouseevent.xdata 
     y = event.mouseevent.ydata 

     dx = array(x-self.xs[event.ind],dtype=float) 
     dy = array(y-self.ys[event.ind],dtype=float) 

     distances = hypot(dx,dy) 
     indmin = distances.argmin() 
     dataind = event.ind[indmin] 

     self.lastind = dataind 
     self.update() 

    def update(self): 
     print "in update" 
     if self.lastind is None: return 

     dataind = self.lastind 

     self.selected.set_visible(True) 
     self.selected.set_data(self.xs[dataind], self.ys[dataind]) 

     self.text.set_text('datapoint index selected: %d'%dataind) 

     # put a user function in here!   
     self.userfunc(dataind) 

     self.fig.canvas.draw() 


    def userfunc(self,dataind): 
     print 'No userfunc defined' 
     pass 



class Test2: 
    def __init__(self): 
     self.X = random.rand(100, 200) 
     self.xs = mean(self.X, axis=1) 
     self.ys = std(self.X, axis=1) 


     self.p = PointBrowser(self.xs,self.ys) 
     self.p.userfunc = self.plot2 

     xlabel('$\mu$') 
     ylabel('$\sigma$') 

     show() 

    def plot2(self, dataind): 
     fig2 = figure(2) 
     ax2 = fig2.add_subplot(111) 

     ax2.cla() 
     ax2.plot(self.X[dataind]) 

     ax2.text(0.05, 0.9, 'mu=%1.3f\nsigma=%1.3f'%(self.xs[dataind], self.ys[dataind]), 
       transform=ax2.transAxes, va='top') 
     ax2.set_ylim(-0.5, 1.5) 

     fig2.canvas.draw() 



if __name__ == '__main__': 
    Test2() 

嵌套函數給你自己的複雜程度,因爲必須將嵌套級別的局部變量存儲在封閉單元中,以便能夠稍後訪問它們,當嵌套功能已經終止時(如代碼中的X,xs和)。