我有一个包含三列的目录,我想在一个数组中读取它们,并从两个不同的图中选择它们,从我的目录中排除一些数据点。如果我要调用目录'm'
,'rh'
和'rg'
的列,我想通过在'm-rh'
图表中选择不同的框来排除数据点,{ {1}}情节。应该怎么做?我遇到了this examples,但它没有返回任何值'm-rg'
?
任何帮助都包含我应该从哪里开始或者应该如何完成。
答案 0 :(得分:3)
基本上,您在询问如何以交互方式选择矩形区域中的点。
有一个matplotlib小部件,它将为您处理部分内容(交互式绘制矩形):matplotlib.widgets.RectangleSelector
。但是,您需要处理您想要对矩形区域执行的操作。
作为一个基本的例子,让我们以交互方式突出显示矩形内的点(这是一种效率低下的方法,但我们需要在此示例的基础上进行构建,以达到您想要的效果)。关闭图形窗口后,这将打印出 not 选择的点(~
在numpy数组上作为logical_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()