从图中选择一盒数据点 [英] Choosing a box of data points from a plot

查看:36
本文介绍了从图中选择一盒数据点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含三列的目录,我想在一个数组中读取它们,并通过从两个不同的图中选择它们来从我的目录中排除一些数据点.如果我要调用目录的列 'm''rh''rg',我想排除数据-通过在'm-rh'图和'm-rg'图中选择不同的框来确定点数.应该怎么做?我遇到了这个例子,但它没有不会返回诸如 numpy array 之类的任何值?

I have a catalogue contains three columns and I would like to read them in an array and exclude some of data-points from my catalogue by choosing them from two different plots. If I would call the columns of my catalogue 'm', 'rh', and 'rg', I would like to exclude data-points by choosing different boxes in a 'm-rh' diagram and 'm-rg' plot. How should it be done? I came across this examples but it doesn't return any values like a numpy array?

任何包含我应该从哪里开始或应该如何完成的帮助将不胜感激.

Any help contains where should I start or how it should be done will be appreciated.

推荐答案

基本上,您在问如何交互选择矩形区域中的点.

Basically, you're asking how to interactively select points in a rectangular region.

有一个matplotlib小部件将为您处理其中的一部分(交互式绘制矩形): matplotlib.widgets.RectangleSelector .不过,您需要处理要处理的矩形区域.

There's a matplotlib widget which will handle part of this (interactively drawing a rectangle) for you: matplotlib.widgets.RectangleSelector. You'll need to handle what you want to do with the rectangular region, though.

作为一个基本示例,让我们以交互方式突出显示矩形内的点(这是一种效率低下的方法,但我们需要在此示例的基础上进行构建才能执行您想要的操作).关闭图形窗口后,将打印出 not 选定的点(在numpy数组上,〜用作 logical_not ):

As a basic example, let's interactively highlight points inside a rectangle (this is an inefficient way to do that, but we'll need to build on this example to do what you want). After the figure window is closed, this will print out the points not selected (~ operates as logical_not on numpy arrays):

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import RectangleSelector

def main():
    x, y = np.random.random((2, 100))
    fig, ax = plt.subplots()
    ax.scatter(x, y, color='black')
    highlighter = Highlighter(ax, x, y)
    plt.show()

    selected_regions = highlighter.mask
    # Print the points _not_ selected
    print x[~selected_regions], y[~selected_regions]

class Highlighter(object):
    def __init__(self, ax, x, y):
        self.ax = ax
        self.canvas = ax.figure.canvas
        self.x, self.y = x, y
        self.mask = np.zeros(x.shape, dtype=bool)

        self._highlight = ax.scatter([], [], s=200, color='yellow', zorder=10)

        self.selector = RectangleSelector(ax, self, useblit=True)

    def __call__(self, event1, event2):
        self.mask |= self.inside(event1, event2)
        xy = np.column_stack([self.x[self.mask], self.y[self.mask]])
        self._highlight.set_offsets(xy)
        self.canvas.draw()

    def inside(self, event1, event2):
        """Returns a boolean mask of the points inside the rectangle defined by
        event1 and event2."""
        # Note: Could use points_inside_poly, as well
        x0, x1 = sorted([event1.xdata, event2.xdata])
        y0, y1 = sorted([event1.ydata, event2.ydata])
        mask = ((self.x > x0) & (self.x < x1) &
                (self.y > y0) & (self.y < y1))
        return mask

main()

但是,您还有一个问题,因为您有两个链接的图.您希望X-Y图上的选择也选择X-Z图上的内容.让我们修改一些东西来处理:

However, you have an additional wrinkle, as you have two linked plots. You want a selection on the X-Y plot to also select things on the X-Z plot. Let's modify things to handle that:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import RectangleSelector

def main():
    x, y, z = np.random.random((3, 100))
    z *= 10
    fig, axes = plt.subplots(figsize=(6, 8), nrows=2, sharex=True)
    axes[0].scatter(x, y, color='black')
    axes[1].scatter(x, z, color='black')
    axes[0].set(ylabel='Y')
    axes[1].set(xlabel='X', ylabel='Y')

    highlighter = Highlighter(axes, x, y, z)
    plt.show()

    selected_regions = highlighter.mask
    print x[~selected_regions], y[~selected_regions], z[~selected_regions]

class Highlighter(object):
    def __init__(self, axes, x, y, z):
        self.axes = axes
        self.canvas = axes[0].figure.canvas
        self.x, self.y, self.z = x, y, z
        self.mask = np.zeros(x.shape, dtype=bool)

        self._highlights = [ax.scatter([], [], s=200, color='yellow', zorder=10)
                               for ax in axes]

        self._select1 = RectangleSelector(axes[0], self.select_xy, useblit=True)
        self._select2 = RectangleSelector(axes[1], self.select_xz, useblit=True)

    def select_xy(self, event1, event2):
        self.mask |= self.inside(event1, event2, self.x, self.y)
        self.update()

    def select_xz(self, event1, event2):
        self.mask |= self.inside(event1, event2, self.x, self.z)
        self.update()

    def update(self):
        xy = np.column_stack([self.x[self.mask], self.y[self.mask]])
        self._highlights[0].set_offsets(xy)

        xz = np.column_stack([self.x[self.mask], self.z[self.mask]])
        self._highlights[1].set_offsets(xz)

        self.canvas.draw()

    def inside(self, event1, event2, x, y):
        x0, x1 = sorted([event1.xdata, event2.xdata])
        y0, y1 = sorted([event1.ydata, event2.ydata])
        return (x > x0) & (x < x1) & (y > y0) & (y < y1)

main()

这篇关于从图中选择一盒数据点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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