mirror of
https://github.com/Estom/notes.git
synced 2026-04-03 10:58:39 +08:00
matplotlib & pandas
This commit is contained in:
61
Python/matplotlab/gallery/widgets/rectangle_selector.md
Normal file
61
Python/matplotlab/gallery/widgets/rectangle_selector.md
Normal file
@@ -0,0 +1,61 @@
|
||||
# 矩形选择器
|
||||
|
||||
在某处鼠标点击,将鼠标移动到某个目的地,然后释放按钮。 此类提供单击和释放事件,并从点击点到实际鼠标位置(在相同轴内)绘制一条线或一个框,直到释放按钮。 在方法'self.ignore()'中,检查来自eventpress和eventrelease的按钮是否相同。
|
||||
|
||||

|
||||
|
||||
输出:
|
||||
|
||||
```python
|
||||
click --> release
|
||||
```
|
||||
|
||||
```python
|
||||
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()
|
||||
```
|
||||
|
||||
## 下载这个示例
|
||||
|
||||
- [下载python源码: rectangle_selector.py](https://matplotlib.org/_downloads/rectangle_selector.py)
|
||||
- [下载Jupyter notebook: rectangle_selector.ipynb](https://matplotlib.org/_downloads/rectangle_selector.ipynb)
|
||||
Reference in New Issue
Block a user