mirror of
https://github.com/Estom/notes.git
synced 2026-04-13 17:09:44 +08:00
matplotlib & pandas
This commit is contained in:
73
Python/matplotlab/gallery/event_handling/resample.md
Normal file
73
Python/matplotlab/gallery/event_handling/resample.md
Normal file
@@ -0,0 +1,73 @@
|
||||
# 重采样数据
|
||||
|
||||
下采样会降低信号的采样率或采样大小。在本教程中,当通过拖动和缩放调整打印时,将对信号进行缩减采样。
|
||||
|
||||

|
||||
|
||||
```python
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
|
||||
# A class that will downsample the data and recompute when zoomed.
|
||||
class DataDisplayDownsampler(object):
|
||||
def __init__(self, xdata, ydata):
|
||||
self.origYData = ydata
|
||||
self.origXData = xdata
|
||||
self.max_points = 50
|
||||
self.delta = xdata[-1] - xdata[0]
|
||||
|
||||
def downsample(self, xstart, xend):
|
||||
# get the points in the view range
|
||||
mask = (self.origXData > xstart) & (self.origXData < xend)
|
||||
# dilate the mask by one to catch the points just outside
|
||||
# of the view range to not truncate the line
|
||||
mask = np.convolve([1, 1], mask, mode='same').astype(bool)
|
||||
# sort out how many points to drop
|
||||
ratio = max(np.sum(mask) // self.max_points, 1)
|
||||
|
||||
# mask data
|
||||
xdata = self.origXData[mask]
|
||||
ydata = self.origYData[mask]
|
||||
|
||||
# downsample data
|
||||
xdata = xdata[::ratio]
|
||||
ydata = ydata[::ratio]
|
||||
|
||||
print("using {} of {} visible points".format(
|
||||
len(ydata), np.sum(mask)))
|
||||
|
||||
return xdata, ydata
|
||||
|
||||
def update(self, ax):
|
||||
# Update the line
|
||||
lims = ax.viewLim
|
||||
if np.abs(lims.width - self.delta) > 1e-8:
|
||||
self.delta = lims.width
|
||||
xstart, xend = lims.intervalx
|
||||
self.line.set_data(*self.downsample(xstart, xend))
|
||||
ax.figure.canvas.draw_idle()
|
||||
|
||||
|
||||
# Create a signal
|
||||
xdata = np.linspace(16, 365, (365-16)*4)
|
||||
ydata = np.sin(2*np.pi*xdata/153) + np.cos(2*np.pi*xdata/127)
|
||||
|
||||
d = DataDisplayDownsampler(xdata, ydata)
|
||||
|
||||
fig, ax = plt.subplots()
|
||||
|
||||
# Hook up the line
|
||||
d.line, = ax.plot(xdata, ydata, 'o-')
|
||||
ax.set_autoscale_on(False) # Otherwise, infinite loop
|
||||
|
||||
# Connect for changing the view limits
|
||||
ax.callbacks.connect('xlim_changed', d.update)
|
||||
ax.set_xlim(16, 365)
|
||||
plt.show()
|
||||
```
|
||||
|
||||
## 下载这个示例
|
||||
|
||||
- [下载python源码: resample.py](https://matplotlib.org/_downloads/resample.py)
|
||||
- [下载Jupyter notebook: resample.ipynb](https://matplotlib.org/_downloads/resample.ipynb)
|
||||
Reference in New Issue
Block a user