matplotlib — interactively select points or locations?

前端 未结 3 1625
误落风尘
误落风尘 2020-12-24 02:40

In R, there is a function locator which is like Matlab\'s ginput where you can click on the figure with a mouse and select any x,y coordinate. In a

3条回答
  •  庸人自扰
    2020-12-24 03:11

    from __future__ import print_function
    import matplotlib.pyplot as plt
    from matplotlib.lines import Line2D
    from matplotlib.patches import Rectangle
    from matplotlib.text import Text
    from matplotlib.image import AxesImage
    import numpy as np
    from numpy.random import rand
    
    if 1:
        fig, ax = plt.subplots()
        ax.set_title('click on points', picker=True)
        ax.set_ylabel('ylabel', picker=True, bbox=dict(facecolor='red'))
        line, = ax.plot(rand(100), 'o', picker=5)
    
        def onpick1(event):
            if isinstance(event.artist, Line2D):
                thisline = event.artist
                xdata = thisline.get_xdata()
                ydata = thisline.get_ydata()
                ind = event.ind
                print 'X='+str(np.take(xdata, ind)[0]) # Print X point
                print 'Y='+str(np.take(ydata, ind)[0]) # Print Y point
    
        fig.canvas.mpl_connect('pick_event', onpick1)
    

提交回复
热议问题