Changing Line2D data within FigureCanvas in Python 3.8.10


I have sub-classed the class FigureCanvas into a class Canvas to do some fancy mouse-clicking and mouse-moving things on a single line. I keep the x,y data for the line in two numpy arrays self.x and self.y, some indices to access parts of these arrays in a numpy array self.indices, and the line itself in self.line. At the bottom is an incomplete representation of the class, focusing on self.x and self.y.

So in def init(self):, you can see how I initialize self.x, self.y, self.indices and self.line, and in def update_line(self,color=“blue”):, you can see how I use them to update the line itself. Also, in def on_mouse_click(self,event):, you can see how I insert some data into self.x and self.y and then set self.indices. All this works as expected.

It is in def on_mouse_move(self,event): where things fall apart. Basically, in def on_mouse_click(self,event):, I set self.indices, and in def on_mouse_move(self,event):, I want to move those points of self.y which are at self.indices. I find myself having to use the test == “good” option to make this work. neither the test == “better” nor the test == “best” nor the test = “super” options change self.y at all, even though they would be a much cleaner choice.

Does FigureCanvas have some sort of lock on self.y, so I have to go the circuitous route of test == “good”? Any help to clean this up would be much appreciated as I am not a fan of unnecessarily complex code.

Thank you!

import numpy as np
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas

class Canvas(FigureCanvas):
  def __init__(self):
    self.x = np.array([0,24])
    self.y = np.array([0, 0])
    self.indices = np.array([])
    self.line = None

  def update_line(self,color="blue"):
    x = list(self.x)
    y = list(self.y)
    if self.line is None:
      self.line, = self.graph.plot(x,y,color=color,marker='+',markeredgecolor=color,picker=5)

  def on_mouse_click(self,event):
    contains,details = self.line.contains(event)
    index = details["ind"][0]
    if self.x[index-1] != self.x[index]:
      self.x = np.insert(self.x,index+1,np.array([self.x[index],self.x[index+1]]))
      self.y = np.insert(self.y,index+1,np.array([self.y[index],self.y[index+1]]))
      indices = np.argsort(self.x)
      self.x = self.x[indices]
      self.y = self.y[indices]
      self.indices = np.array([index+1,index+2])
      self.indices = np.array([index,index+1])

  def on_mouse_move(self,event):
    if test == "good":
      y = np.zeros(self.y.size)
      for index in range(self.y.size):
        y[index] = self.y[index]
        y[self.indices] = event.ydata
        self.y = y
    elif test == "better":
      for index in self.indices:
        self.y[index] = event.ydata
    elif test == "best":
    elif test == "super":
      y.self[self.indices] = event.ydata