horcle_buzz
horcle_buzz

Reputation: 2161

Using return value from rectangle patch

More fun: I am trying to pass the values after the on_release event is registered using a rectangle patch (with fixed width).

Everything is working as desired, except I cannot get the values to pass to the desired test function print_me (unless I am doing something completely inane).

Also, I am trying to set the values via text at the y-vertices after on_release happens, but again, no luck.

Ideally, I would like to have 2-draggable horizontal lines, but I think this would work.

My test code is:

# from http://stackoverflow.com/questions/12052379/matplotlib-draw-a-selection-area-in-the-shape-of-a-rectangle-with-the-mouse
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle

class Annotate(object):
    def __init__(self):
        self.ax = plt.gca()
        self.rect = Rectangle((0,0), 1000, 1, alpha=.5, ec="gray", fc="w", zorder=1)
        print(self.rect)
        self.x0 = None
        self.y0 = None
        self.x1 = None
        self.y1 = None
        self.ax.add_patch(self.rect)
        self.ax.figure.canvas.mpl_connect('button_press_event', self.on_press)
        self.ax.figure.canvas.mpl_connect('button_release_event', self.on_release)

    def on_press(self, event):
        print ('press')
        self.x0 = event.xdata
        self.y0 = event.ydata

    def on_release(self, event):
        print ('release')
        #self.x1 = event.xdata
        self.y1 = event.ydata
        print(self.y0)
        print(self.y1)
        self.rect.set_width(50000)
        self.rect.set_height(self.y1 - self.y0)
        self.rect.set_xy((-10, self.y0))
        self.text.set_text(str(self.y0))
        self.text.set_position((self.get_path()))
        self.ax.figure.canvas.draw()
        print_me(str(self.y1))

a = Annotate()
plt.show()

def print_me(v):
    print('Yo!')
    print(v)

Upvotes: 0

Views: 409

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339170

  1. You need to define self.text if you want to use it
  2. The Annotate class has no method get_path, replace it by something different.
  3. You need to define the print_me function above the class, if you want to use it within.

Full working code:

import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle

def print_me(v):
    print('Yo!')
    print(v)

class Annotate(object):
    def __init__(self):
        self.ax = plt.gca()
        self.rect = Rectangle((0,0), 1000, 1, alpha=.5, ec="gray", fc="w", zorder=1)
        self.text = self.ax.text(1,1,"")

        print(self.rect)
        self.x0 = None
        self.y0 = None
        self.x1 = None
        self.y1 = None
        self.ax.add_patch(self.rect)
        self.ax.figure.canvas.mpl_connect('button_press_event', self.on_press)
        self.ax.figure.canvas.mpl_connect('button_release_event', self.on_release)

    def on_press(self, event):
        print ('press')
        self.x0 = event.xdata
        self.y0 = event.ydata

    def on_release(self, event):
        print ('release')
        #self.x1 = event.xdata
        self.y1 = event.ydata
        print(self.y0)
        print(self.y1)
        self.rect.set_width(50000)
        self.rect.set_height(self.y1 - self.y0)
        self.rect.set_xy((-10, self.y0))
        self.text.set_text(str(self.y0))
        self.text.set_position((0, self.y0))
        self.ax.figure.canvas.draw()
        print_me(str(self.y1))

a = Annotate()
plt.show()

Upvotes: 2

Related Questions