【发布时间】: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_())
【问题讨论】: