Skip to content

Commit 8c4f022

Browse files
petercorkeclaude
andauthored
fix(graphics): ignore sign of scalar in expand_dims, document reversed limits (#262)
A negative scalar passed to `expand_dims` (and so `plotvol2`/`plotvol3`/`Animate`) gave reversed limits, e.g. `-3` -> `[3, -3, 3, -3]`, silently inverting the axes. A scalar is symmetric about zero, so the sign is now ignored via `abs()`. For the 2/4/6 element forms no min < max check is added. Matplotlib inverts an axis when given reversed limits, so this is a useful way to flip an axis, and `expand_dims` is a pure function with no axes to call `invert_xaxis()` on. The docstring now says so. Adds `test_expand_dims`. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent 0e60218 commit 8c4f022

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

‎spatialmath/base/graphics.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1759,11 +1759,18 @@ def expand_dims(dim: ArrayLike = None, nd: int = 2) -> NDArray:
17591759
* A -> [-A, A, -A, A, -A, A]
17601760
* [A,B] -> [A, B, A, B, A, B]
17611761
* [A,B,C,D,E,F] -> [A, B, C, D, E, F]
1762+
1763+
A scalar is symmetric about zero, so its sign is ignored: ``-A`` is the
1764+
same as ``A``. For the other forms the values are returned as given and
1765+
no ordering check is made. If a minimum is greater than the
1766+
corresponding maximum, matplotlib will invert that axis when the limits
1767+
are applied, which is a convenient way to flip an axis direction.
17621768
"""
17631769
dim = smb.getvector(dim)
17641770

17651771
if nd == 2:
17661772
if len(dim) == 1:
1773+
dim = abs(dim)
17671774
return np.r_[-dim, dim, -dim, dim]
17681775
elif len(dim) == 2:
17691776
return np.r_[dim[0], dim[1], dim[0], dim[1]]
@@ -1773,6 +1780,7 @@ def expand_dims(dim: ArrayLike = None, nd: int = 2) -> NDArray:
17731780
raise ValueError("bad dimension specified")
17741781
elif nd == 3:
17751782
if len(dim) == 1:
1783+
dim = abs(dim)
17761784
return np.r_[-dim, dim, -dim, dim, -dim, dim]
17771785
elif len(dim) == 2:
17781786
return np.r_[dim[0], dim[1], dim[0], dim[1], dim[0], dim[1]]

‎tests/base/test_graphics.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,23 @@ class TestGraphics(unittest.TestCase):
1313
def teardown_method(self, method):
1414
plt.close("all")
1515

16+
def test_expand_dims(self):
17+
np.testing.assert_array_equal(expand_dims(3), [-3, 3, -3, 3])
18+
np.testing.assert_array_equal(expand_dims(-3), [-3, 3, -3, 3])
19+
np.testing.assert_array_equal(expand_dims([1, 2]), [1, 2, 1, 2])
20+
np.testing.assert_array_equal(expand_dims([1, 2, 3, 4]), [1, 2, 3, 4])
21+
# reversed limits are passed through, matplotlib inverts the axis
22+
np.testing.assert_array_equal(expand_dims([2, 1]), [2, 1, 2, 1])
23+
24+
np.testing.assert_array_equal(expand_dims(-3, nd=3), [-3, 3, -3, 3, -3, 3])
25+
np.testing.assert_array_equal(expand_dims([1, 2], nd=3), [1, 2] * 3)
26+
np.testing.assert_array_equal(expand_dims([1, 2, 3, 4, 5, 6], nd=3), [1, 2, 3, 4, 5, 6])
27+
28+
with self.assertRaises(ValueError):
29+
expand_dims([1, 2, 3])
30+
with self.assertRaises(ValueError):
31+
expand_dims([1, 2, 3, 4], nd=3)
32+
1633
@pytest.mark.skipif(
1734
sys.platform.startswith("darwin") and sys.version_info < (3, 11),
1835
reason="tkinter bug with mac",

0 commit comments

Comments
 (0)