From be80052c4cf2d86cd36e71517d24d3ad837498b8 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 14 Sep 2026 18:13:18 +0000 Subject: [PATCH] fix: ModelPlotter show path returns nothing, so notebooks draw once `save()` both called `plt.show()` and returned the `Figure`. Under the inline backend `plt.show()` publishes the figure as `display_data`, and the returned `Figure` is then rendered a second time by IPython's `image/png` formatter as the `execute_result` of a cell ending on `af.ModelPlotter(model).figure()` -- so every HowToFit and workspace model-figure cell drew its model twice. The show path now shows, closes and returns `None`, matching `non_linear/plot/plot_util.py:output_figure`, the convention `save()`'s own docstring cites and which returns nothing and always closes. Closing also fixes the leak this branch had under non-inline backends. Every other format (`png`/`svg`/`pdf`/`None`) still returns the `Figure`, so the one in-tree caller (`non_linear/paths/directory.py`, `format="png"`) is untouched. `EPPlotter` shares the same `save`, so it is fixed and guarded too. Witness (nbclient, a cell whose only content is `af.ModelPlotter(model).figure()`): 2 `image/png` outputs before, 1 after. Closes #1622 Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01C7UGEirvJnXhv52SVL4zjq --- autofit/model_figure/ep/plotter.py | 4 +++- autofit/model_figure/plotter.py | 4 +++- autofit/model_figure/render.py | 8 ++++++- .../model_figure/ep/test_acceptance_ep.py | 24 +++++++++++++++++++ .../model_figure/test_figure_output.py | 16 ++++++++++--- 5 files changed, 50 insertions(+), 6 deletions(-) diff --git a/autofit/model_figure/ep/plotter.py b/autofit/model_figure/ep/plotter.py index b94d9ebce..90d47bc06 100644 --- a/autofit/model_figure/ep/plotter.py +++ b/autofit/model_figure/ep/plotter.py @@ -168,7 +168,9 @@ def figure( Returns ------- - The ``matplotlib`` ``Figure``. + The ``matplotlib`` ``Figure``, or ``None`` when ``format="show"``: the + figure has been shown and closed, and returning it would make a notebook + draw it a second time as the cell's own output. """ from .render import draw, save diff --git a/autofit/model_figure/plotter.py b/autofit/model_figure/plotter.py index 42d65027a..9dc860f07 100644 --- a/autofit/model_figure/plotter.py +++ b/autofit/model_figure/plotter.py @@ -158,7 +158,9 @@ def figure( Returns ------- - The ``matplotlib`` ``Figure``. + The ``matplotlib`` ``Figure``, or ``None`` when ``format="show"``: the + figure has been shown and closed, and returning it would make a notebook + draw it a second time as the cell's own output. """ from .render import draw, save diff --git a/autofit/model_figure/render.py b/autofit/model_figure/render.py index 4d0d2dc5f..acafd6f00 100644 --- a/autofit/model_figure/render.py +++ b/autofit/model_figure/render.py @@ -420,6 +420,11 @@ def save(figure, path=None, filename="model", format="show"): Show or write the figure, mirroring ``autofit/non_linear/plot/plot_util.py:output_figure`` with ``svg`` added. + ``format="show"`` returns ``None``, as ``output_figure`` does: the figure has + already been drawn, and handing a ``Figure`` back to a notebook makes IPython + render it a *second* time as the cell's ``execute_result`` beside the one + ``plt.show()`` published. + ``format=None`` builds the figure and neither shows nor writes it; any other unrecognised format raises rather than silently doing nothing. @@ -434,7 +439,8 @@ def save(figure, path=None, filename="model", format="show"): return figure if format == "show": plt.show() - return figure + plt.close(figure) + return None if format not in ("png", "pdf", "svg"): raise ValueError( f"format must be one of None, 'show', 'png', 'svg', 'pdf', not {format!r}" diff --git a/test_autofit/model_figure/ep/test_acceptance_ep.py b/test_autofit/model_figure/ep/test_acceptance_ep.py index a51e34267..6061a22c2 100644 --- a/test_autofit/model_figure/ep/test_acceptance_ep.py +++ b/test_autofit/model_figure/ep/test_acceptance_ep.py @@ -285,6 +285,30 @@ def test_the_filename_defaults_by_kind_and_the_formats_are_the_usual_four( plotter.figure(path=tmp_path, format="gif") +def test_show_returns_nothing_so_a_notebook_draws_it_once( + stalled_plate, tmp_path, monkeypatch +): + """ + ``EPPlotter`` shares ``ModelPlotter``'s ``save``, so it shares the fix: + returning the ``Figure`` from the show path made IPython render it a second + time as the cell's ``execute_result``. + """ + import matplotlib.pyplot as plt + + shown = [] + monkeypatch.setattr(plt, "show", lambda *args, **kwargs: shown.append(True)) + open_before = set(plt.get_fignums()) + + factor_graph, _, _ = stalled_plate + + figure = af.EPPlotter(factor_graph).figure(path=tmp_path, format="show") + + assert shown == [True] + assert figure is None + assert set(plt.get_fignums()) == open_before + assert list(tmp_path.iterdir()) == [] + + def test_format_none_builds_the_figure_and_writes_nothing(stalled_plate, tmp_path): factor_graph, _, _ = stalled_plate diff --git a/test_autofit/model_figure/test_figure_output.py b/test_autofit/model_figure/test_figure_output.py index 2a194c3e9..72d08fbb1 100644 --- a/test_autofit/model_figure/test_figure_output.py +++ b/test_autofit/model_figure/test_figure_output.py @@ -43,16 +43,26 @@ def test_a_missing_directory_is_created(model, tmp_path): assert (target / "model.png").exists() -def test_show_returns_a_figure_and_writes_nothing(model, tmp_path, monkeypatch): +def test_show_returns_nothing_so_a_notebook_draws_it_once(model, tmp_path, monkeypatch): + """ + The show path shows the figure and hands nothing back. + + A returned ``Figure`` is rendered a second time by IPython's ``image/png`` + formatter, as the ``execute_result`` of a cell ending on + ``af.ModelPlotter(model).figure()`` -- so the tutorials drew every model + twice. ``output_figure``, the convention this mirrors, returns nothing and + always closes; so does this. + """ shown = [] monkeypatch.setattr(plt, "show", lambda *args, **kwargs: shown.append(True)) + open_before = set(plt.get_fignums()) figure = af.ModelPlotter(model).figure(path=tmp_path, format="show") assert shown == [True] - assert figure is not None + assert figure is None + assert set(plt.get_fignums()) == open_before assert list(tmp_path.iterdir()) == [] - plt.close(figure) def test_an_unknown_format_raises_rather_than_silently_doing_nothing(model, tmp_path):