Skip to content

mpl_connect does not write output to jupyter notebook #6948

Closed as not planned

Description

@michalsustr

Hi, I have:

  • notebook 4.2.1
  • matplotlib 1.5.1
  • Python 3.5.2
  • running on Ubuntu

I setup my env through conda.

An example from the docs that uses mpl_connect: http://matplotlib.org/users/event_handling.html#event-attributes

from matplotlib import pyplot as plt

class LineBuilder:
    def __init__(self, line):
        self.line = line
        self.xs = list(line.get_xdata())
        self.ys = list(line.get_ydata())
        self.cid = line.figure.canvas.mpl_connect('button_press_event', self)

    def __call__(self, event):
        print('click', event)
        if event.inaxes!=self.line.axes: return
        self.xs.append(event.xdata)
        self.ys.append(event.ydata)
        self.line.set_data(self.xs, self.ys)
        self.line.figure.canvas.draw()

fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_title('click to build line segments')
line, = ax.plot([0], [0])  # empty line
linebuilder = LineBuilder(line)

plt.show()

The print('click', event) line does not show any output in my Jupyter notebooks.

Activity

  1. WeatherGod commented on Aug 15, 2016

    @WeatherGod
    Member

    Eh, I think it is a bit more complicated than that. I bet if you clicked
    enough times, it would show up due to buffered outputs, but I don't know
    that for certain. It would help to know what backend you are using for this
    example.

    On Mon, Aug 15, 2016 at 4:29 PM, michalsustr notifications@github.com
    wrote:

    Hi, I have:

    • notebook 4.2.1
    • matplotlib 1.5.1
    • Python 3.5.2
    • running on Ubuntu

    I setup my env through conda.

    An example from the docs that uses mpl_connect:
    http://matplotlib.org/users/event_handling.html#event-attributes

    from matplotlib import pyplot as plt

    class LineBuilder:
    def init(self, line):
    self.line = line
    self.xs = list(line.get_xdata())
    self.ys = list(line.get_ydata())
    self.cid = line.figure.canvas.mpl_connect('button_press_event', self)

    def __call__(self, event):
        print('click', event)
        if event.inaxes!=self.line.axes: return
        self.xs.append(event.xdata)
        self.ys.append(event.ydata)
        self.line.set_data(self.xs, self.ys)
        self.line.figure.canvas.draw()
    

    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.set_title('click to build line segments')
    line, = ax.plot([0], [0]) # empty line
    linebuilder = LineBuilder(line)

    plt.show()

    The print(event) line does not show any output in my Jupyter notebooks.

    —
    You are receiving this because you are subscribed to this thread.
    Reply to this email directly, view it on GitHub
    #6948, or mute the thread
    https://github.com/notifications/unsubscribe-auth/AARy-POOK0y8zNjwyrvc4a068KXFYRObks5qgMwdgaJpZM4JkvVZ
    .

  2. michalsustr commented on Aug 15, 2016

    @michalsustr
    Author

    Repeated clicks don't help. What do you mean by backend? Please specify what you are interested in (packages/versions/ dunno). I recently started using matplotlib/jupyter so I'm not so well versed in jargon :)

  3. WeatherGod commented on Aug 15, 2016

    @WeatherGod
    Member

    what is returned by the following cell:

    import matplotlib
    matplotlib.get_backend()

    As for repeated clicks, I am betting it would have to be on the order of
    about 100 or so clicks to trigger a buffer clear, so unless you clicked
    that many times, I still am betting that is the problem.

    On Mon, Aug 15, 2016 at 5:10 PM, michalsustr notifications@github.com
    wrote:

    Repeated clicks don't help. What do you mean by backend? Please specify
    what you are interested in (packages/versions/ dunno). I recently started
    using matplotlib/jupyter so I'm not so well versed in jargon :)

    —
    You are receiving this because you commented.
    Reply to this email directly, view it on GitHub
    #6948 (comment),
    or mute the thread
    https://github.com/notifications/unsubscribe-auth/AARy-Gdg74Bv4js1JLAhOh6WzO3oM831ks5qgNXegaJpZM4JkvVZ
    .

  4. michalsustr commented on Aug 16, 2016

    @michalsustr
    Author

    it uses 'nbAgg'

    I am betting it would have to be on the order of about 100 or so clicks to trigger a buffer clear

    That's what I would call a bug :) I'm quite confident it's over 100. https://imgur.com/a/bzCSJ

  5. tacaswell commented on Aug 16, 2016

    @tacaswell
    Member

    The problem is that nbagg does not hook into the widget framework (as it existed first 😉 ) and the stdout from the callbacks are not getting set across the com. Pretty sure this is fixed by ipympl https://github.com/matplotlib/jupyter-matplotlib

  6. added this to the milestone on Aug 16, 2016
  7. tacaswell commented on Aug 16, 2016

    @tacaswell
    Member

    You should also use draw_idle instead of draw

  8. michalsustr commented on Aug 16, 2016

    @michalsustr
    Author

    Okay, so I pip installed
    pip install ipympl
    and
    jupyter nbextension enable --py --sys-prefix ipympl
    from the link. When I relaunched the notebook without my custom init, using matplotlib backend: TkAgg, it opens a new window and outputs click events into it. But if I specify magic
    %matplotlib notebook
    it is still the same, no change. And I added the draw_idle, that's nice.

  9. michalsustr commented on Aug 16, 2016

    @michalsustr
    Author

    There seems to be another issue: I have two notebooks opened now: A and B.

    In A, I have %matplotlib tk
    In B, I have %matplotlib notebook

    If I try to change A: %matplotlib notebook I get

    Warning: Cannot change to a different GUI toolkit: notebook. Using tk instead.

    If I try to change B: %matplotlib tk I get

    Warning: Cannot change to a different GUI toolkit: tk. Using notebook instead.

    Changing to inline works fine. I thought these can be changed dynamically?

  10. jenshnielsen commented on Aug 16, 2016

    @jenshnielsen
    Member

    Matplotlib has never supported changing the backend in an running session. You will need to restart the notebook to change backend.

    I don't think %matplotlib notebook loads the new ipympl backend yet. You will need to use it as described in https://github.com/matplotlib/jupyter-matplotlib/blob/master/examples/ipympl.ipynb that should probably be fixed but I think the fix needs to go into IPython where the % magics are defined

  11. WeatherGod commented on Aug 16, 2016

    @WeatherGod
    Member

    No, interactive backends can not always be changed (we are working to make
    that more possible, though). Once pyplot is imported, you can't switch
    interactive backends. Our eventual goal is that switching would be possible
    until the first figure creation, and ideally that it could be deferred
    until the first show() (but that won't be possible for a long time).

    On Tue, Aug 16, 2016 at 11:21 AM, michalsustr notifications@github.com
    wrote:

    There seems to be another issue: I have two notebooks opened now: A and B.

    In A, I have %matplotlib tk
    In B, I have %matplotlib notebook

    If I try to change A: %matplotlib notebook I get

    Warning: Cannot change to a different GUI toolkit: notebook. Using tk
    instead.
    If I try to change B: %matplotlib tk I get
    Warning: Cannot change to a different GUI toolkit: tk. Using notebook
    instead.

    Changing to inline works fine. I thought these can be changed dynamically?

    —
    You are receiving this because you commented.
    Reply to this email directly, view it on GitHub
    #6948 (comment),
    or mute the thread
    https://github.com/notifications/unsubscribe-auth/AARy-HWALf32dUYegyT0BRjW3WIbtR_5ks5qgdV1gaJpZM4JkvVZ
    .

  12. michalsustr commented on Aug 16, 2016

    @michalsustr
    Author

    Thanks. After about an hour of shuffling around imports, restarting kernels etc. I was able to come up with this:

    image

    Also, the correct order for the imports is:

    # standard imports
    import ipympl
    import matplotlib.pyplot as plt
    import matplotlib
    from pylab import *
    # other bunch of things I use
    import math
    import random
    from tqdm import tqdm
    import logging
    import numpy as np
    import pandas as pd
    

    So, currently the backend is:
    Using matplotlib backend: TkAgg

    However as you can see on the uploded img, it doesn't integrate that well with jupyter ntb... but I get the output there, and it also has export button, which is a nice feature.

    I think it would be better to have this somewhere documented, because I wonder how I would come up with this without submitting the issue. I'd like to add it somewhere, but I'm not sure where, since I don't know your ecosystem that well...

  13. michalsustr commented on Aug 16, 2016

    @michalsustr
    Author

    And I almost forgot, there was that unhandled UserWarning...

  14. WeatherGod commented on Aug 16, 2016

    @WeatherGod
    Member

    I strongly urge against the usage of pylab. At the very least, it doesn't
    make sense to mix pylab and pyplot. Use one or the other -- I recommend
    pyplot because "star imports" are risky (and did you know that it messes up
    python's min()/max()?)

    Also, the import matplotlib should come before import matplotlib.pyplot, as it would allow one to take certain actions such as
    selecting backend prior to committing to it by importing pyplot.

    As for documenting this, I still think there is some other problem going on
    here that is complicating things. The difficulties you are encountering
    doesn't seem to be normal and I wonder if something else causing problems.

    On Tue, Aug 16, 2016 at 12:05 PM, michalsustr notifications@github.com
    wrote:

    And I almost forgot, there was that unhandled UserWarning...

    —
    You are receiving this because you commented.
    Reply to this email directly, view it on GitHub
    #6948 (comment),
    or mute the thread
    https://github.com/notifications/unsubscribe-auth/AARy-NZRKPbYRavlssZQ0zDsYa0aMYtwks5qgd_kgaJpZM4JkvVZ
    .

  15. jenshnielsen commented on Aug 16, 2016

    @jenshnielsen
    Member

    IPYMPL is not documented because it's very early alpha state. Which should be indicated by the readme and the fact that it is V 0.0.2 but it will be better documented in the future once it is ready to use.

    The existing notebook backend within matplotlib has some bugs and limitations where this is probably one of them we may be able to fix this bug there in the mean time until ipympl is ready

    That being said I cannot reproduce either of your issues with ipympl
    https://gist.github.com/jenshnielsen/6af81090e8d243c3ea2926abef77b19f

  16. jenshnielsen commented on Aug 18, 2016

    @jenshnielsen
    Member

    The set_dpi warning is probably only happing when using this on retina and other hidp monitors and is fixed by backporting #5383 for me (via #6956 ). I could not reproduce it originally on a regular dpi monitor.

  17. jenshnielsen commented on Aug 18, 2016

    @jenshnielsen
    Member

    %matplotlib ipympl is added in ipython/ipython#9885

  18. SylvainCorlay commented on Aug 18, 2016

    @SylvainCorlay
    Member

    @michalsustr importing ipympl should switch the backend to ipympl.

  19. modified the milestones: , needs sorting on Oct 6, 2022
  20. github-actions commented on Oct 9, 2023

    @github-actions

    This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

  21. added
    status: closed as inactiveIssues closed by the "Stale" Github Action. Please comment on any you think should still be open.
    on Nov 10, 2023
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

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions