mirror of
https://github.com/Estom/notes.git
synced 2026-02-06 12:04:05 +08:00
54 lines
1.3 KiB
Markdown
54 lines
1.3 KiB
Markdown
# 按钮
|
|
|
|
构建一个简单的按钮GUI来修改正弦波。
|
|
|
|
``下一个``和``上一个``按钮小部件有助于以新频率显示波形。
|
|
|
|

|
|
|
|
```python
|
|
import numpy as np
|
|
import matplotlib.pyplot as plt
|
|
from matplotlib.widgets import Button
|
|
|
|
freqs = np.arange(2, 20, 3)
|
|
|
|
fig, ax = plt.subplots()
|
|
plt.subplots_adjust(bottom=0.2)
|
|
t = np.arange(0.0, 1.0, 0.001)
|
|
s = np.sin(2*np.pi*freqs[0]*t)
|
|
l, = plt.plot(t, s, lw=2)
|
|
|
|
|
|
class Index(object):
|
|
ind = 0
|
|
|
|
def next(self, event):
|
|
self.ind += 1
|
|
i = self.ind % len(freqs)
|
|
ydata = np.sin(2*np.pi*freqs[i]*t)
|
|
l.set_ydata(ydata)
|
|
plt.draw()
|
|
|
|
def prev(self, event):
|
|
self.ind -= 1
|
|
i = self.ind % len(freqs)
|
|
ydata = np.sin(2*np.pi*freqs[i]*t)
|
|
l.set_ydata(ydata)
|
|
plt.draw()
|
|
|
|
callback = Index()
|
|
axprev = plt.axes([0.7, 0.05, 0.1, 0.075])
|
|
axnext = plt.axes([0.81, 0.05, 0.1, 0.075])
|
|
bnext = Button(axnext, 'Next')
|
|
bnext.on_clicked(callback.next)
|
|
bprev = Button(axprev, 'Previous')
|
|
bprev.on_clicked(callback.prev)
|
|
|
|
plt.show()
|
|
```
|
|
|
|
## 下载这个示例
|
|
|
|
- [下载python源码: buttons.py](https://matplotlib.org/_downloads/buttons.py)
|
|
- [下载Jupyter notebook: buttons.ipynb](https://matplotlib.org/_downloads/buttons.ipynb) |