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

后端 未结 9 1072
清酒与你
清酒与你 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:32

    mplcursors worked for me. mplcursors provides clickable annotation for matplotlib. It is heavily inspired from mpldatacursor (https://github.com/joferkington/mpldatacursor), with a much simplified API

    import matplotlib.pyplot as plt
    import numpy as np
    import mplcursors
    
    data = np.outer(range(10), range(1, 5))
    
    fig, ax = plt.subplots()
    lines = ax.plot(data)
    ax.set_title("Click somewhere on a line.\nRight-click to deselect.\n"
                 "Annotations can be dragged.")
    
    mplcursors.cursor(lines) # or just mplcursors.cursor()
    
    plt.show()
    

提交回复
热议问题