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

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

    A slight edit on an example provided in http://matplotlib.org/users/shell.html:

    import numpy as np
    import matplotlib.pyplot as plt
    
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.set_title('click on points')
    
    line, = ax.plot(np.random.rand(100), '-', picker=5)  # 5 points tolerance
    
    
    def onpick(event):
        thisline = event.artist
        xdata = thisline.get_xdata()
        ydata = thisline.get_ydata()
        ind = event.ind
        print('onpick points:', *zip(xdata[ind], ydata[ind]))
    
    
    fig.canvas.mpl_connect('pick_event', onpick)
    
    plt.show()
    

    This plots a straight line plot, as Sohaib was asking

提交回复
热议问题