pyqtgraph select 2D region of graph as threshold to redraw the graph

假如想象 提交于 2019-12-01 09:24:42

问题


I wish to add functionality such that the user can draw a rectangle over a selection of lines and the graph will refresh such that the lines within the rectangle keep their respective colour and any lines outside turn grey?

My code is as follows, and currently zooms on the drawing of a user defined rectangle over the lines, (for 3 lines, my actual code will plot a lot more):

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

pg.setConfigOption('background', 'w')
pg.setConfigOption('foreground', 'k')

from random import randint

class CustomViewBox(pg.ViewBox):
    def __init__(self, *args, **kwds):
    pg.ViewBox.__init__(self, *args, **kwds)
    self.setMouseMode(self.RectMode)

    ## reimplement right-click to zoom out
    def mouseClickEvent(self, ev):
    if ev.button() == QtCore.Qt.RightButton:
        #self.autoRange()
        self.setXRange(0,5)
        self.setYRange(0,10)

    def mouseDragEvent(self, ev):
    if ev.button() == QtCore.Qt.RightButton:
        ev.ignore()
    else:
        pg.ViewBox.mouseDragEvent(self, ev)


app = pg.mkQApp()

vb = CustomViewBox()

graph = pg.PlotWidget(viewBox=vb, enableMenu=False)

colour = []

for i in range(0,3):
    colourvalue = [randint(0,255), randint(0,255), randint(0,255)]
    tuple(colourvalue)
    colour.append(colourvalue)

y_data = [ 
     [['a',0],['b',1],['c',None],['d',6],['e',7]],
     [['a',5],['b',2],['c',1],['d',None],['e',1]],
     [['a',3],['b',None],['c',4],['d',9],['e',None]],
     ]

x_data = [0, 1, 2, 3, 4]

for i in range(3):
    xv = []
    yv = []
    for j, v in enumerate(row[i][1] for row in y_data):
    if v is not None:
        xv.append(int(j))
        yv.append(float(v))
    graph.plot(xv, yv, pen = colour[i], name=y_data[0][i][0])

graph.show()
graph.setWindowTitle('Hourly Frequency Graph')
graph.setXRange(0,5)
graph.setYRange(0,10)

graph.setLabel('left', "Frequency", units='%')
graph.setLabel('bottom', "Hour")
graph.showGrid(x=True, y=True)

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

Thanks in advance for any help and advice!

As an aside I would also like to know why this code always give a segmentation fault: 11 when I close the window.

来源:https://stackoverflow.com/questions/18867980/pyqtgraph-select-2d-region-of-graph-as-threshold-to-redraw-the-graph

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!