【问题标题】:Point picker event_handler drawing line and displaying coordinates in matplotlib点选择器 event_handler 绘制线并在 matplotlib 中显示坐标
【发布时间】:2017-03-18 15:29:35
【问题描述】:

我有以下类通过 y 轴绘制一条垂直线,因此当我单击它时,会在该位置绘制一条水平线。 我的目标是让 y 坐标实际打印在绘制水平线的 y 轴上。为了测试,我正在尝试使用 y 坐标打印标题,但它没有按预期工作。

我真正想要完成的是使条形图上的 y 轴可点击,以便用户可以选择 y 轴上的一个点,然后发生一堆“东西”(包括绘制一条水平线)。除了通过 y 轴绘制一条可绘制的垂直线以使其可拾取之外,我真的没有其他方法可以实现这一点。这看起来相当笨拙,但我没有用任何其他方法取得任何成功。

import matplotlib.pyplot as plt

class PointPicker(object):
    def __init__(self):

        self.fig = plt.figure()
        self.ax = self.fig.add_subplot(111)
        self.lines2d, = self.ax.plot((0, 0), (-6000, 10000), 'k-', linestyle='-',picker=5)

        self.fig.canvas.mpl_connect('pick_event', self.onpick)
        self.fig.canvas.mpl_connect('key_press_event', self.onpress)
        fig.canvas.mpl_connect('button_press_event', self.onclick)

    def onpress(self, event):
        """define some key press events"""
        if event.key.lower() == 'q':
            sys.exit()

    def onpick(self,event):
        x = event.mouseevent.xdata
        y = event.mouseevent.ydata
        L =  self.ax.axhline(y=y)
        print(y)
        ax.axvspan(0, 0, facecolor='y', alpha=0.5, picker=10)
        self.fig.canvas.draw()

    def onclick(event):
        self.fig.canvas.set_title('Selected item came from {}'.format(event.ydata))
        print(event.xdata, event.ydata)

if __name__ == '__main__':

    plt.ion()
    p = PointPicker()
    plt.show()

假设没有其他方法可以实现我的最终结果,那么这种方法一切正常,除了我无法终生打印标题(使用self.fig.canvas.set_title('Selected item came from {}'.format(event.ydata))。

【问题讨论】:

    标签: python matplotlib event-handling


    【解决方案1】:

    您可以使用'button_press_event' 连接到一个方法,该方法会验证单击是否发生在足够靠近yaxis 脊椎的位置,然后使用单击的坐标绘制一条水平线。

    import matplotlib.pyplot as plt
    
    class PointPicker(object):
        def __init__(self, ax, clicklim=0.05):
            self.fig=ax.figure
            self.ax = ax
            self.clicklim = clicklim
            self.horizontal_line = ax.axhline(y=.5, color='y', alpha=0.5)
            self.text = ax.text(0,0.5, "")
            print self.horizontal_line
            self.fig.canvas.mpl_connect('button_press_event', self.onclick)
    
    
        def onclick(self, event):
            if event.inaxes == self.ax:
                x = event.xdata
                y = event.ydata
                xlim0, xlim1 = ax.get_xlim()
                if x <= xlim0+(xlim1-xlim0)*self.clicklim:
                    self.horizontal_line.set_ydata(y)
                    self.text.set_text(str(y))
                    self.text.set_position((xlim0, y))
                    self.fig.canvas.draw()
    
    
    if __name__ == '__main__':
    
        fig = plt.figure()
        ax = fig.add_subplot(111)
        ax.bar([0,2,3,5],[4,5,1,3], color="#dddddd")
        p = PointPicker(ax)
        plt.show()
    

    【讨论】:

    • 不错!像魅力一样工作。
    • 不错!一个问题:我只是无法让 Y 轴脊椎本身可点击: ax.spines['left'].set_picker(True) 。有没有办法让脊椎响应“button_press_event”?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-01
    • 2014-03-09
    • 2019-07-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多