And*_*bis 5 python plot pyqtgraph
我需要在 PyQtGraph 中绘制一个大的时间序列(数百万个点)。按原样绘制它实际上是不可能的,并且当打开优化选项(使用下采样setDownsampling和使用裁剪setClipToView)时,缩小时它仍然几乎不可用(只有在放大时,由于裁剪,它才会变得很快)。
不过我有一个想法。我可以对数据进行预下采样,因为它们是静态的。然后,我可以在缩小时使用缓存的下采样数据,在放大时使用原始数据。
我怎样才能做到这一点?
@two_pineapples 的答案描述了对 PyQtGraph 中默认下采样的一个非常好的改进,但它仍然需要动态执行下采样,这在我的情况下是有问题的。
因此,我决定实施不同的策略,即对数据进行预下采样,然后根据“缩放级别”选择已经下采样的数据或原始数据。
我将该方法与 PyQtGraph 本身使用的默认自动下采样策略结合起来,以进一步提高速度(可以通过 @ Three_pineapples 建议进一步改进)。
这样,PyQtGraph 总是从低维数据开始,这使得即使有大量样本,缩放和平移也能即时进行。
这段代码总结了我的方法,它修补了PlotDataItem.
# Downsample data
downsampled_data = downsample(data, 100)
# Replacement for the default getData function
def getData(obj):
# Calculate the visible range
range = obj.viewRect()
if range is not None:
dx = float(data[-1, 0] - data[0, 0]) / (data.size[0] - 1)
x0 = (range.left() - data[0, 0]) / dx
x1 = (range.right() - data[0, 0]) / dx
# Decide whether to use downsampled or original data
if (x1 - x0) > 20000:
obj.xData = downsampled_data[:, 0]
obj.yData = downsampled_data[:, 1]
else:
obj.xData = data[:, 0]
obj.yData = data[:, 1]
# Run the original getData of PlotDataItem
return PlotDataItem.getData(obj)
# Replace the original getData with our getData
plot_data_item.getData = types.MethodType(getData, plot_data_item)
Run Code Online (Sandbox Code Playgroud)