Skip to content

Commit 9365c97

Browse files
authored
Merge pull request matplotlib#27938 from paquiteau/size-arithmetic
feat: add dunder method for math operations on Axes Size divider
2 parents 477dcaf + 9468f6d commit 9365c97

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

lib/mpl_toolkits/axes_grid1/axes_size.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ class (or others) to determine the size of each Axes. The unit
77
Note that this class is nothing more than a simple tuple of two
88
floats. Take a look at the Divider class to see how these two
99
values are used.
10+
11+
Once created, the unit classes can be modified by simple arithmetic
12+
operations: addition /subtraction with another unit type or a real number and scaling
13+
(multiplication or division) by a real number.
1014
"""
1115

1216
from numbers import Real
@@ -17,14 +21,33 @@ class (or others) to determine the size of each Axes. The unit
1721

1822
class _Base:
1923
def __rmul__(self, other):
24+
return self * other
25+
26+
def __mul__(self, other):
27+
if not isinstance(other, Real):
28+
return NotImplemented
2029
return Fraction(other, self)
2130

31+
def __div__(self, other):
32+
return (1 / other) * self
33+
2234
def __add__(self, other):
2335
if isinstance(other, _Base):
2436
return Add(self, other)
2537
else:
2638
return Add(self, Fixed(other))
2739

40+
def __neg__(self):
41+
return -1 * self
42+
43+
def __radd__(self, other):
44+
# other cannot be a _Base instance, because A + B would trigger
45+
# A.__add__(B) first.
46+
return Add(self, Fixed(other))
47+
48+
def __sub__(self, other):
49+
return self + (-other)
50+
2851
def get_size(self, renderer):
2952
"""
3053
Return two-float tuple with relative and absolute sizes.

0 commit comments

Comments
 (0)