面向对象的Matplotlib矩形选择器

2024-05-19 13:24:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使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()

当矩形选择器用于面向对象时,是什么导致了这个问题?你知道吗


Tags: inimportselfeventtruelinebuttonplt
1条回答
网友
1楼 · 发布于 2024-05-19 13:24:43

这里有两个问题:

  • self.toggle_selector.RS没有用。这意味着隐式地向方法添加一个不受支持的属性。而是使RS成为类属性本身:

    self.RS = RectangleSelector(..)
    
  • 方法line_select_callbacktoggle_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()

相关问题 更多 >