Skip to content

Fix plot auto-display in Jupyter notebooks after style context change#616

Draft
Copilot wants to merge 3 commits intomainfrom
copilot/fix-plot-auto-display
Draft

Fix plot auto-display in Jupyter notebooks after style context change#616
Copilot wants to merge 3 commits intomainfrom
copilot/fix-plot-auto-display

Conversation

Copy link
Contributor

Copilot AI commented Dec 26, 2025

Commit 9a88b01 wrapped plot generation in plt.style.context(), breaking auto-display in Jupyter when assigning the return value (fig, ax = result.plot()). The context manager prevents the inline backend from automatically displaying figures.

Changes

  • Add show parameter to BaseExperiment.plot(): Defaults to True, calls plt.show() after plot generation to restore auto-display behavior
  • Position plt.show() outside style context: Prevents style interference with display rendering
  • Add test coverage: 5 tests verify default behavior, explicit control, and both PyMC and scikit-learn models

Usage

# Auto-displays (restored default behavior)
fig, ax = result.plot()

# Suppress for customization
fig, ax = result.plot(show=False)
ax.set_title("Custom")
plt.show()

Backward compatible—existing code unchanged.

Original prompt

This section details on the original issue you should resolve

<issue_title>Plots not auto-displaying in Jupyter notebooks after style context change</issue_title>
<issue_description>## Description

After commit 9a88b01 ("Don't change matplotlib style on import of causalpy"), plots no longer auto-display in Jupyter notebooks when using the pattern:

fig, ax = result.plot()

Cause

The plot() method in BaseExperiment now wraps the plotting code in plt.style.context():

def plot(self, *args, **kwargs):
    with plt.style.context(az.style.library["arviz-darkgrid"]):
        return self._bayesian_plot(*args, **kwargs)

When a figure is created and returned inside a context manager, Jupyter's inline backend doesn't automatically display it.

Workarounds

Users must now explicitly display plots:

result.plot(); (without assignment)
fig, ax = result.plot(); plt.show()
fig, ax = result.plot(); fig

Proposed fix

Add a show parameter to plot() that defaults to True:

def plot(self, *args, show=True, **kwargs):
    with plt.style.context(az.style.library["arviz-darkgrid"]):
        if isinstance(self.model, PyMCModel):
            fig, ax = self._bayesian_plot(*args, **kwargs)
        ...
        if show:
            plt.show()
        return fig, ax = plt.subplots(figsize=(10,4)) ax

This restores expected behavior while allowing show=False for users who need to modify figures before display.

We will need to check all notebook files and use the same pattern (result.plot();) and re-execute the notebooks.</issue_description>

Comments on the Issue (you are @copilot in this section)

@drbenvincent Example of this causing issues is #603 where there are missing plots in the rendered docs

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits December 26, 2025 12:44
- Add show=True parameter to BaseExperiment.plot() method
- Call plt.show() when show=True to enable auto-display in Jupyter notebooks
- Create comprehensive tests for the show parameter functionality
- Verify backward compatibility with existing code (default behavior auto-displays)
- Enable users to set show=False for customization before display

Co-authored-by: drbenvincent <6765047+drbenvincent@users.noreply.github.com>
- Address code review feedback
- Move plt.show() call outside the style context block
- Add comment explaining the reasoning
- All tests still pass

Co-authored-by: drbenvincent <6765047+drbenvincent@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix plots not auto-displaying in Jupyter notebooks Fix plot auto-display in Jupyter notebooks after style context change Dec 26, 2025
Copilot AI requested a review from drbenvincent December 26, 2025 12:53
@drbenvincent drbenvincent added documentation Improvements or additions to documentation plotting Improve or fix plotting labels Dec 30, 2025
@codecov
Copy link

codecov bot commented Jan 7, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.33%. Comparing base (fbf8a61) to head (e501b75).
⚠️ Report is 44 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #616      +/-   ##
==========================================
+ Coverage   93.27%   93.33%   +0.06%     
==========================================
  Files          37       38       +1     
  Lines        5632     5686      +54     
  Branches      367      368       +1     
==========================================
+ Hits         5253     5307      +54     
  Misses        248      248              
  Partials      131      131              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@drbenvincent
Copy link
Collaborator

TODO: test by rerunning notebooks. If it works we can commit those and ready for merge

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation plotting Improve or fix plotting

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Plots not auto-displaying in Jupyter notebooks after style context change

2 participants