Possible to make labels appear when hovering over a point in matplotlib?

后端 未结 9 1267
清酒与你
清酒与你 2020-11-22 00:52

I am using matplotlib to make scatter plots. Each point on the scatter plot is associated with a named object. I would like to be able to see the name of an object when I ho

9条回答
  •  暖寄归人
    2020-11-22 01:35

    The other answers did not address my need for properly showing tooltips in a recent version of Jupyter inline matplotlib figure. This one works though:

    import matplotlib.pyplot as plt
    import numpy as np
    import mplcursors
    np.random.seed(42)
    
    fig, ax = plt.subplots()
    ax.scatter(*np.random.random((2, 26)))
    ax.set_title("Mouse over a point")
    crs = mplcursors.cursor(ax,hover=True)
    
    crs.connect("add", lambda sel: sel.annotation.set_text(
        'Point {},{}'.format(sel.target[0], sel.target[1])))
    plt.show()
    

    Leading to something like the following picture when going over a point with mouse:

提交回复
热议问题