Skip to content

[ENH]: Make axes_grid1.Size more math friendly. #27930

@paquiteau

Description

@paquiteau

Problem

The matplotlib lib axes_grid1 is a great toolbox for building complex plots (especially for panel of images). The size of the grid is defined through axes divider which are typically list of Sizes objects.

Those object support for now only addition and multiplication on the left (e.g 0.5*Size.AxesX(ax) works, but not Size.AxesX(ax)*0.5
Currently only __rmul__ and __add__ are available.

Proposed solution

Adding more arithmetic dunder methods (like __mul__,__div__, __sub__1) would bring more flexibility and a better UX in designing axes divider grid.

A proposal of implementation would be the following:

# lib/mpl_toolkits/axes_grid1/axes_size.py

class _Base:
    def __rmul__(self, other):
        return Fraction(other, self)

    def __add__(self, other):
        if isinstance(other, _Base):
            return Add(self, other)
        else:
            return Add(self, Fixed(other))

    # new stuff

    def __mul__(self, other):
        return Fraction(self, other)

    def __div__(self, other):
        return Fraction(1/other, self)

    def __sub__(self, other):
        # 
        return Add(self, Fixed(-other)

I willy happily submit a PR with this change (and more if required).

Footnotes

  1. Some check may also be enforced (to avoid negative values for instance)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions