将散点颜色与图像颜色条匹配

时间:2021-03-16 06:20:26

标签: python pyqt5 pyqtgraph

我用 PyQt5 制作了这个 GUI:

enter image description here

如何使底部绘图中的点的颜色与上面二维绘图中的颜色条相匹配?并在我更改 bar max/min 时相应地更改?


MWE:

from PyQt5 import QtGui, QtCore
import pyqtgraph as pg
import sys
import numpy as np

width = 1000
height = 500

x = np.linspace(-10,10,100)
def func():
    X, Y = np.meshgrid(x, x)
    return np.exp(-X**2/10 - Y**2/2)

array = func()
sumxaxis = np.sum(array, axis=0)
sumyaxis = np.sum(array, axis=1)

class layout():
    def setup(self, window):
        self.window = window
        self.window.resize(width, height)

        self.centralwidget = QtGui.QWidget(self.window)
        self.horizontallayout = QtGui.QHBoxLayout(self.centralwidget)
        self.window.setCentralWidget(self.centralwidget)

        self.plot = pg.GraphicsLayoutWidget(self.window)

        self.horizontallayout.addWidget(self.plot)
        self.view = self.plot.addPlot()
        self.img = pg.ImageItem(border='k')
        self.img.setImage(array, border = 'k')
        self.view.addItem(self.img)
        self.viewbox = self.view.getViewBox()

        self.hist = pg.HistogramLUTItem()
        self.hist.setImageItem(self.img)
        self.hist.setLevels(0, 1)
        self.hist.gradient.loadPreset('viridis')
        self.plot.addItem(self.hist, colspan=1)

        self.plot.nextRow()
        self.plot2 = self.plot.addPlot(colspan=1)
        self.plot2.setMaximumHeight(200)
        self.plot2.plot(-x,sumyaxis,symbol='o',symbolSize=1, symbolBrush=('w'), pen=None, clear=True)


class Window(pg.Qt.QtGui.QMainWindow, layout):

    def __init__(self, shot = None):

        super(Window, self).__init__()
        self.setup(self)
        self.show()

if __name__ == '__main__':
    app = pg.Qt.QtGui.QApplication([])
    Window()
    sys.exit(app.exec_())

更新

我设法将散点图更改为与 2D 图像相同的颜色条,但我还没有在拖动颜色条光标时使其颜色发生变化!

enter image description here

新的 MWE:

from PyQt5 import QtGui, QtCore
import pyqtgraph as pg
import sys
import numpy as np

width = 1000
height = 500

x = np.linspace(-10,10,100)
def func():
    X, Y = np.meshgrid(x, x)
    return np.exp(-X**2/10 - Y**2/2)

array = func()
sumyaxis = array[:, len(array)//2]

class layout():
    def setup(self, window):
        self.window = window
        self.window.resize(width, height)

        self.centralwidget = QtGui.QWidget(self.window)
        self.horizontallayout = QtGui.QHBoxLayout(self.centralwidget)
        self.window.setCentralWidget(self.centralwidget)

        self.plot = pg.GraphicsLayoutWidget(self.window)

        self.horizontallayout.addWidget(self.plot)
        self.view = self.plot.addPlot()
        self.img = pg.ImageItem(border='k')
        self.img.setImage(array, border = 'k')
        self.view.addItem(self.img)
        self.viewbox = self.view.getViewBox()

        self.hist = pg.HistogramLUTItem()
        self.hist.setImageItem(self.img)
        self.hist.setLevels(0, 1)
        self.hist.gradient.loadPreset('viridis')
        self.plot.addItem(self.hist, colspan=1)

        self.colours = self.hist.getLookupTable(img=array)
        self.cmap = pg.ColorMap(pos=np.linspace(self.hist.getLevels()[0], self.hist.getLevels()[1], len(self.colours)), color=self.colours)

        self.plot.nextRow()
        self.plot2 = self.plot.addPlot(colspan=1)
        self.plot2.setMaximumHeight(200)
        self.c = self.cmap.map(sumyaxis, 'qcolor')
        self.plot2.plot(-x,sumyaxis,symbol='o',symbolSize=10, symbolBrush = self.c, pen=None, clear=True)

class Window(pg.Qt.QtGui.QMainWindow, layout):

    def __init__(self, shot = None):

        super(Window, self).__init__()
        self.setup(self)

        self.show()

if __name__ == '__main__':
    app = pg.Qt.QtGui.QApplication([])
    w = Window()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:3)

您必须使用 sigLookupTableChanged 信号并实现更新 PlotDataItem 的 symbolBrush 的逻辑:

    # ...
    self.plot.nextRow()
    self.plot2 = self.plot.addPlot(colspan=1)
    self.plot2.setMaximumHeight(200)
    self.curve = self.plot2.plot(
        -x,
        sumyaxis,
        symbol="o",
        symbolSize=10,
        pen=None,
        clear=True,
    )
    self.hist.sigLookupTableChanged.connect(self.handle_sigLookupTableChanged)
    self.handle_sigLevelsChanged()

def handle_sigLookupTableChanged(self):
    colours = self.hist.getLookupTable(img=array)
    cmap = pg.ColorMap(
        pos=np.linspace(*self.hist.getLevels(), len(colours)),
        color=colours,
    )
    c = cmap.map(sumyaxis, "qcolor")
    self.curve.opts["symbolBrush"] = c
    self.curve.updateItems()