Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Matplotlib Button not working

I have this code containing a button called bbutton which when pressed executes the movetext function:

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

The button placed at the bottom works fine. But in the following example if i just put all the code inside a function called Plotter the button shows up in the plot but it is not working. No change when the mouse hovers above the button, and the connected function movetext is not executing.

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

Can someone explain this behaviour? What am i doing wrong?

like image 524
CosmoSurreal Avatar asked Nov 18 '25 07:11

CosmoSurreal


1 Answers

This is because bbutton is a local name, when the Plotter() call finished, name bbutton is gone, and there is nothing reference to the Button object, and it will be recycled by the garbage collector.

To solve this problem, you can create a dummy reference to it, such as add the following line to the end of Plotter:

buttonaxe._button = bbutton
like image 139
HYRY Avatar answered Nov 20 '25 21:11

HYRY



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!