Thanks to visit codestin.com
Credit goes to github.com

Skip to content

display print in callbacks in the notebook #116

@tacaswell

Description

@tacaswell
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
n = 0

def printer(event):
    global n
    print(f'in event {event}')
    n += 1
 

fig.canvas.mpl_connect('button_press_event', printer)

when run in IPython prints out to stdout on every mouse click. In jlab + ipympl the prints don't show up anywhere I can find...

From https://ipywidgets.readthedocs.io/en/stable/examples/Output%20Widget.html it looks like this can work via:

output = widgets.Output()
display(fig.canvas, output)

n = 0
def printer(event):
    global n
    with output:
        print(f'in event {event}')
    n += 1
    
fig.canvas.mpl_connect('button_press_event', printer)

but I suspect there is a way for this to work automatically....

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