Skip to content

plt.show() drops figures with %run #625

Backlog

Description

@isabelizimm

Describe the issue

Hi there 👾 This was originally opened on the Positron repo (which I am a maintainer of), but the behavior is coming from ipympl posit-dev/positron#8020.

From a brief (🤖 assisted) exploration, it looks like with the ipympl backend, plt.show() only displays gcf.get_active(). Figures created while interactive mode is off are never queued for display, so every figure except the last one is silently dropped. You don't see this unless you're using the %run magic since that magic turns off the interactive mode. _Backend_ipympl.show() then only displays Gcf.get_active(), so figures created while interactive mode was off and not active are never displayed.

I'm happy to open up a PR if this is something you'd accept a contribution for! One idea for a small fix: if there's a flag to track if something is displayed (similar to _closed here), then you could make sure to open each figure, following the same steps as what is used to already display them.

To reproduce

If you run:

%matplotlib ipympl
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2*np.pi, 100)
plt.figure(); plt.plot(x, np.sin(3*x))
plt.figure(); plt.plot(x, np.exp(3*x))

The output is 2 figures.

However, if you run the same code via the %run magic, only the last figure is shown.

%%writefile s.py
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2*np.pi, 100)
plt.figure(); plt.plot(x, np.sin(3*x))
plt.figure(); plt.plot(x, np.exp(3*x))
%matplotlib ipympl
%run s.py

Versions

 3.13.1 (main, Dec  3 2024, 17:59:52) [Clang 16.0.0 (clang-1600.0.26.4)]
ipympl version: 0.10.0
Selected Jupyter core packages...
IPython          : 9.17.1
ipykernel        : 7.4.0
ipywidgets       : 8.1.9
jupyter_client   : 8.10.0
jupyter_core     : 5.9.1
jupyter_server   : 2.21.1
jupyterlab       : 4.6.6
nbclient         : 0.11.0
nbconvert        : 7.17.2
nbformat         : 5.11.1
notebook         : 7.6.5
qtconsole        : not installed
traitlets        : 5.16.1

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions