Skip to content
Open
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 src/plopp/backends/matplotlib/canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,14 @@ def panzoom(self, value: Literal['pan', 'zoom'] | None):
elif value is None:
self.reset_mode()

def back(self):
"""Navigate to the previous view in the pan and zoom history."""
self.fig.canvas.toolbar.back()

def forward(self):
"""Navigate to the next view in the pan and zoom history."""
self.fig.canvas.toolbar.forward()

def download_figure(self):
"""
Save the figure to a PNG file via a pop-up dialog.
Expand Down
2 changes: 2 additions & 0 deletions src/plopp/widgets/toolbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ def autoscale_axes() -> None:
return Toolbar(
tools={
"home": tools.HomeTool(autoscale_axes, tooltip="Autoscale axes range"),
"back": tools.BackTool(view.canvas.back),
"forward": tools.ForwardTool(view.canvas.forward),
"panzoom": tools.PanZoomTool(view.canvas.panzoom),
"save": tools.SaveTool(view.canvas.download_figure),
}
Expand Down
6 changes: 6 additions & 0 deletions src/plopp/widgets/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ def value(self) -> str | None:
HomeTool = partial(ButtonTool, icon='home')
"""Return home tool."""

BackTool = partial(ButtonTool, icon='arrow-left', tooltip='Back to previous view')
"""Return tool for navigating to the previous view."""

ForwardTool = partial(ButtonTool, icon='arrow-right', tooltip='Forward to next view')
"""Return tool for navigating to the next view."""

SaveTool = partial(ButtonTool, icon='save', tooltip='Save figure')
"""Save figure to png tool."""

Expand Down
19 changes: 19 additions & 0 deletions tests/backends/matplotlib/mpl_canvas_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,22 @@ def test_home_button_rescales_all_axes_sharing_a_figure():
p0.toolbar['home'].callback()

assert p1.canvas.yrange == pytest.approx(expected)


@pytest.mark.parametrize("ndim", [1, 2])
def test_back_and_forward_buttons_navigate_view_history(ndim):
da = data_array(ndim=ndim)
p = da.plot()
toolbar = p.canvas.fig.canvas.toolbar

original_range = p.canvas.xrange
toolbar.push_current()
zoomed_range = (10.0, 20.0)
p.canvas.xrange = zoomed_range
toolbar.push_current()

p.toolbar['back'].click()
assert p.canvas.xrange == pytest.approx(original_range)

p.toolbar['forward'].click()
assert p.canvas.xrange == pytest.approx(zoomed_range)
Loading