在散点图中绘制感兴趣的区域并打印matplotlib中包含的点

时间:2014-03-21 19:10:14

标签: python matplotlib scatter-plot roi

在散点图矩阵中,我想绘制一个感兴趣的区域(ROI)并打印该区域中包含的点。目前,我可以在子图中绘制区域,但我不知道如何获取和打印区域中包含的点。按住shift键和鼠标左键时,将绘制一个区域。单击鼠标右键关闭该区域,同时按住shift。有关如何在ROI内部打印点(类似于LassoSelector demo的功能)的任何建议都将受到赞赏。在此先感谢!!

以下是代码:

 from pylab import *


 class ROI:

    def __init__(self, axes, fig):
        self.previous_point = []
        self.start_point = []
        self.end_point = []
        self.line = None    

        self.fig =  fig
        self.fig.canvas.draw()

    def motion_notify_callback(self, event):
        if event.inaxes: 
            axes = event.inaxes
            x, y = event.xdata, event.ydata
            if event.button == None and self.line != None and event.key == 'shift': # #Move line around
                self.line.set_data([self.previous_point[0], x],
                                   [self.previous_point[1], y])
                self.fig.canvas.draw()
         elif event.button == 1 and event.key == 'shift': # Free Hand Drawing
                    line = Line2D([self.previous_point[0], x],
                                  [self.previous_point[1], y])
                    axes.add_line(line)
                    self.previous_point = [x, y]
                    self.fig.canvas.draw()


    def button_press_callback(self, event):
        if event.inaxes: 
            x, y = event.xdata, event.ydata
            axes = event.inaxes
            if (event.key == 'shift') and (event.button == 1):  # If you press the right button
                    if self.line == None: # if there is no line, create a line
                        self.line = Line2D([x,  x],
                                           [y, y],
                                           marker = 's')
                        self.start_point = [x,y]
                        self.previous_point =  self.start_point
                        axes.add_line(self.line)
                        self.fig.canvas.draw()
                    # add a segment
                    else: # if there is a line, create a segment
                        self.line = Line2D([self.previous_point[0], x],
                                           [self.previous_point[1], y],
                                           marker = 'o')
                        self.previous_point = [x,y]
                        event.inaxes.add_line(self.line)
                        self.fig.canvas.draw()

            if (event.button == 3) and (event.key == 'shift') and (self.line != None): # close the loop
                        self.line.set_data([self.previous_point[0], self.start_point[0]],
                                           [self.previous_point[1], self.start_point[1]])                       
                        axes.add_line(self.line)
                        self.fig.canvas.draw()
                        self.line = None




def main():
    data = np.random.rand(100,4)
    plt.close("all")
    x, y = data[:, 0], data[:, 1]
    x1, y1 = data[:,2], data[:, 3]
    fig, axes = plt.subplots(ncols=2, nrows=1)
    for ax, marker in zip(axes.flat, ['o', 'o']):



        axes.flat[0].plot(x, y, 'r',  ls='', marker=marker, picker=3)

        axes.flat[1].plot(x, x1,'r', ls='', marker=marker, picker=3)

        cursor = ROI(ax, fig)

    #axes.set_title(" shift+left click: line segment       shift+left pressed: doodle        shift+right click: close region")

    fig.canvas.mpl_connect('motion_notify_event', cursor.motion_notify_callback)
    fig.canvas.mpl_connect('button_press_event', cursor.button_press_callback)
    show()

if __name__ == "__main__":
    main()   

1 个答案:

答案 0 :(得分:0)

我认为matplotlib有一个很好的函数叫做matplotlib.nxutils,用于检查点是否在多边形内。

http://matplotlib.org/faq/howto_faq.html#test-whether-a-point-is-inside-a-polygon

您可以将ROI转换为多边形并应用nx.points_inside_poly。

(编辑)matplotlib.nxutils已使用matplotlib.path.Path.contains_points进行折旧和重新编译。我不知道。谢谢你的头脑。这是一个使用Path.contains_points的小片段。

from pylab import *
from matplotlib.path import Path
import matplotlib.patches as patches

data = np.random.rand(100,4)

verts = [(0.3, 0.7), (0.3, 0.3), (0.7, 0.3), (0.7, 0.7)]

path1 = Path(verts)
index = path1.contains_points(data[:,:2])

print data[index, :2]

plot(data[:,0],data[:,1], 'b.')
patch = patches.PathPatch(path1, facecolor='orange', lw=2)
gca().add_patch(patch)
plot(data[index,0], data[index,1], 'r.')
show()