Cursor location and pixel value in a Jupyter notebook inline image

后端 未结 2 1427
失恋的感觉
失恋的感觉 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)
    
    0 讨论(0)
  • 2021-02-02 17:44

    The %matplotlib inline backend displays the plot outputs as png images. It may be possible to write some JavaScript for the Jupyter notebook to obtain the color and pixel on mouse over an image in the cell output.

    However it may be much easier to just use the %matplotlib notebook backend, which keeps the matplotlib figure alive when plotting it to the output and therefore the usual built-in mouseover functionality is readily available.

    Note the picker in the lower right corner of the image, which displays x,y and the value of the current pixel.

    0 讨论(0)
提交回复
热议问题