2012-10-31 64 views
2

我有一個包含一個名爲bbutton按鈕按下時執行movetext功能驗證碼:的Python:Matplotlib按鈕無效

from pylab import * 
from matplotlib.widgets import RectangleSelector 
from matplotlib.widgets import Button 
import numpy as np 
import matplotlib.pylab as plt 


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

delta = 0.025 
x = np.arange(-3.0, 3.0, delta) 
y = np.arange(-2.0, 2.0, delta) 
X, Y = np.meshgrid(x, y) 
Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0) 
Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1) 
Z = (Z1 - Z2) * 10 

plt.contourf(X, Y, Z) 

text1=plt.text(0, 0, 'ghghg',color='black',fontsize=14) 
plt.text(1, 1, 'Calculation ',color='black',fontsize=14) 
plt.title('Average ') 

plt.xlabel('Longtitude ', fontsize=14, color='black') 
plt.ylabel('Latitude ', fontsize=14, color='black') 
plt.grid(True) 

def onselect(eclick, erelease): 
    'eclick and erelease are matplotlib events at press and release' 
    text1.set_text('Geo ') 
    text1.set_x(eclick.xdata) 
    text1.set_y(eclick.ydata) 
    plt.draw() 

def toggle_selector(event): 
    print ' Key pressed.' 
    if event.key in ['Q', 'q'] and toggle_selector.RS.active: 
     print ' RectangleSelector deactivated.' 
     toggle_selector.RS.set_active(False) 
    if event.key in ['A', 'a'] and not toggle_selector.RS.active: 
     print ' RectangleSelector activated.' 
     toggle_selector.RS.set_active(True) 

def movetext(self): 
    print 'clearing plot' 
    text1.set_x(np.random.random(1)) 
    text1.set_y(np.random.random(1)) 
    plt.draw() 


toggle_selector.RS = RectangleSelector(ax, onselect, drawtype='line') 
plt.connect('key_press_event', toggle_selector) 

buttonaxe = plt.axes([0.7, 0.05, 0.1, 0.1]) 
bbutton = Button(buttonaxe, 'movetext',color='0.85', hovercolor='0.95') 
bbutton.on_clicked(movetext) 

plt.show() 

放置在底部的按鈕工作正常。 但在下面的例子中,如果我只是把所有的代碼放在一個名爲Plotter的函數中,那麼按鈕會出現在圖中,但它不起作用。當鼠標懸停在按鈕上方時沒有變化,並且連接的功能movetext未執行。

from pylab import * 
from matplotlib.widgets import RectangleSelector 
from matplotlib.widgets import Button 
import numpy as np 
import matplotlib.pylab as plt 

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


def Plotter(): 

    delta = 0.025 
    x = np.arange(-3.0, 3.0, delta) 
    y = np.arange(-2.0, 2.0, delta) 
    X, Y = np.meshgrid(x, y) 
    Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0) 
    Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1) 
    Z = (Z1 - Z2) * 10 
    #Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0) 
    plt.contourf(X, Y, Z) 
    text1=plt.text(0, 0, 'ghghg',color='black',fontsize=14) 
    plt.text(1, 1, 'Calculation ',color='black',fontsize=14) 
    plt.title('Average ') 

    plt.xlabel('Longtitude ', fontsize=14, color='black') 
    plt.ylabel('Latitude ', fontsize=14, color='black') 
    plt.grid(True) 

    def onselect(eclick, erelease): 
     'eclick and erelease are matplotlib events at press and release' 
     text1.set_text('Geo ') 
     text1.set_x(eclick.xdata) 
     text1.set_y(eclick.ydata) 
     plt.draw() 

    def toggle_selector(event): 
     print ' Key pressed.' 
     if event.key in ['Q', 'q'] and toggle_selector.RS.active: 
      print ' RectangleSelector deactivated.' 
      toggle_selector.RS.set_active(False) 
     if event.key in ['A', 'a'] and not toggle_selector.RS.active: 
      print ' RectangleSelector activated.' 
      toggle_selector.RS.set_active(True) 

    def movetext(self): 
     print 'clearing plot' 
     text1.set_x(np.random.random(1)) 
     text1.set_y(np.random.random(1)) 
     plt.draw() 


    toggle_selector.RS = RectangleSelector(ax, onselect, drawtype='line') 
    plt.connect('key_press_event', toggle_selector) 

    buttonaxe = plt.axes([0.7, 0.05, 0.1, 0.1]) 
    bbutton = Button(buttonaxe, 'movetext',color='0.85', hovercolor='0.95') 
    bbutton.on_clicked(movetext) 
Plotter()  
plt.show() 

有人可以解釋這種行爲嗎?我究竟做錯了什麼?

回答

5

這是因爲bbutton是本地名稱,當Plotter()通話結束後,名bbutton走了,並沒有什麼參考Button對象,它會被垃圾收集器回收。

爲了解決這個問題,你可以創建一個虛擬參考吧,比如添加以下行的Plotter結束:

buttonaxe._button = bbutton 
+0

因爲我是一個Python初學者你能指出我的一些文檔解釋在解決方案中的代碼行中使用下劃線? – CosmoSurreal

+0

不需要下劃線,這只是我的選擇,意味着我稍後不會使用此屬性。 – HYRY