Friday, 19 March 2021

Matching the scatter points colour with the Image colour bar

I have made this GUI with PyQt5:

enter image description here

How can I make the colour of the points in the bottom plot match the colour bar in the 2D plot above? And change accordingly when I change the 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_())

UPDATE

I managed to change the scatter plot to the same colourbar as the 2D image, but I have not yet managed to make it change its colour as I drag the colour bar cursor!

enter image description here

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


from Matching the scatter points colour with the Image colour bar

No comments:

Post a Comment