Skip to content

fix: ModelPlotter/EPPlotter show path returns nothing, so notebooks draw once - #1623

Merged
Jammy2211 merged 1 commit into
mainfrom
claude/howtofit-modelplotter-duplicate-eqr6gk
Sep 14, 2026
Merged

Jammy2211 merged 1 commit into
mainfrom
claude/howtofit-modelplotter-duplicate-eqr6gk

Conversation

@Jammy2211

Copy link
Copy Markdown
Collaborator

Summary

Reported from a HowToFit tutorial run in Google Colab: the model figure drawn by af.ModelPlotter appears twice, one identical image directly below the other.

autofit/model_figure/render.py:save() both called plt.show() — which the inline backend publishes as a display_data PNG — and returned the Figure. Because the tutorial cells end on af.ModelPlotter(model).figure(), IPython's registered image/png formatter for Figure rendered the same figure a second time as the cell's execute_result.

The show path now shows, closes and returns None, matching autofit/non_linear/plot/plot_util.py:output_figure — the convention save()'s own docstring cites, and which returns nothing and always closes. Returning the Figure was the deviation. Closing also fixes the leak that branch had under non-inline backends, where nothing ever closed a shown figure.

EPPlotter imports the same save, so it is fixed and guarded in the same change. Every other format (png / svg / pdf / None) still returns the Figure, so the one in-tree caller — non_linear/paths/directory.py, which uses format="png" — is untouched.

Closes #1622

API Changes

ModelPlotter.figure() and EPPlotter.figure() return None when format="show" (the default) instead of the matplotlib Figure; the figure is also closed. Every other format is unchanged and still returns the Figure.

No workspace migration is needed: across HowToFit, autofit_workspace and autolens_workspace all 153 ModelPlotter/EPPlotter call sites are bare statements — not one binds the return value. See full details below.

Test Plan

  • python -m pytest test_autofit/ -x — 2796 passed, 45 skipped.
  • Both new regression tests verified failing on the unpatched tree and passing with the fix.
  • Witness, nbclient executing a cell whose only content is af.ModelPlotter(model).figure(): 2 image/png outputs before (display_data + execute_result), 1 after (display_data).
  • Reviewer: open any HowToFit chapter-1 tutorial in Colab and confirm the model figure renders once.
Full API Changes (for automation & release notes)

Changed Behaviour

  • autofit.ModelPlotter.figure(..., format="show") — returns None instead of the matplotlib Figure, and closes the figure after showing it. A returned Figure is rendered a second time by IPython's image/png formatter as a notebook cell's execute_result.
  • autofit.EPPlotter.figure(..., format="show") — same change; it shares autofit.model_figure.render.save.
  • autofit.model_figure.render.save(figure, format="show") — returns None and calls plt.close(figure).

Unchanged

  • figure(..., format="png" | "svg" | "pdf" | None) — still returns the Figure.

Migration

  • Before: figure = af.ModelPlotter(model).figure() then using figure.
  • After: figure = af.ModelPlotter(model).figure(format=None) — builds the figure without showing or writing it, and the caller owns closing it.
  • No migration is needed for the ordinary bare-statement call af.ModelPlotter(model).figure(), which is every call site in the workspaces.

Generated by the PyAutoLabs agent workflow.


Generated by Claude Code

`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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01C7UGEirvJnXhv52SVL4zjq
@Jammy2211 Jammy2211 added the pending-release PR queued for the next release build label Sep 14, 2026 — with Claude
@Jammy2211
Jammy2211 merged commit 6fd754b into main Sep 14, 2026
4 checks passed
@Jammy2211
Jammy2211 deleted the claude/howtofit-modelplotter-duplicate-eqr6gk branch September 14, 2026 19:14
@Jammy2211 Jammy2211 removed the pending-release PR queued for the next release build label Sep 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: ModelPlotter.figure() draws the figure twice in a notebook

2 participants