【问题标题】:Matching the scatter points colour with the Image colour bar将散点颜色与图像颜色条匹配
【发布时间】:2021-06-13 10:17:54
【问题描述】:

我用 PyQt5 制作了这个 GUI:

如何使底部图中点的颜色与上面二维图中的颜色条相匹配?并在我更改最大/最小值时相应更改?


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 图像相同的颜色条,但我还没有设法在拖动颜色条光标时让它改变颜色!

新 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_())

【问题讨论】:

    标签: python pyqt5 pyqtgraph


    【解决方案1】:

    您必须使用 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()

    【讨论】:

    • "AttributeError: 'Window' 对象没有属性 'handlesigLevelsChanged' "
    • @SuperCiocia 你真的复制了我的代码吗?我不这么认为
    • 是的,我刚刚复制了它。无论如何,我设法让它工作 - 我删除了 self.handle_sigLevelsChanged() 并将之前的行更改为 self.hist.sigLevelsChanged.connect(self.handle_sigLookupTableChanged)
    • @SuperCiocia handlesigLevelsChangedhandle_sigLevelsChanged 不同
    • 谢谢。根据我自己的理解,你怎么知道这是语法?我查看了 HistogramLUTItem 的源代码,但找不到使用不断变化的颜色条更新 2D 图像颜色的函数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-10
    • 1970-01-01
    • 2018-08-24
    • 2021-08-09
    • 2011-08-29
    相关资源
    最近更新 更多