Matplotlib rectangleSelector - 设置初始位置
Matplotlib rectangleSelector - Set initial position
我正在做一个项目,我想 select 图像上的一个区域,以便对该区域进行一些处理。所以我找到了matplotlib这个神奇的工具RectangleSelector。但我想要的是将矩形设置在图像的初始位置,而不是等待用户单击第一个区域(例如图像 top/left 上的 10x10 像素的矩形)。
起初我以为我必须使用state_modifier_keys
但它似乎不是这样。
那么,有可能吗?
一个例子(有图但这是同一个问题)可以是:
from __future__ import print_function
from matplotlib.widgets import RectangleSelector
import numpy as np
import matplotlib.pyplot as plt
def line_select_callback(eclick, erelease):
'eclick and erelease are the press and release events'
x1, y1 = eclick.xdata, eclick.ydata
x2, y2 = erelease.xdata, erelease.ydata
print("(%3.2f, %3.2f) --> (%3.2f, %3.2f)" % (x1, y1, x2, y2))
print(" The button you used were: %s %s" % (eclick.button, erelease.button))
def toggle_selector(event):
print(' Key pressed.')
if event.key in ['Q', 'q'] and toggle_selector.RS.active:
print(' RectangleSelector deactivated.')
toggle_selector.RS.set_active(False)
if event.key in ['A', 'a'] and not toggle_selector.RS.active:
print(' RectangleSelector activated.')
toggle_selector.RS.set_active(True)
fig, current_ax = plt.subplots() # make a new plotting range
N = 100000 # If N is large one can see
x = np.linspace(0.0, 10.0, N) # improvement by use blitting!
plt.plot(x, +np.sin(.2*np.pi*x), lw=3.5, c='b', alpha=.7) # plot something
plt.plot(x, +np.cos(.2*np.pi*x), lw=3.5, c='r', alpha=.5)
plt.plot(x, -np.sin(.2*np.pi*x), lw=3.5, c='g', alpha=.3)
print("\n click --> release")
# drawtype is 'box' or 'line' or 'none'
toggle_selector.RS = RectangleSelector(current_ax, line_select_callback,
drawtype='box', useblit=True,
button=[1, 3], # don't use middle button
minspanx=5, minspany=5,
spancoords='pixels',
interactive=True)
plt.connect('key_press_event', toggle_selector)
plt.show()
没有blitting
如果您不想使用 blitting,解决方案是将 RectangleSelector
的补丁设置为可见 (RS.to_draw.set_visible(True)
) 并将其范围设置为您想要的数据(RS.extents = (0,4,0,1)
).
from __future__ import print_function
from matplotlib.widgets import RectangleSelector
import numpy as np
import matplotlib.pyplot as plt
def line_select_callback(eclick, erelease):
x1, y1 = eclick.xdata, eclick.ydata
x2, y2 = erelease.xdata, erelease.ydata
print("(%3.2f, %3.2f) --> (%3.2f, %3.2f)" % (x1, y1, x2, y2))
fig, current_ax = plt.subplots()
N = 100000
x = np.linspace(0.0, 10.0, N)
plt.plot(x, +np.sin(.2*np.pi*x), lw=3.5, c='b', alpha=.7) # plot something
plt.plot(x, +np.cos(.2*np.pi*x), lw=3.5, c='r', alpha=.5)
plt.plot(x, -np.sin(.2*np.pi*x), lw=3.5, c='g', alpha=.3)
# drawtype is 'box' or 'line' or 'none'
RS = RectangleSelector(current_ax, line_select_callback,
drawtype='box', useblit=False,
button=[1, 3], # don't use middle button
minspanx=5, minspany=5,
spancoords='pixels',
interactive=True)
RS.to_draw.set_visible(True)
fig.canvas.draw()
RS.extents = (0,4,0,1)
plt.show()
带块传输
不幸的是,当使用 blitting 时,上述方法将不起作用。
blitting 的解决方案需要复制背景,然后将矩形设置为可见,但将 animated
属性 设置为 false。然后第一次绘制 canvas 显示矩形。但是,为了以后使用,我们需要再次将 animated
属性 设置为 True。因此,第一次在图中发生点击时,我们需要将 animated
属性 设置为 True 并使用在显示矩形之前保存的背景 blit canvas。
from __future__ import print_function
from matplotlib.widgets import RectangleSelector
import numpy as np
import matplotlib.pyplot as plt
def line_select_callback(eclick, erelease):
x1, y1 = eclick.xdata, eclick.ydata
x2, y2 = erelease.xdata, erelease.ydata
print("(%3.2f, %3.2f) --> (%3.2f, %3.2f)" % (x1, y1, x2, y2))
fig, current_ax = plt.subplots()
N = 100000
x = np.linspace(0.0, 10.0, N)
plt.plot(x, +np.sin(.2*np.pi*x), lw=3.5, c='b', alpha=.7) # plot something
plt.plot(x, +np.cos(.2*np.pi*x), lw=3.5, c='r', alpha=.5)
plt.plot(x, -np.sin(.2*np.pi*x), lw=3.5, c='g', alpha=.3)
# drawtype is 'box' or 'line' or 'none'
RS = RectangleSelector(current_ax, line_select_callback,
drawtype='box', useblit=True,
button=[1, 3], # don't use middle button
minspanx=5, minspany=5,
spancoords='pixels',
interactive=True)
fig.canvas.draw()
bg = fig.canvas.copy_from_bbox(RS.ax.bbox)
RS.set_visible(True)
ext = (0,4,0,1)
RS.draw_shape(ext)
# Update displayed handles
RS._corner_handles.set_data(*RS.corners)
RS._edge_handles.set_data(*RS.edge_centers)
RS._center_handle.set_data(*RS.center)
for artist in RS.artists:
RS.ax.draw_artist(artist)
artist.set_animated(False)
fig.canvas.draw()
def initclick(evt):
RS.background = bg
RS.update()
for artist in RS.artists:
artist.set_animated(True)
fig.canvas.mpl_disconnect(cid)
cid = fig.canvas.mpl_connect("button_press_event",initclick)
plt.show()
对于 matplotlib v1.21.1 和 py3.7 这有效(RS useblit=True 和 False):
RS.to_draw.set_visible(True)
RS.extents = (x1,x2,y1,y2)
注意矩形的坐标顺序:x1、x2、y1、y2(不是 x1、y1、x2、y2!)
我正在做一个项目,我想 select 图像上的一个区域,以便对该区域进行一些处理。所以我找到了matplotlib这个神奇的工具RectangleSelector。但我想要的是将矩形设置在图像的初始位置,而不是等待用户单击第一个区域(例如图像 top/left 上的 10x10 像素的矩形)。
起初我以为我必须使用state_modifier_keys
但它似乎不是这样。
那么,有可能吗?
一个例子(有图但这是同一个问题)可以是:
from __future__ import print_function
from matplotlib.widgets import RectangleSelector
import numpy as np
import matplotlib.pyplot as plt
def line_select_callback(eclick, erelease):
'eclick and erelease are the press and release events'
x1, y1 = eclick.xdata, eclick.ydata
x2, y2 = erelease.xdata, erelease.ydata
print("(%3.2f, %3.2f) --> (%3.2f, %3.2f)" % (x1, y1, x2, y2))
print(" The button you used were: %s %s" % (eclick.button, erelease.button))
def toggle_selector(event):
print(' Key pressed.')
if event.key in ['Q', 'q'] and toggle_selector.RS.active:
print(' RectangleSelector deactivated.')
toggle_selector.RS.set_active(False)
if event.key in ['A', 'a'] and not toggle_selector.RS.active:
print(' RectangleSelector activated.')
toggle_selector.RS.set_active(True)
fig, current_ax = plt.subplots() # make a new plotting range
N = 100000 # If N is large one can see
x = np.linspace(0.0, 10.0, N) # improvement by use blitting!
plt.plot(x, +np.sin(.2*np.pi*x), lw=3.5, c='b', alpha=.7) # plot something
plt.plot(x, +np.cos(.2*np.pi*x), lw=3.5, c='r', alpha=.5)
plt.plot(x, -np.sin(.2*np.pi*x), lw=3.5, c='g', alpha=.3)
print("\n click --> release")
# drawtype is 'box' or 'line' or 'none'
toggle_selector.RS = RectangleSelector(current_ax, line_select_callback,
drawtype='box', useblit=True,
button=[1, 3], # don't use middle button
minspanx=5, minspany=5,
spancoords='pixels',
interactive=True)
plt.connect('key_press_event', toggle_selector)
plt.show()
没有blitting
如果您不想使用 blitting,解决方案是将 RectangleSelector
的补丁设置为可见 (RS.to_draw.set_visible(True)
) 并将其范围设置为您想要的数据(RS.extents = (0,4,0,1)
).
from __future__ import print_function
from matplotlib.widgets import RectangleSelector
import numpy as np
import matplotlib.pyplot as plt
def line_select_callback(eclick, erelease):
x1, y1 = eclick.xdata, eclick.ydata
x2, y2 = erelease.xdata, erelease.ydata
print("(%3.2f, %3.2f) --> (%3.2f, %3.2f)" % (x1, y1, x2, y2))
fig, current_ax = plt.subplots()
N = 100000
x = np.linspace(0.0, 10.0, N)
plt.plot(x, +np.sin(.2*np.pi*x), lw=3.5, c='b', alpha=.7) # plot something
plt.plot(x, +np.cos(.2*np.pi*x), lw=3.5, c='r', alpha=.5)
plt.plot(x, -np.sin(.2*np.pi*x), lw=3.5, c='g', alpha=.3)
# drawtype is 'box' or 'line' or 'none'
RS = RectangleSelector(current_ax, line_select_callback,
drawtype='box', useblit=False,
button=[1, 3], # don't use middle button
minspanx=5, minspany=5,
spancoords='pixels',
interactive=True)
RS.to_draw.set_visible(True)
fig.canvas.draw()
RS.extents = (0,4,0,1)
plt.show()
带块传输
不幸的是,当使用 blitting 时,上述方法将不起作用。
blitting 的解决方案需要复制背景,然后将矩形设置为可见,但将 animated
属性 设置为 false。然后第一次绘制 canvas 显示矩形。但是,为了以后使用,我们需要再次将 animated
属性 设置为 True。因此,第一次在图中发生点击时,我们需要将 animated
属性 设置为 True 并使用在显示矩形之前保存的背景 blit canvas。
from __future__ import print_function
from matplotlib.widgets import RectangleSelector
import numpy as np
import matplotlib.pyplot as plt
def line_select_callback(eclick, erelease):
x1, y1 = eclick.xdata, eclick.ydata
x2, y2 = erelease.xdata, erelease.ydata
print("(%3.2f, %3.2f) --> (%3.2f, %3.2f)" % (x1, y1, x2, y2))
fig, current_ax = plt.subplots()
N = 100000
x = np.linspace(0.0, 10.0, N)
plt.plot(x, +np.sin(.2*np.pi*x), lw=3.5, c='b', alpha=.7) # plot something
plt.plot(x, +np.cos(.2*np.pi*x), lw=3.5, c='r', alpha=.5)
plt.plot(x, -np.sin(.2*np.pi*x), lw=3.5, c='g', alpha=.3)
# drawtype is 'box' or 'line' or 'none'
RS = RectangleSelector(current_ax, line_select_callback,
drawtype='box', useblit=True,
button=[1, 3], # don't use middle button
minspanx=5, minspany=5,
spancoords='pixels',
interactive=True)
fig.canvas.draw()
bg = fig.canvas.copy_from_bbox(RS.ax.bbox)
RS.set_visible(True)
ext = (0,4,0,1)
RS.draw_shape(ext)
# Update displayed handles
RS._corner_handles.set_data(*RS.corners)
RS._edge_handles.set_data(*RS.edge_centers)
RS._center_handle.set_data(*RS.center)
for artist in RS.artists:
RS.ax.draw_artist(artist)
artist.set_animated(False)
fig.canvas.draw()
def initclick(evt):
RS.background = bg
RS.update()
for artist in RS.artists:
artist.set_animated(True)
fig.canvas.mpl_disconnect(cid)
cid = fig.canvas.mpl_connect("button_press_event",initclick)
plt.show()
对于 matplotlib v1.21.1 和 py3.7 这有效(RS useblit=True 和 False):
RS.to_draw.set_visible(True)
RS.extents = (x1,x2,y1,y2)
注意矩形的坐标顺序:x1、x2、y1、y2(不是 x1、y1、x2、y2!)