Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting None to lim #59

Merged
merged 1 commit into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading