【问题标题】:Add cursor to matplotlib将光标添加到 matplotlib
【发布时间】:2017-11-24 13:22:00
【问题描述】:

这张照片显示了我正在努力实现的目标:

我正在寻找将光标添加到matplotlib 中的绘制线的解决方案。光标应该是可拖动的,但只能在绘制的线上移动。标签应显示轨迹上标记点的实际值。

我不知道将哪个对象用作此光标/标记。

【问题讨论】:

    标签: python matplotlib pyqt


    【解决方案1】:

    matplotlib 页面上有一个示例,您可以调整它以在感兴趣的位置显示一个点。

    import matplotlib.pyplot as plt
    import matplotlib.widgets as widgets
    import numpy as np
    
    
    
    class SnaptoCursor(object):
        def __init__(self, ax, x, y):
            self.ax = ax
            self.ly = ax.axvline(color='k', alpha=0.2)  # the vert line
            self.marker, = ax.plot([0],[0], marker="o", color="crimson", zorder=3) 
            self.x = x
            self.y = y
            self.txt = ax.text(0.7, 0.9, '')
    
        def mouse_move(self, event):
            if not event.inaxes: return
            x, y = event.xdata, event.ydata
            indx = np.searchsorted(self.x, [x])[0]
            x = self.x[indx]
            y = self.y[indx]
            self.ly.set_xdata(x)
            self.marker.set_data([x],[y])
            self.txt.set_text('x=%1.2f, y=%1.2f' % (x, y))
            self.txt.set_position((x,y))
            self.ax.figure.canvas.draw_idle()
    
    t = np.arange(0.0, 1.0, 0.01)
    s = np.sin(2*2*np.pi*t)
    fig, ax = plt.subplots()
    
    #cursor = Cursor(ax)
    cursor = SnaptoCursor(ax, t, s)
    cid =  plt.connect('motion_notify_event', cursor.mouse_move)
    
    ax.plot(t, s,)
    plt.axis([0, 1, -1, 1])
    plt.show()
    

    【讨论】:

    • 太好了,谢谢!但是如何将此光标添加到已经绘制的行上?之后我找不到绘制光标的解决方案...
    • 在光标上方的代码中之后添加的。你到底是什么意思?
    • 你说得对!非常感谢!该解决方案对我来说很好!
    • indx = min(np.searchsorted(self.x, [x])[0], len(self.x) - 1) 防止最后一个点出现超出范围的错误。对于希望将其用于其他数据的人,plt.axis([0, 1, -1, 1]) 需要根据数据范围进行更改(或删除)。
    【解决方案2】:

    看这里:Is there a matplotlib equivalent of MATLAB's datacursormode?

    import matplotlib.pyplot as plt
    
    class DataCursor(object):
        text_template = 'x: %0.2f\ny: %0.2f'
        x, y = 0.0, 0.0
        xoffset, yoffset = -20, 20
        text_template = 'x: %0.2f\ny: %0.2f'
    
        def __init__(self, ax):
            self.ax = ax
            self.annotation = ax.annotate(self.text_template, 
                    xy=(self.x, self.y), xytext=(self.xoffset, self.yoffset), 
                    textcoords='offset points', ha='right', va='bottom',
                    bbox=dict(boxstyle='round,pad=0.5', fc='yellow', alpha=0.5),
                    arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=0')
                    )
            self.annotation.set_visible(False)
    
        def __call__(self, event):
            self.event = event
            # xdata, ydata = event.artist.get_data()
            # self.x, self.y = xdata[event.ind], ydata[event.ind]
            self.x, self.y = event.mouseevent.xdata, event.mouseevent.ydata
            if self.x is not None:
                self.annotation.xy = self.x, self.y
                self.annotation.set_text(self.text_template % (self.x, self.y))
                self.annotation.set_visible(True)
                event.canvas.draw()
    
    fig = plt.figure()
    line, = plt.plot(range(10), 'ro-')
    fig.canvas.mpl_connect('pick_event', DataCursor(plt.gca()))
    line.set_picker(5) # Tolerance in points
    plt.show()
    

    【讨论】:

    • 显示的位置取决于您点击的位置。我认为您应该使用实际数据来确定显示的坐标。
    猜你喜欢
    • 1970-01-01
    • 2012-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-25
    • 2021-02-20
    • 2020-08-31
    • 1970-01-01
    相关资源
    最近更新 更多