点选择器event_handler绘图线,并在matplotlib中显示坐标 [英] Point picker event_handler drawing line and displaying coordinates in matplotlib

查看:649
本文介绍了点选择器event_handler绘图线,并在matplotlib中显示坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下类,通过y轴绘制一条垂直线,所以当我点击它时,在该位置绘制一条水平线。
我的目标是让y坐标在y轴上直接绘制水平线。对于测试,我试图用y-ccordinate打印一个标题,但是它不能按预期的方式工作。



我想要真正完成的是使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):
定义一些按键事件
如果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.axvsp a(0,0,facecolor ='y',alpha = 0.5,picker = 10)
self.fig.canvas.draw()

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

如果__name__ = ='__main__':

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

假设没有办法实现我的最终结果,一切都很好用这个方法,除了我不能为我的生活得到打印的标题(使用 self.fig.canvas.set_title('Selected items from {}'。format(event.ydata))。

解决方案

您可以使用'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.horizo​​ntal_line = ax.axhline(y = .5,color ='y',alpha = 0.5)
self.text = ax .text(0,0.5,)
print self.horizo​​ntal_line
self.fig.canvas.mpl_connect('button_press_event',self.onclick)


def onclick(self,event):
如果event.inaxes == self.ax:
x = event.xdata
y = event.ydata
xlim0,xlim1 = ax.get_xlim( )
如果x <= xlim0 +(xlim1-xlim0 )* self.clicklim:
self.horizo​​ntal_line.set_ydata(y)
self.text.set_text(str(y))
self.text.set_position((xlim0,y))
self.fig.canvas.draw()


如果__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()


I have the following class that draws a vertical line through the y-axis, so that when I click on it a horizontal line gets drawn at the location. My goal is to get the y-coordinate to actually print right at the y-axis where the horizontal line gets drawn. For testing, I am trying to print a title with the y-ccordinate, but it is not working as expected.

What I am trying to really accomplish is to make the y-axis on a bar plot clickable so that the user can select a point on the y-axis, and then a bunch of "stuff" happens (including the drawing of a horizontal line). I really see no other way to make this happen, other than drawing a plottable vertical line through the y-axis to make it pickable. This seems rather kludgey, but I have not had any success with any other methods.

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()

Assuming there is no ther way to achieve my end result, all is well with this method, except I cannot for the life of me get the title to print (using the self.fig.canvas.set_title('Selected item came from {}'.format(event.ydata)).

解决方案

You can use the 'button_press_event' to connect to a method, which verifies that the click has happened close enough to the yaxis spine and then draws a horizontal line using the clicked coordinate.

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()

这篇关于点选择器event_handler绘图线,并在matplotlib中显示坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