在 Pyqtgraph 中绘制交互式时间跟踪器

Plotting an interactive time-tracker in Pyqtgraph

我目前正在创建一条交互式时间跟踪线,它沿 X 轴穿过曲线。

问题:我已经能够使用 Matplotlib 中的动画编写代码,但只能使线条移动(即,它不是互动).

import numpy as np
import csv
import csv_splitter
import matplotlib
import matplotlib.pyplot as plt
from matplotlib import animation
import matplotlib.colors as cm
from scipy import interpolate
from scipy import ndimage
from PIL import Image
import cv2
import os
plt.rcParams['animation.ffmpeg_path'] = unicode ('C:/FFMPEG/bin/ffmpeg.exe')

with open('C:/Users/j/Desktop/Force.csv','r') as input, open('C:/Users/j/Desktop/Op.csv', 'w') as output:
    try:
        read = csv.reader(input)
        for r in read:
            non_blank = (line for line in input if line.strip())            
            for i in range(17):
                next(read)                
            output.writelines(non_blank)
    finally:
        print("Done!")

data_orig = np.genfromtxt('C:/Users/jkspu/Desktop/Op.csv', dtype =int, delimiter = ",")
fig = plt.figure()
X = data_orig[412:524,1]
Y1 = data_orig[412:524,4]
Y2 = data_orig[412:524,3]
Y3 = data_orig[412:524,2]

plt.plot(X, Y1, 'r--', X, Y2, 'b--', X, Y3, 'g--')
plt.xlabel('Time -----> (ms)')
plt.ylabel('Force Data -----> (N)')

X_MIN = 2055
X_MAX = 2615
Y_MIN = -300
Y_MAX = 1500
X_VALS = range(X_MIN, X_MAX+1);


def update_line(num, line):
    i = X_VALS[num]
    line.set_data( [i, i], [Y_MIN, Y_MAX])
    return line,

l , v = plt.plot(2055, -200, 2615, 1200, linewidth=2, color= 'gold')
anim = animation.FuncAnimation(fig, update_line, len(X_VALS), fargs=(l, ), interval=15, blit=True, repeat=False)
print(update_line)

FFwriter = animation.FFMpegWriter(fps=30, extra_args=['-vcodec', 'libx264'])
anim.save('basic_animation.mp4', writer = FFwriter)
plt.show()

目标: 我有兴趣让时间跟踪线互动并移动(保持动画)。为此,我正在尝试使用Pyqtgraph(希望找到交互式解决方案)。

我用过的代码是这样的(但是我不知道如何在这段代码中插入matplotlib动画):

import numpy as np
import csv
import csv_splitter
import cv2
import os

from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import pyqtgraph as pg

app = QtGui.QApplication([])

with open('C:/Users/j/Desktop/Force.csv','r') as input, open('C:/Users/j/Desktop/Op.csv', 'w') as output:
    try:
        read = csv.reader(input)
        for r in read:
            non_blank = (line for line in input if line.strip())            
            for i in range(17):
                next(read)                
            output.writelines(non_blank)
    finally:
        print("Done!")

data_orig = np.genfromtxt('C:/Users/j/Desktop/Op.csv', dtype =int, delimiter = ",")

win = pg.GraphicsWindow(title="Plot")
win.resize(1000, 600)
win.setWindowTitle('First_Plot')
p1 = win.addPlot(title="Parametric, grid enabled")
X = data_orig[412:524,1]
Y1 = data_orig[412:524,4]
Y2 = data_orig[412:524,3]
Y3 = data_orig[412:524,2]

p1.plot(X, Y1, pen = 'r')
p1.plot(X, Y2, pen = 'b')
fig = p1.plot(X, Y3, pen = 'g')

p1.setLabel('left', "Y Axis", units = 'N')
p1.setLabel('bottom', "X Axis", units = 'ms')






if __name__ == '__main__':
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()

一张我理想中期待的照片:

请求:请问一些在 Pyqtgraph 中制作动画线条(+ 交互式)的专业知识?

希望我能说清楚,如果不清楚请告诉我,谢谢!

我猜你希望能够:

1) 添加用户可以拖动的垂直线。为此,尝试

vline = p1.addLine(x=0, movable=True)

2) 在垂直线与绘图曲线相交的地方绘制标记。您可以通过连接到线路移动时发出的信号来手动执行此操作:

def update(line):
    x = line.value()
    # find and plot intersection points

vline.sigPositionChanged.connect(update)