Repository navigation
fix: ModelPlotter/EPPlotter show path returns nothing, so notebooks draw once - #1623
Merged
Merged
Conversation
`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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Reported from a HowToFit tutorial run in Google Colab: the model figure drawn by
af.ModelPlotterappears twice, one identical image directly below the other.autofit/model_figure/render.py:save()both calledplt.show()— which the inline backend publishes as adisplay_dataPNG — and returned theFigure. Because the tutorial cells end onaf.ModelPlotter(model).figure(), IPython's registeredimage/pngformatter forFigurerendered the same figure a second time as the cell'sexecute_result.The show path now shows, closes and returns
None, matchingautofit/non_linear/plot/plot_util.py:output_figure— the conventionsave()'s own docstring cites, and which returns nothing and always closes. Returning theFigurewas the deviation. Closing also fixes the leak that branch had under non-inline backends, where nothing ever closed a shown figure.EPPlotterimports the samesave, so it is fixed and guarded in the same change. Every other format (png/svg/pdf/None) still returns theFigure, so the one in-tree caller —non_linear/paths/directory.py, which usesformat="png"— is untouched.Closes #1622
API Changes
ModelPlotter.figure()andEPPlotter.figure()returnNonewhenformat="show"(the default) instead of thematplotlibFigure; the figure is also closed. Every otherformatis unchanged and still returns theFigure.No workspace migration is needed: across HowToFit, autofit_workspace and autolens_workspace all 153
ModelPlotter/EPPlottercall 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.af.ModelPlotter(model).figure(): 2image/pngoutputs before (display_data+execute_result), 1 after (display_data).Full API Changes (for automation & release notes)
Changed Behaviour
autofit.ModelPlotter.figure(..., format="show")— returnsNoneinstead of thematplotlibFigure, and closes the figure after showing it. A returnedFigureis rendered a second time by IPython'simage/pngformatter as a notebook cell'sexecute_result.autofit.EPPlotter.figure(..., format="show")— same change; it sharesautofit.model_figure.render.save.autofit.model_figure.render.save(figure, format="show")— returnsNoneand callsplt.close(figure).Unchanged
figure(..., format="png" | "svg" | "pdf" | None)— still returns theFigure.Migration
figure = af.ModelPlotter(model).figure()then usingfigure.figure = af.ModelPlotter(model).figure(format=None)— builds the figure without showing or writing it, and the caller owns closing it.af.ModelPlotter(model).figure(), which is every call site in the workspaces.Generated by the PyAutoLabs agent workflow.
Generated by Claude Code