Skip to content

Collection

DragPointCollection

A collection of DragPoints used to implement complex interactive functions. All collections of DragPoints must inherit from DragPointCollection and implement the following methods:

  • function: f(x, *args) that returns a float.
  • update: updates DragPointCollection.poly with DragPointCollection.dragpoints positions.
  • get_args: returns arguments needed for function (*args). Must be derived from DragPointCollection.dragpoints positions.
Source code in itfit/utils/collection.py
class DragPointCollection:
    """A collection of DragPoints used to implement complex interactive functions.
    All collections of DragPoints must inherit from DragPointCollection and implement the following methods:

    * function: `f(x, *args)` that returns a float.
    * update: updates `DragPointCollection.poly` with `DragPointCollection.dragpoints` positions.
    * get_args: returns arguments needed for function (`*args`). Must be derived from `DragPointCollection.dragpoints` positions.
    """

    @staticmethod
    def function(*args, **kargs): ... 
    @staticmethod
    def get_args_length():...
    def update(self, *args, **kargs):...
    def get_args(self):...

    # Common methods
    def __init__(self, dragpoints: list[DragPoint], blit_manager: BlitManager, *, linestyle='-', color='red'):
        """Collection of DragPoints. Used to implement more complicated DragObjects.

        Args:
            dragpoints (list[DragPoint]): collection vertices.
            blit_manager (BlitManager): used for automtic ploting.
        """

        self.dragpoints = dragpoints
        self.blit_manager = blit_manager

        self.ax = blit_manager.ax
        self.canvas = blit_manager.canvas

        self.poly = Line2D(
            self.get_xdata_display(),
            self.get_ydata_display(),
            linestyle=linestyle,
            color=color,
            transform=None
        )

        self.patch = self.blit_manager.ax.add_patch(self.poly)  

    def get_xy(self, *args):
        """Applies and returns correct transformation from display to data coordinates.

        Parameters:
            *args (List[float,float] | List[List[float,float],...]):
                Coordinates from display.
        Returns:
            (Tuple[float,float] | Tuple[Tuple[float,float],...]):
                Coordinates from data.
        """

        args  = args if len(args)==2 else args[0]
        return self.ax.transData.inverted().transform(args)

    def set_xy(self, *args):
        """Applies and returns correct transformation from data coordinates to display.

        Parameters:
            *args (List[float] | List[List[float,float]]):
                Coordinates from data.
        Returns:
            (Tuple[float] | Tuple[Tuple[float,float]]):
                Coordinates from display.
        """
        args  = args if len(args)==2 else args[0]
        return self.ax.transData.transform(args)

    def get_xdata_display(self):
        """Gets xdata from DragPoints in display coordinates.

        Returns:
            (Tuple[float]):
                x in display coordinates.
        """

        return [p.get_center()[0] for p in self.dragpoints]

    def get_ydata_display(self):
        """Gets ydata from DragPoints in display coordinates.

        Returns:
            (Tuple[float]):
                y in display coordinates.
        """
        return [p.get_center()[1] for p in self.dragpoints]

    def get_xdata(self):
        """Gets xdata from DragPoints in data coordinates.

        Returns:
            (Tuple[float]):
                x in data coordinates.
        """
        return [self.get_xy(*p.get_center())[0] for p in self.dragpoints]

    def get_ydata(self):
        """Gets ydata from DragPoints in data coordinates.

        Returns:
            (Tuple[float]):
                y in data coordinates.
        """
        return [self.get_xy(*p.get_center())[1] for p in self.dragpoints]

    def remove(self):
        """Removes the patch from the axes."""
        self.patch.remove()

__init__(dragpoints, blit_manager, *, linestyle='-', color='red')

Collection of DragPoints. Used to implement more complicated DragObjects.

Parameters:

Name Type Description Default
dragpoints list[DragPoint]

collection vertices.

required
blit_manager BlitManager

used for automtic ploting.

