Skip to main content
 首页 » 编程设计

python之根据 pyqtgraph.opengl 中的信号值调整曲面图项目的颜色

2024年11月01日8lyj

这里是尝试做一个瀑布表示。 我需要用不同的颜色表示信号(数组)值(振幅/电平/密度),而不是用阴影表示。 因为我是一名算法和信号处理工程师。而不是软件开发人员,我不熟悉彩色 map 和这些东西。因此,如果有人可以帮我解决将颜色与信号值相关联的代码。

from pyqtgraph.Qt import QtCore, QtGui 
import pyqtgraph.opengl as gl 
import scipy.ndimage as ndi 
import numpy as np 
 
Nf = 90     # No. of frames 
Ns = 100    # Signal length 
 
app = QtGui.QApplication([]) 
 
w_SA = QtGui.QWidget(); w_SA.setFixedSize(400, 400) 
 
# Create a GL View widget to display data 
plt_SA1 = gl.GLViewWidget(w_SA); plt_SA1.move(10, 10); plt_SA1.resize(380, 380) 
plt_SA1.setCameraPosition(elevation=90.0, azimuth=0.0, distance=70) 
p1 = gl.GLSurfacePlotItem(shader='shaded', color=(0.5, 0.5, 1, 1), smooth=False) 
p1.translate(-Nf/2, -Ns/2, 0) 
plt_SA1.addItem(p1) 
 
Arx = np.zeros([Nf, Ns]) 
def update(): 
    global Arx 
    Arx = np.roll(Arx, 1, axis=0) 
    Arx[0] = ndi.gaussian_filter(np.random.normal(size=(1,Ns)), (1,1)) 
    p1.setData(z=Arx) 
timer = QtCore.QTimer() 
timer.timeout.connect(update) 
timer.start(30) 
 
w_SA.show() 
 
if __name__ == '__main__': 
    import sys 
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'): 
        QtGui.QApplication.instance().exec_() 

请您参考如下方法:

您能否更具体地说明您希望图像如何着色?如果您不需要 OpenGL,这里有一个使用 pyqtgraph.ImageView 的更简单的解决方案。您可以右键单击右侧的渐变条以更改用于为图像着色的查找表。也有多种方法可以手动设置此表,具体取决于所需的效果。

from pyqtgraph.Qt import QtCore, QtGui 
import pyqtgraph as pg 
import scipy.ndimage as ndi 
import numpy as np 
 
Nf = 90     # No. of frames 
Ns = 100    # Signal length 
 
app = QtGui.QApplication([]) 
 
Arx = np.zeros([Nf, Ns]) 
win = pg.image(Arx) 
win.view.setAspectLocked() 
def update(): 
    global Arx 
    Arx = np.roll(Arx, 1, axis=0) 
    Arx[0] = ndi.gaussian_filter(np.random.normal(size=(1,Ns)), (1, 1)) 
    win.setImage(Arx.T, autoRange=False) 
 
timer = QtCore.QTimer() 
timer.timeout.connect(update) 
timer.start(30) 
 
if __name__ == '__main__': 
    import sys 
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'): 
        QtGui.QApplication.instance().exec_()