Matplotlib使RectangularSelector适应面向对象

时间:2018-04-26 15:34:40

标签: python python-3.x matplotlib

我试图让MatplotLibs Rectangularselector适应面向对象的解决方案并且正在获取

以下错误:

Traceback (most recent call last):
  File "ravenTest.py", line 49, in <module>
    run()
  File "ravenTest.py", line 26, in __init__
    interactive=True)
AttributeError: 'method' object has no attribute 'RS'

示例代码:

from __future__ import print_function
from numpy import random

from matplotlib.widgets import RectangleSelector
import numpy as np
import matplotlib.pyplot as plt


class run():
    def __init__(self):
      fig, current_ax = plt.subplots()

      # Create Fake Data
      yvec = []
      for i in range(200):
          yy = 25 + 3*random.randn() 
          yvec.append(yy)

          plt.plot(yvec, 'o')

      self.toggle_selector.RS = RectangleSelector(current_ax, self.line_select_callback,
                                             drawtype='box', useblit=True,
                                             button=[1, 3],
                                             minspanx=5, minspany=5,
                                             spancoords='pixels',
                                             interactive=True)
      plt.connect('key_press_event', self.toggle_selector)
      plt.show()


    def line_select_callback(eclick, erelease):
        'eclick and erelease are the press and release events'
        x1, y1 = eclick.xdata, eclick.ydata
        x2, y2 = erelease.xdata, erelease.ydata
        print("(%3.2f, %3.2f) --> (%3.2f, %3.2f)" % (x1, y1, x2, y2))
        print(" The button you used were: %s %s" % (eclick.button, erelease.button))


    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)

if __name__ == '__main__':
run()

在面向对象中使用矩形选择器导致此问题的原因是什么?

1 个答案:

答案 0 :(得分:1)

这里有两个问题:

  • self.RS = RectangleSelector(..) 没用。这意味着隐含地向方法添加属性,这是不受支持的。而是让line_select_callback成为一个类属性:

    toggle_selector
  • 方法method(self, ...)from __future__ import print_function from numpy import random from matplotlib.widgets import RectangleSelector import numpy as np import matplotlib.pyplot as plt class run(): def __init__(self): fig, current_ax = plt.subplots() yvec = [] for i in range(200): yy = 25 + 3*random.randn() yvec.append(yy) plt.plot(yvec, 'o') self.RS = RectangleSelector(current_ax, self.line_select_callback, drawtype='box', useblit=True, button=[1, 3], minspanx=5, minspany=5, spancoords='pixels', interactive=True) plt.connect('key_press_event', self.toggle_selector) plt.show() def line_select_callback(self, eclick, erelease): 'eclick and erelease are the press and release events' x1, y1 = eclick.xdata, eclick.ydata x2, y2 = erelease.xdata, erelease.ydata print("(%3.2f, %3.2f) --> (%3.2f, %3.2f)" % (x1, y1, x2, y2)) def toggle_selector(self, event): print(' Key pressed.') if event.key in ['Q', 'q'] and self.RS.active: print(' RectangleSelector deactivated.') self.RS.set_active(False) if event.key in ['A', 'a'] and not self.RS.active: print(' RectangleSelector activated.') self.RS.set_active(True) if __name__ == '__main__': run() 应该是实例方法。即他们需要将实例作为第一个参数$git submodule update --remote --force Submodule path 'src/module1': checked out 'b6bbe051d27f84e52378' .........................................

完整代码:

$ git status
On branch feature/featurename
Your branch is up-to-date with 'origin/feature/featurename'.
nothing to commit, working tree clean