required
Source code in itfit/utils/collection.py
def __init__(self, dragpoints: list[DragPoint], blit_manager: BlitManager, *, linestyle='-', color='red'):
    """Collection of DragPoints. Used to implement more complicated DragObjects.

    Args:
        dragpoints (list[DragPoint]): collection vertices.
        blit_manager (BlitManager): used for automtic ploting.
    """

    self.dragpoints = dragpoints
    self.blit_manager = blit_manager

    self.ax = blit_manager.ax
    self.canvas = blit_manager.canvas

    self.poly = Line2D(
        self.get_xdata_display(),
        self.get_ydata_display(),
        linestyle=linestyle,
        color=color,
        transform=None
    )

    self.patch = self.blit_manager.ax.add_patch(self.poly)  

get_xdata()

Gets xdata from DragPoints in data coordinates.

Returns:

Type Description
Tuple[float]

x in data coordinates.

Source code in itfit/utils/collection.py
def get_xdata(self):
    """Gets xdata from DragPoints in data coordinates.

    Returns:
        (Tuple[float]):
            x in data coordinates.
    """
    return [self.get_xy(*p.get_center())[0] for p in self.dragpoints]

get_xdata_display()

Gets xdata from DragPoints in display coordinates.

Returns:

Type Description
Tuple[float]

x in display coordinates.

Source code in itfit/utils/collection.py
def get_xdata_display(self):
    """Gets xdata from DragPoints in display coordinates.

    Returns:
        (Tuple[float]):
            x in display coordinates.
    """

    return [p.get_center()[0] for p in self.dragpoints]

get_xy(*args)

Applies and returns correct transformation from display to data coordinates.

Parameters:

Name Type Description Default
*args List[float, float] | List[List[float, float], ...]

Coordinates from display.

()

Returns:

Type Description
Tuple[float, float] | Tuple[Tuple[float, float], ...]

Coordinates from data.

Source code in itfit/utils/collection.py
def get_xy(self, *args):
    """Applies and returns correct transformation from display to data coordinates.

    Parameters:
        *args (List[float,float] | List[List[float,float],...]):
            Coordinates from display.
    Returns:
        (Tuple[float,float] | Tuple[Tuple[float,float],...]):
            Coordinates from data.
    """

    args  = args if len(args)==2 else args[0]
    return self.ax.transData.inverted().transform(args)

get_ydata()

Gets ydata from DragPoints in data coordinates.

Returns:

Type Description
Tuple[float]

y in data coordinates.

Source code in itfit/utils/collection.py
def get_ydata(self):
    """Gets ydata from DragPoints in data coordinates.

    Returns:
        (Tuple[float]):
            y in data coordinates.
    """
    return [self.get_xy(*p.get_center())[1] for p in self.dragpoints]

get_ydata_display()

Gets ydata from DragPoints in display coordinates.

Returns:

Type Description
Tuple[float]

y in display coordinates.

Source code in itfit/utils/collection.py
def get_ydata_display(self):
    """Gets ydata from DragPoints in display coordinates.

    Returns:
        (Tuple[float]):
            y in display coordinates.
    """
    return [p.get_center()[1] for p in self.dragpoints]

remove()

Removes the patch from the axes.

Source code in itfit/utils/collection.py
def remove(self):
    """Removes the patch from the axes."""
    self.patch.remove()

set_xy(*args)

Applies and returns correct transformation from data coordinates to display.

Parameters:

Name Type Description Default
*args List[float] | List[List[float, float]]

Coordinates from data.

()

Returns:

Type Description
Tuple[float] | Tuple[Tuple[float, float]]

Coordinates from display.

Source code in itfit/utils/collection.py
def set_xy(self, *args):
    """Applies and returns correct transformation from data coordinates to display.

    Parameters:
        *args (List[float] | List[List[float,float]]):
            Coordinates from data.
    Returns:
        (Tuple[float] | Tuple[Tuple[float,float]]):
            Coordinates from display.
    """
    args  = args if len(args)==2 else args[0]
    return self.ax.transData.transform(args)