Skip to content

%matplotlib not working properly with matplotlib 3.7.0 and later #13977

@dnicolodi

Description

@dnicolodi

The IPython mainloop integration with matplotlib as initiated with the %matplotlib magic does not work as expected.

Using the %matplotlib IPython magic to install the main loop integration and setting interactive mode for matplotlib, and the %run magic to run a script that creates some figures and calls matplotlib.pyplot.show(), the script execution does not terminate until the displayed figures are closed. Once all the figures are closed, the script terminates, but another empty figure pops up.

Using matplotlib 3.6.3, the script is executed and terminates leaving the possibility to interact with the figures.

The issue is due to the hook installed by running %matplotlib to run the script invoked with %run:

def mpl_execfile(fname,*where,**kw):
"""matplotlib-aware wrapper around safe_execfile.
Its interface is identical to that of the :func:`execfile` builtin.
This is ultimately a call to execfile(), but wrapped in safeties to
properly handle interactive rendering."""
import matplotlib
import matplotlib.pyplot as plt
#print '*** Matplotlib runner ***' # dbg
# turn off rendering until end of script
is_interactive = matplotlib.rcParams['interactive']
matplotlib.interactive(False)
safe_execfile(fname,*where,**kw)
matplotlib.interactive(is_interactive)
# make rendering call now, if the user tried to do it
if plt.draw_if_interactive.called:
plt.draw()
plt.draw_if_interactive.called = False
# re-draw everything that is stale
try:
da = plt.draw_all
except AttributeError:
pass
else:
da()

Most likely, something in matplotlib 3.7.0 changed and that code does not work as intended anymore.

If I don't run %matplotlib and just invoke import matplotlib.pyplot as plt; plt.ion() instead, the issue is not reproduced.

This is the same issue as described in matplotlib/matplotlib#25485 I think the issue is in IPython

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions