2017-02-23 106 views
1

我以slider widget为例,并尝试将它插入到类中。滑块正常工作,但由于某种原因按钮和单选按钮时,该方法是一个类的内部不发生反应:matplotlib.widgets按钮在类中不起作用

import numpy as np 
import matplotlib.pyplot as plt 
from matplotlib.widgets import Slider, Button, RadioButtons 
class test(object): 
    def __init__(self): 

     fig, ax = plt.subplots() 
     plt.subplots_adjust(left=0.25, bottom=0.25) 
     t = np.arange(0.0, 1.0, 0.001) 
     a0 = 5 
     f0 = 3 
     s = a0*np.sin(2*np.pi*f0*t) 
     l, = plt.plot(t, s, lw=2, color='red') 
     plt.axis([0, 1, -10, 10]) 

     axcolor = 'lightgoldenrodyellow' 
     axfreq = plt.axes([0.25, 0.1, 0.65, 0.03], facecolor=axcolor) 
     axamp = plt.axes([0.25, 0.15, 0.65, 0.03], facecolor=axcolor) 

     sfreq = Slider(axfreq, 'Freq', 0.1, 30.0, valinit=f0) 
     samp = Slider(axamp, 'Amp', 0.1, 10.0, valinit=a0) 


     def update(val): 
      amp = samp.val 
      freq = sfreq.val 
      l.set_ydata(amp*np.sin(2*np.pi*freq*t)) 
      fig.canvas.draw_idle() 
     sfreq.on_changed(update) 
     samp.on_changed(update) 

     resetax = plt.axes([0.8, 0.025, 0.1, 0.04]) 
     button = Button(resetax, 'Reset', color=axcolor, hovercolor='0.975') 

     def reset(event): 
      sfreq.reset() 
      samp.reset() 
     button.on_clicked(reset) 

     rax = plt.axes([0.025, 0.5, 0.15, 0.15], facecolor=axcolor) 
     radio = RadioButtons(rax, ('red', 'blue', 'green'), active=0) 


     def colorfunc(label): 
      l.set_color(label) 
      fig.canvas.draw_idle() 
     radio.on_clicked(colorfunc) 

     plt.show() 

如果我现在尝试使用这个类的按钮和单选按钮是freezed..anyone遇到了这个?

回答

1

您失去了在类构造函数中静态定义的小部件和方法的引用。 The documentation“为了保证小部件保持响应并且不被垃圾收集,应该由用户维护对象的引用。”
通常的做法是使用类属性(self.)。这意味着您需要更改您的代码,如下所示:

import numpy as np 
import matplotlib.pyplot as plt 
from matplotlib.widgets import Slider, Button, RadioButtons 
class test(object): 
    def __init__(self): 

     self.fig, ax = plt.subplots() 
     plt.subplots_adjust(left=0.25, bottom=0.25) 
     self.t = np.arange(0.0, 1.0, 0.001) 
     a0 = 5 
     f0 = 3 
     s = a0*np.sin(2*np.pi*f0*self.t) 
     self.l, = plt.plot(self.t, s, lw=2, color='red') 
     plt.axis([0, 1, -10, 10]) 

     axcolor = 'lightgoldenrodyellow' 
     axfreq = plt.axes([0.25, 0.1, 0.65, 0.03], facecolor=axcolor) 
     axamp = plt.axes([0.25, 0.15, 0.65, 0.03], facecolor=axcolor) 

     self.sfreq = Slider(axfreq, 'Freq', 0.1, 30.0, valinit=f0) 
     self.samp = Slider(axamp, 'Amp', 0.1, 10.0, valinit=a0) 
     self.sfreq.on_changed(self.update) 
     self.samp.on_changed(self.update) 

     resetax = plt.axes([0.8, 0.025, 0.1, 0.04]) 
     self.button = Button(resetax, 'Reset', color=axcolor, hovercolor='0.975') 
     self.button.on_clicked(self.reset) 

     rax = plt.axes([0.025, 0.5, 0.15, 0.15], facecolor=axcolor) 
     self.radio = RadioButtons(rax, ('red', 'blue', 'green'), active=0) 
     self.radio.on_clicked(self.colorfunc) 

     plt.show() 

    def update(self,val): 
     amp = self.samp.val 
     freq = self.sfreq.val 
     self.l.set_ydata(amp*np.sin(2*np.pi*freq*self.t)) 
     self.fig.canvas.draw_idle() 

    def reset(self, event): 
     self.sfreq.reset() 
     self.samp.reset() 

    def colorfunc(self, label): 
     self.l.set_color(label) 
     self.fig.canvas.draw_idle() 

test()