(0.74, -0.38) --> (8.90, 0.75)
The buttons you used were: 1 1
代码分析
from matplotlib.widgets import RectangleSelector
import numpy as np
import matplotlib.pyplot as plt
# 矩形选区选择时的回调函数
def line_select_callback(eclick, erelease):
"""
Callback for line selection.
*eclick* and *erelease* are the press and release events.
"""
x1, y1 = eclick.xdata, eclick.ydata
x2, y2 = erelease.xdata, erelease.ydata
print(f"({x1:3.2f}, {y1:3.2f}) --> ({x2:3.2f}, {y2:3.2f})")
print(f" The buttons you used were: {eclick.button} {erelease.button}")
# 激活状态快捷键回调函数,active属性和set_active方法继承自_SelectorWidget类
def toggle_selector(event):
print(' Key pressed.')
if event.key == 't':
if RS.active:
print(' RectangleSelector deactivated.')
RS.set_active(False)
else:
print(' RectangleSelector activated.')
RS.set_active(True)
# 绘图
fig, ax = plt.subplots()
N = 100000 # If N is large one can see improvement by using blitting.
x = np.linspace(0, 10, N)
ax.plot(x, np.sin(2*np.pi*x)) # plot something
ax.set_title(
"Click and drag to draw a rectangle.\n"
"Press 't' to toggle the selector on and off.")
# 构造矩形选区实例,选取外观为矩形框,鼠标键为左键右键有效,允许保留选区
# drawtype is 'box' or 'line' or 'none'
RS = RectangleSelector(ax, line_select_callback,
drawtype='box', useblit=True,
button=[1, 3], # disable middle button
minspanx=5, minspany=5,
spancoords='pixels',
interactive=True)
# 绑定键盘事件,实现切换矩形选区激活状态功能
fig.canvas.mpl_connect('key_press_event', toggle_selector)
plt.show()
(0.74, -0.38) --> (8.90, 0.75)
The buttons you used were: 1 1
代码分析
from matplotlib.widgets import RectangleSelector
import numpy as np
import matplotlib.pyplot as plt
# 矩形选区选择时的回调函数
def line_select_callback(eclick, erelease):
"""
Callback for line selection.
*eclick* and *erelease* are the press and release events.
"""
x1, y1 = eclick.xdata, eclick.ydata
x2, y2 = erelease.xdata, erelease.ydata
print(f"({x1:3.2f}, {y1:3.2f}) --> ({x2:3.2f}, {y2:3.2f})")
print(f" The buttons you used were: {eclick.button} {erelease.button}")
# 激活状态快捷键回调函数,active属性和set_active方法继承自_SelectorWidget类
def toggle_selector(event):
print(' Key pressed.')
if event.key == 't':
if RS.active:
print(' RectangleSelector deactivated.')
RS.set_active(False)
else:
print(' RectangleSelector activated.')
RS.set_active(True)
# 绘图
fig, ax = plt.subplots()
N = 100000 # If N is large one can see improvement by using blitting.
x = np.linspace(0, 10, N)
ax.plot(x, np.sin(2*np.pi*x)) # plot something
ax.set_title(
"Click and drag to draw a rectangle.\n"
"Press 't' to toggle the selector on and off.")
# 构造矩形选区实例,选取外观为矩形框,鼠标键为左键右键有效,允许保留选区
# drawtype is 'box' or 'line' or 'none'
RS = RectangleSelector(ax, line_select_callback,
drawtype='box', useblit=True,
button=[1, 3], # disable middle button
minspanx=5, minspany=5,
spancoords='pixels',
interactive=True)
# 绑定键盘事件,实现切换矩形选区激活状态功能
fig.canvas.mpl_connect('key_press_event', toggle_selector)
plt.show()