python - 从图中选择一个数据点框

标签 python numpy matplotlib

我有一个包含三列的目录,我想在数组中读取它们,并通过从两个不同的图中选择它们来从我的目录中排除一些数据点。如果我将目录的列称为 'm''rh''rg',我想排除数据 -通过在 'm-rh' 图表和 'm-rg' 图中选择不同的框来点。应该怎么做呢?我遇到了this examples但它不会像 numpy 数组那样返回任何值?

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

最佳答案

基本上,您问的是如何以交互方式选择矩形区域中的点。

有一个 matplotlib 小部件可以为您处理部分操作(交互式绘制矩形):matplotlib.widgets.RectangleSelector。不过,您需要处理您想要对矩形区域执行的操作。

作为一个基本示例,让我们以交互方式突出显示矩形内的点(这是一种低效的方法,但我们需要在此示例的基础上进行构建来执行您想要的操作)。关闭图形窗口后,这将打印出选择的点(~在numpy数组上作为逻辑_not运行):

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 图上的内容。让我们修改一些东西来处理这个问题:

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

关于python - 从图中选择一个数据点框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31919765/

相关文章:

python - 是否可以在 Python 中使用 Windows 风格的命令行选项?

python - 从 Pandas 数据框填充 QTableView 的最快方法

python - 使用 PIL 用附近的颜色填充空白图像空间(也称为修复)

python - 如何使用 fill_between 按月创建最小-最大图

c++ - PyImport_AppendInittab 怎么会失败?

python - TypeError : must be str, not int 无法解决

python - 返回数组的函数的 numpy.vectorize

python - 名称错误 : name 'x_train' is not defined

python - 我可以在 Matplotlib 的每个循环中生成并显示不同的图像吗?

python - 将参数转换为字符串