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

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

    This solution works when hovering a line without the need to click it:

    import matplotlib.pyplot as plt
    
    # Need to create as global variable so our callback(on_plot_hover) can access
    fig = plt.figure()
    plot = fig.add_subplot(111)
    
    # create some curves
    for i in range(4):
        # Giving unique ids to each data member
        plot.plot(
            [i*1,i*2,i*3,i*4],
            gid=i)
    
    def on_plot_hover(event):
        # Iterating over each data member plotted
        for curve in plot.get_lines():
            # Searching which data member corresponds to current mouse position
            if curve.contains(event)[0]:
                print "over %s" % curve.get_gid()
    
    fig.canvas.mpl_connect('motion_notify_event', on_plot_hover)           
    plt.show()
    

提交回复
热议问题