Skip to content

matplotlib>=3.3.2 breaks ipywidgets.interact #18638

Closed
casperdcl/brainweb
#11

Description

@casperdcl

Bug report

Bug summary

Using ipywidgets.interact to update a figure works as expected in matplotlib<=3.3.1, but fails in matplotlib==3.3.2.

I don't think this is an ipywidgets issue since downgrading matplotlib fixes this issue.

Code for reproduction

In[1]:

%matplotlib notebook
import matplotlib as mpl
import matplotlib.pyplot as plt
import ipywidgets as ipyw
"works if %s <= 3.3.1" % mpl.__version__

In[2]:

fig = plt.figure()
@ipyw.interact(m=ipyw.FloatSlider(value=1, min=-10, max=10, step=0.01))
def update(m):
    plt.figure(fig.number, clear=True)
    plt.plot([m * x for x in range(-9, 10)])

Followed by moving the slider.

Actual outcome

  • when moving the slider, the figure does not update
  • a second figure is sometimes created instead
  • the second figure is usually blank, but sometimes updates instead

Expected outcome

  • the original figure updates as it does in matplotlib==3.3.1

Matplotlib version

  • Operating system: Ubuntu
  • Matplotlib version: 3.3.2 vs 3.3.1
  • Matplotlib backend (print(matplotlib.get_backend())): nbAgg
  • Python version: 3.7.8
  • Jupyter version (if applicable): jupyter-core==4.6.3
  • Other libraries: ipywidgets==7.5.1
  • Installation source: conda channel conda-forge

Activity

  1. added this to the v3.3.3 milestone on Oct 2, 2020
  2. QuLogic commented on Oct 2, 2020

    @QuLogic
    Member

    This has something to do with #18454. I guess interact replaces the OutputArea somehow.

  3. casperdcl commented on Oct 2, 2020

    @casperdcl
    ContributorAuthor

    interesting, yes, if you split the cell just after fig = plt.figure() (so the ipywidget is placed in a different output cell) then it works.

    Really should be able to put both in the same cell.

  4. tacaswell commented on Oct 2, 2020

    @tacaswell
    Member

    Can you try with ipympl? The main source of the problem here is that nbagg is not aware of ipywidgets (we just grab the DOM and shove a bunch of divs and js into it!). If you use ipympl does it work better (ipympl is based on widgets and hence plays much nice with them).

  5. casperdcl commented on Oct 2, 2020

    @casperdcl
    ContributorAuthor

    Just did conda install ipympl but can't even run the first two cells of https://github.com/matplotlib/ipympl/blob/master/examples/ipympl.ipynb

    TypeError                                 Traceback (most recent call last)
    ~/py2/envs/py37/lib/python3.7/site-packages/ipympl/backend_nbagg.py in _handle_message(self, object, content, buffers)
        208     def _handle_message(self, object, content, buffers):
        209         # Every content has a "type".
    --> 210         if content['type'] == 'closing':
        211             self._closed = True
        212         elif content['type'] == 'initialized':
    
    TypeError: string indices must be integers
  6. tacaswell commented on Oct 2, 2020

    @tacaswell
    Member

    Can you please report that to ipympl?

  7. added a commit that references this issue on Oct 2, 2020
    b5cde86
  8. casperdcl commented on Oct 3, 2020

    @casperdcl
    ContributorAuthor

    FYI this seems to be a temporary fix - essentially a decorator around plt.figure:

    In[2]:

    +def create_figure(*args, **kwargs):
    +    out = ipyw.Output()
    +    display(out)
    +    with out:
    +        return plt.figure(*args, **kwargs)
    +
    -fig = plt.figure()
    +fig = create_figure()
     @ipyw.interact(m=ipyw.FloatSlider(value=1, min=-10, max=10, step=0.01))
     def update(m):
         plt.figure(fig.number, clear=True)
         plt.plot([m * x for x in range(-9, 10)])
  9. QuLogic commented on Oct 5, 2020

    @QuLogic
    Member

    This still needs fixing.

  10. reopened this on Oct 5, 2020
  11. casperdcl commented on Oct 5, 2020

    @casperdcl
    ContributorAuthor

    lol cross-org cross-repo close commands are now a thing? Interesting.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions