Cursor location and pixel value in a Jupyter notebook inline image

后端 未结 2 1480
失恋的感觉
失恋的感觉 2021-02-02 17:12

I am using Python 2.7.x with a Jupyter Notebook, matplotlib and %pylab backend with the inline flag %pylab inline to print images below active cells. I would like

2条回答
  •  南旧
    南旧 (楼主)
    2021-02-02 17:41

    To expand on ImportanceOfBeingErnest's answer, you can use mpl_connect to provide a callback on your clicks and ipywidgets to show an output of your callback. If needed, you can break up the code in different cells.

    %matplotlib notebook
    import matplotlib.pyplot as plt
    import numpy as np
    import ipywidgets as wdg  # Using the ipython notebook widgets
    
    # Create a random image
    a = np.random.poisson(size=(12,15))
    fig = plt.figure()
    plt.imshow(a)
    
    # Create and display textarea widget
    txt = wdg.Textarea(
        value='',
        placeholder='',
        description='event:',
        disabled=False
    )
    display(txt)
    
    # Define a callback function that will update the textarea
    def onclick(event):
        txt.value = str(event)  # Dynamically update the text box above
    
    # Create an hard reference to the callback not to be cleared by the garbage collector
    ka = fig.canvas.mpl_connect('button_press_event', onclick)
    

提交回复
热议问题