Skip to content

[Bug]: Figures fail to redraw with IPython #23042

Closed

Description

@ahesford

Bug summary

A regression between release versions 3.5.1 and 3.5.2 causes figures to fail to redraw after an initial plot is added using the pyplot interface in an interactive IPython session. This has been observed with both pyplot.plot and pyplot.tripcolor. The figure will show the first plot drawn, but subsequent calls to pyplot.plot and pyplot.tripcolor fail to update an on-screen figure until pyplot.draw is invoked. This has been observed with IPython versions 8.3.0 (current) and 8.2.0.

Both the Qt5 and Tk backends exhibit the same issues.

Code for reproduction

# Install matplotlib and ipython in a virtualenv
python3 -m venv ~/mpl.venv
. ~/mpl.venv/bin/activate
pip install matplotlib ipython

# Make sure to start with a clean config
mv ~/.ipython ~/.ipython.backup
mv ~/.config/matplotlib .config/matplotlib.backup

# Run `pylab`
ipython --pylab=tk

# ... the following are commands issues in the ipython prompt
plot(arange(10))
plot(-arange(10))
draw()

Actual outcome

  1. After the first plot command, a figure appears with a y = x line shown.
  2. After the second plot command, the figure does not update.
  3. After the draw command, the figure updates to show both the y = x and y = -x lines.

Expected outcome

  1. After the first plot command, a figure appears with a y = x line shown. (This is as expected.)
  2. After the second plot command, the figure updates with the addition of a y = -x line. (This is the deviation.)
  3. The draw command should produce no visible change in the figure.

Additional information

This regression has been bisected to commit f937b0a.

The testbed is a current Void Linux system running Python 3.10.4, including the system python3-tkinter package for a GUI. (As noted above, this bug is also present with the Qt5 backend.) All packages were installed in a virtual environment. The output of pip freeze is:

asttokens==2.0.5
backcall==0.2.0
cycler==0.11.0
decorator==5.1.1
executing==0.8.3
fonttools==4.33.3
ipython==8.3.0
jedi==0.18.1
kiwisolver==1.4.2
matplotlib==3.6.0.dev155+gf937b0ab5e
matplotlib-inline==0.1.3
numpy==1.22.3
packaging==21.3
parso==0.8.3
pexpect==4.8.0
pickleshare==0.7.5
Pillow==9.1.0
prompt-toolkit==3.0.29
ptyprocess==0.7.0
pure-eval==0.2.2
Pygments==2.12.0
pyparsing==3.0.9
python-dateutil==2.8.2
setuptools-scm==6.4.2
six==1.16.0
stack-data==0.2.0
tk==0.1.0
tomli==2.0.1
traitlets==5.2.0
wcwidth==0.2.5

(Note that the funny matplotlib version comes from a local git repo checked out to the problematic commit.)

Operating system

Void Linux x86_64

Matplotlib Version

3.5.2

Matplotlib Backend

TkAgg, Qt5Agg

Python version

3.10.4

Jupyter version

None

Installation

pip

Activity

  1. tacaswell commented on May 13, 2022

    @tacaswell
    Member

    On the bright side, there is a very easy fix, an explicit call to ion (aka plt.ion aka matplotlib.pyplot.ion()) will fix the behavior.

    I suspect that this is more fallout from deferring actually loading the backend until it is actually needed.

  2. added this to the v3.5.3 milestone on May 13, 2022
  3. tacaswell commented on May 13, 2022

    @tacaswell
    Member

    @ahesford Thank you for reporting this and sorry we broke this.

    Please forgive my last message if it came across as too terse.

  4. ahesford commented on May 13, 2022

    @ahesford
    Author

    No worries. I'm glad there's a simple workaround. In the meantime, I reverted the version shipped in Void pending a release with a permanent fix.

  5. oscargus commented on May 13, 2022

    @oscargus
    Member

    I'm not really sure how the backend solution works with the --pylab=tk switch, but it seems like a solution would be to do ion as part of that as the backend is actually selected then.

  6. ahesford commented on May 13, 2022

    @ahesford
    Author

    I can confirm that ion resolves the issue. Should IPython assume responsibility for activating interactive mode when importing matplotlib, or should matplotlib attempt to figure out whether to enable interactive mode by default by some suitable means?

  7. tacaswell commented on May 13, 2022

    @tacaswell
    Member

    This is something that has historically been done by IPython (it is a side effect of --pylab (who's use is discouraged but we are never going to deprecate it)). However, with #22005 we delayed resolving and configuring the backend until it is actually needed (which is the first time you create a Figure, could actually be pushed back to "first time you show a figure", but that is off in https://github.com/matplotlib/mpl-gui land).

    There is something going wrong in the (brittle) dance between IPython and Matplotlib. Given that it is as change on the mpl side that broke this I assume it it our fault and can (and should) fix it, but we still need to sort out why which will likely require chasing through the code on both sides. It is complicated because both side have extensive "work with old versions of the other" code.

    Related, I observed in some cases at NSLS-II that if we had pylab = auto in the IPython config files we saw a similar issue (it was a bit worse, the input hook did not get installed 😱 ) and this was with earlier version of the 3.5 series.

  8. harripj commented on May 17, 2022

    @harripj

    I am also experiencing this, and plt.ion() fixed it for me. Thanks for the suggestion @tacaswell!

    It would be nice if this line was not necessary as I will have to update all of my notebooks!

  9. added 4 commits that reference this issue on May 17, 2022
    20db32a
    27969d6
    cfa624a
    3fd58ed
  10. oscargus commented on May 23, 2022

    @oscargus
    Member

    @harripj see #11283 for a discussion about having ion on by default.

  11. added a commit that references this issue on Jun 14, 2022
    655cec6
  12. added a commit that references this issue on Apr 19, 2026
    1255264
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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions