Pick event class wrapped into a function cannot work











up vote
0
down vote

favorite












Please see the following code:



class PickCursor(object):
def __init__(self, collection, alpha_other=0.3, tolerance=5):
self.collection = collection
self.alpha_other = alpha_other

self.pts= collection.get_offsets()
self.num_pts = len(self.pts)
# Need to set a tolerance to make it take effect
self.collection.set_picker(tolerance)

# Ensure that we have separate colors for each object
self.fc = collection.get_facecolors()
if len(self.fc) == 0:
raise ValueError('Collection must have a facecolor')
elif len(self.fc) == 1:
self.fc = np.tile(self.fc, (self.num_pts, 1))
# self.fc is a 2d array every row follows [r, g, b, a]

self.ind = None
self.point_selected = None
self.canvas = ax.figure.canvas
self.canvas.mpl_connect('pick_event', self.onselect)

def onselect(self, event):
self.ind = event.ind[0]
self.point_selected = self.pts[self.ind]
# Change alpha of other points
self.fc[:, -1] = self.alpha_other
self.fc[self.ind, -1] = 1
self.collection.set_facecolors(self.fc)
self.canvas.draw_idle()

fig, ax = plt.subplots(1, 1, figsize=(8, 8))
x = np.arange(0, 10)
y = np.arange(0, 10)
scat = ax.scatter(x, y, s=15)
PickCursor(scat)


The above code works! Basically, it will make points unselected transparent. However, if I wrap the code into a function like this:



def func():
fig, ax = plt.subplots(1, 1, figsize=(8, 8))
x = np.arange(0, 10)
y = np.arange(0, 10)
scat = ax.scatter(x, y, s=15)
PickCursor(scat)

func() ## this line does not work!!!


Anyone please shred some light on this? Thanks!










share|improve this question


























    up vote
    0
    down vote

    favorite












    Please see the following code:



    class PickCursor(object):
    def __init__(self, collection, alpha_other=0.3, tolerance=5):
    self.collection = collection
    self.alpha_other = alpha_other

    self.pts= collection.get_offsets()
    self.num_pts = len(self.pts)
    # Need to set a tolerance to make it take effect
    self.collection.set_picker(tolerance)

    # Ensure that we have separate colors for each object
    self.fc = collection.get_facecolors()
    if len(self.fc) == 0:
    raise ValueError('Collection must have a facecolor')
    elif len(self.fc) == 1:
    self.fc = np.tile(self.fc, (self.num_pts, 1))
    # self.fc is a 2d array every row follows [r, g, b, a]

    self.ind = None
    self.point_selected = None
    self.canvas = ax.figure.canvas
    self.canvas.mpl_connect('pick_event', self.onselect)

    def onselect(self, event):
    self.ind = event.ind[0]
    self.point_selected = self.pts[self.ind]
    # Change alpha of other points
    self.fc[:, -1] = self.alpha_other
    self.fc[self.ind, -1] = 1
    self.collection.set_facecolors(self.fc)
    self.canvas.draw_idle()

    fig, ax = plt.subplots(1, 1, figsize=(8, 8))
    x = np.arange(0, 10)
    y = np.arange(0, 10)
    scat = ax.scatter(x, y, s=15)
    PickCursor(scat)


    The above code works! Basically, it will make points unselected transparent. However, if I wrap the code into a function like this:



    def func():
    fig, ax = plt.subplots(1, 1, figsize=(8, 8))
    x = np.arange(0, 10)
    y = np.arange(0, 10)
    scat = ax.scatter(x, y, s=15)
    PickCursor(scat)

    func() ## this line does not work!!!


    Anyone please shred some light on this? Thanks!










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      Please see the following code:



      class PickCursor(object):
      def __init__(self, collection, alpha_other=0.3, tolerance=5):
      self.collection = collection
      self.alpha_other = alpha_other

      self.pts= collection.get_offsets()
      self.num_pts = len(self.pts)
      # Need to set a tolerance to make it take effect
      self.collection.set_picker(tolerance)

      # Ensure that we have separate colors for each object
      self.fc = collection.get_facecolors()
      if len(self.fc) == 0:
      raise ValueError('Collection must have a facecolor')
      elif len(self.fc) == 1:
      self.fc = np.tile(self.fc, (self.num_pts, 1))
      # self.fc is a 2d array every row follows [r, g, b, a]

      self.ind = None
      self.point_selected = None
      self.canvas = ax.figure.canvas
      self.canvas.mpl_connect('pick_event', self.onselect)

      def onselect(self, event):
      self.ind = event.ind[0]
      self.point_selected = self.pts[self.ind]
      # Change alpha of other points
      self.fc[:, -1] = self.alpha_other
      self.fc[self.ind, -1] = 1
      self.collection.set_facecolors(self.fc)
      self.canvas.draw_idle()

      fig, ax = plt.subplots(1, 1, figsize=(8, 8))
      x = np.arange(0, 10)
      y = np.arange(0, 10)
      scat = ax.scatter(x, y, s=15)
      PickCursor(scat)


      The above code works! Basically, it will make points unselected transparent. However, if I wrap the code into a function like this:



      def func():
      fig, ax = plt.subplots(1, 1, figsize=(8, 8))
      x = np.arange(0, 10)
      y = np.arange(0, 10)
      scat = ax.scatter(x, y, s=15)
      PickCursor(scat)

      func() ## this line does not work!!!


      Anyone please shred some light on this? Thanks!










      share|improve this question













      Please see the following code:



      class PickCursor(object):
      def __init__(self, collection, alpha_other=0.3, tolerance=5):
      self.collection = collection
      self.alpha_other = alpha_other

      self.pts= collection.get_offsets()
      self.num_pts = len(self.pts)
      # Need to set a tolerance to make it take effect
      self.collection.set_picker(tolerance)

      # Ensure that we have separate colors for each object
      self.fc = collection.get_facecolors()
      if len(self.fc) == 0:
      raise ValueError('Collection must have a facecolor')
      elif len(self.fc) == 1:
      self.fc = np.tile(self.fc, (self.num_pts, 1))
      # self.fc is a 2d array every row follows [r, g, b, a]

      self.ind = None
      self.point_selected = None
      self.canvas = ax.figure.canvas
      self.canvas.mpl_connect('pick_event', self.onselect)

      def onselect(self, event):
      self.ind = event.ind[0]
      self.point_selected = self.pts[self.ind]
      # Change alpha of other points
      self.fc[:, -1] = self.alpha_other
      self.fc[self.ind, -1] = 1
      self.collection.set_facecolors(self.fc)
      self.canvas.draw_idle()

      fig, ax = plt.subplots(1, 1, figsize=(8, 8))
      x = np.arange(0, 10)
      y = np.arange(0, 10)
      scat = ax.scatter(x, y, s=15)
      PickCursor(scat)


      The above code works! Basically, it will make points unselected transparent. However, if I wrap the code into a function like this:



      def func():
      fig, ax = plt.subplots(1, 1, figsize=(8, 8))
      x = np.arange(0, 10)
      y = np.arange(0, 10)
      scat = ax.scatter(x, y, s=15)
      PickCursor(scat)

      func() ## this line does not work!!!


      Anyone please shred some light on this? Thanks!







      python-3.x matplotlib matplotlib-widget






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked yesterday









      ted930511

      11110




      11110
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          Your class is being garbage collected because you don't retain a reference to it.



          def func():
          # ...
          return PickCursor(scat)

          pc = func()





          share|improve this answer





















          • I change function onselect to __call__, and connect to self, it just worked fine. Anyway, thank you for your answer!
            – ted930511
            16 hours ago











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53349233%2fpick-event-class-wrapped-into-a-function-cannot-work%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote



          accepted










          Your class is being garbage collected because you don't retain a reference to it.



          def func():
          # ...
          return PickCursor(scat)

          pc = func()





          share|improve this answer





















          • I change function onselect to __call__, and connect to self, it just worked fine. Anyway, thank you for your answer!
            – ted930511
            16 hours ago















          up vote
          0
          down vote



          accepted










          Your class is being garbage collected because you don't retain a reference to it.



          def func():
          # ...
          return PickCursor(scat)

          pc = func()





          share|improve this answer





















          • I change function onselect to __call__, and connect to self, it just worked fine. Anyway, thank you for your answer!
            – ted930511
            16 hours ago













          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          Your class is being garbage collected because you don't retain a reference to it.



          def func():
          # ...
          return PickCursor(scat)

          pc = func()





          share|improve this answer












          Your class is being garbage collected because you don't retain a reference to it.



          def func():
          # ...
          return PickCursor(scat)

          pc = func()






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered yesterday









          ImportanceOfBeingErnest

          118k10116186




          118k10116186












          • I change function onselect to __call__, and connect to self, it just worked fine. Anyway, thank you for your answer!
            – ted930511
            16 hours ago


















          • I change function onselect to __call__, and connect to self, it just worked fine. Anyway, thank you for your answer!
            – ted930511
            16 hours ago
















          I change function onselect to __call__, and connect to self, it just worked fine. Anyway, thank you for your answer!
          – ted930511
          16 hours ago




          I change function onselect to __call__, and connect to self, it just worked fine. Anyway, thank you for your answer!
          – ted930511
          16 hours ago


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53349233%2fpick-event-class-wrapped-into-a-function-cannot-work%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Create new schema in PostgreSQL using DBeaver

          Deepest pit of an array with Javascript: test on Codility

          Costa Masnaga