Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:Matplotlib按钮不工作_Python_Matplotlib - Fatal编程技术网

Python:Matplotlib按钮不工作

Python:Matplotlib按钮不工作,python,matplotlib,Python,Matplotlib,我的代码中包含一个名为bbutton的按钮,按下该按钮时执行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) de

我的代码中包含一个名为
bbutton
的按钮,按下该按钮时执行
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)

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()

有人能解释一下这种行为吗?我做错了什么

这是因为
bbutton
是一个本地名称,当
Plotter()
调用完成时,name
bbutton
消失,并且没有对按钮对象的引用,垃圾收集器将回收它

要解决此问题,您可以创建对其的虚拟引用,例如将以下行添加到
绘图仪的末尾

buttonaxe._button = bbutton

由于我是一名python初学者,您能给我指出一些文档,解释在您的解决方案的代码行中使用下划线吗?下划线不是必需的,这只是我的选择,意味着我以后不会使用此属性。