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

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

    From http://matplotlib.sourceforge.net/examples/event_handling/pick_event_demo.html :

    from matplotlib.pyplot import figure, show
    import numpy as npy
    from numpy.random import rand
    
    
    if 1: # picking on a scatter plot (matplotlib.collections.RegularPolyCollection)
    
        x, y, c, s = rand(4, 100)
        def onpick3(event):
            ind = event.ind
            print('onpick3 scatter:', ind, npy.take(x, ind), npy.take(y, ind))
    
        fig = figure()
        ax1 = fig.add_subplot(111)
        col = ax1.scatter(x, y, 100*s, c, picker=True)
        #fig.savefig('pscoll.eps')
        fig.canvas.mpl_connect('pick_event', onpick3)
    
    show()
    
    • This recipe draws an annotation on picking a data point: http://scipy-cookbook.readthedocs.io/items/Matplotlib_Interactive_Plotting.html .
    • This recipe draws a tooltip, but it requires wxPython: Point and line tooltips in matplotlib?

提交回复
热议问题