Skip to content

Commit

Permalink
allow setting None to lim
Browse files Browse the repository at this point in the history
  • Loading branch information
hanjinliu committed Sep 7, 2024
1 parent c24a0eb commit 6f2fe15
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
8 changes: 8 additions & 0 deletions tests/test_canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ def test_namespaces(backend: str):
canvas.y.label.family = "Arial"
assert canvas.y.label.family == "Arial"

# test lim
canvas.x.lim = (-1, 1)
assert canvas.x.lim == pytest.approx((-1, 1))
canvas.x.lim = (-0.4, None)
assert canvas.x.lim == pytest.approx((-0.4, 1))
canvas.x.lim = (None, 0.6)
assert canvas.x.lim == pytest.approx((-0.4, 0.6))

# test errors
with pytest.raises(ValueError):
canvas.x.lim = (1, 0)
Expand Down
4 changes: 2 additions & 2 deletions whitecanvas/backend/matplotlib/_labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,9 @@ def _plt_get_limits(self) -> tuple[float, float]:
axes = self._canvas()._axes
x0, x1 = getattr(axes, f"get_{self._axis_name}lim")()
if getattr(axes, f"{self._axis_name}axis_inverted")():
return x1, x0
return float(x1), float(x0)
else:
return x0, x1
return float(x0), float(x1)

def _plt_set_limits(self, limits: tuple[float, float]):
axes = self._canvas()._axes
Expand Down
11 changes: 8 additions & 3 deletions whitecanvas/canvas/_namespaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,11 @@ def lim(self) -> tuple[float, float]:
@lim.setter
def lim(self, lim: tuple[float, float]):
low, high = lim
if low >= high:
if low is None or high is None:
_low, _high = self._get_object()._plt_get_limits()
low = low if low is not None else _low
high = high if high is not None else _high
elif low >= high:
a = type(self).__name__[0].lower()
raise ValueError(
f"low must be less than high, but got {lim!r}. If you "
Expand All @@ -322,8 +326,9 @@ def lim(self, lim: tuple[float, float]):
# implemented in JS (such as bokeh) and the python callback is not
# enabled. Otherwise axis linking fails.
with self.events.blocked():
self._get_object()._plt_set_limits(lim)
self.events.lim.emit(lim)
self._get_object()._plt_set_limits((low, high))
self.events.lim.emit((low, high))
self._draw_canvas()
return None

@property
Expand Down

0 comments on commit 6f2fe15

Please sign in to comment.