首页 > 解决方案 > Surfaceplot 高度颜色

问题描述

我正在通过 tcp-data 制作动画曲面图。

当传入的数据很小(随机 0 - 5)时,我会得到一个颜色很好的图表,但是当我发送更大的数据(例如 -50 到 +50)时,颜色会变得混乱(下图),整个表面图是白色的. 我尝试了一些matplotlib Colormaps,但结果相似,只有白色变为另一种颜色,但由于所有颜色都具有相同的颜色,因此看不到任何表面。

这是我的代码:

from pyqtgraph.Qt import QtCore, QtGui
import pyqtgraph.opengl as gl
import numpy as np
import datetime
from matplotlib import cm

numberOfData = 1000
widthOfData = 500
x = np.linspace(-widthOfData / 2, widthOfData / 2, widthOfData)
y = np.linspace(-numberOfData / 2, numberOfData / 2, numberOfData)
#colormap = cm.get_cmap('jet')  # cm.get_cmap("CMRmap") 'viridis'
#colormap._init()
#lut = (colormap._lut * 255).view(np.ndarray)  # Convert matplotlib colormap from 0-1 to 0 -255 for Qt
p4 = gl.GLSurfacePlotItem(x, y, shader='heightColor', computeNormals=False,
                          smooth=False)  # smooth true = faster; dont turn on computenormals
p4.shader()['colorMap'] = np.array([0.2, 2, 0.5, 0.2, 1, 1, 0.2, 0, 2]) #lut
# p4.setGLOptions('opaque')
data = np.zeros((widthOfData, numberOfData), dtype=int)

index = 0


def init():
    global p4, data, index

    ## Create a GL View widget to display data
    app = QtGui.QApplication([])
    w = gl.GLViewWidget()
    w.show()
    w.setWindowTitle('PAS Surfaceplot')
    w.setGeometry(100, 100, 1500, 800)  # distance && resolution
    w.setCameraPosition(distance=1000)

    ## Create axis
    # axis = pg.AxisItem('left', pen=None, linkView=None, parent=None, maxTickLength=-5, showValues=True)
    # axis.show()
    # axis = pg.AxisItem('left', pen = None)
    # xAxis.paint()
    # Axis.setSize(self.valueNumber, self.valueNumber, self.valueNumber)
    # axis.setStyle(showValues = True)
    # axis.show()
    # --------------------
    axis = gl.GLAxisItem()
    # xAxis.paint()
    # axis.setSize(self.valueNumber, self.valueNumber, self.valueNumber)
    w.addItem(axis)

    ## Add a grid to the view
    g = gl.GLGridItem()
    g.setSize(x=widthOfData * 2, y=numberOfData * 2)
    # g.scale(2,2,1000)
    g.setDepthValue(10)  # draw grid after surfaces since they may be translucent
    w.addItem(g)

    ## create a surface plot, tell it to use the 'heightColor' shader
    ## since this does not require normal vectors to render (thus we
    ## can set computeNormals=False to save time when the mesh updates)
    # p4.translate(100, 100, 0)
    w.addItem(p4)

    # timer = QtCore.QTimer()
    # timer.timeout.connect(updateSelf)
    # timer.start(20)

    ## Start Qt event loop unless running in interactive mode.
    import sys

    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()

#update via timer
def updateSelf():
    global p4, data, index
    timeBeforeUpdate = datetime.datetime.now()
    data = np.delete(data, 0, 0)
    newValues = np.random.randint(5, size=(1, numberOfData))
    # print('newval ', newValues)
    data = np.concatenate((data, newValues))
    p4.setData(z=data)
    timeAfterUpdate = datetime.datetime.now()
    timeDiff = timeAfterUpdate - timeBeforeUpdate
    elapsed_ms = (timeDiff.days * 86400000) + (timeDiff.seconds * 1000) + (timeDiff.microseconds / 1000)
    # print(elapsed_ms, ' ms')

#update via tcp
def update(framesList):
    global p4, data, index
    timeBeforeUpdate = datetime.datetime.now()
    for frame in framesList:
        data = np.delete(data, 0, 0)
        frame = np.array(frame, ndmin=2)
        # print('data: ', data)
        # print('frame: ', frame)
        data = np.concatenate((data, frame))
        p4.setData(z=data)
    timeAfterUpdate = datetime.datetime.now()
    timeDiff = timeAfterUpdate - timeBeforeUpdate
    elapsed_ms = (timeDiff.days * 86400000) + (timeDiff.seconds * 1000) + (timeDiff.microseconds / 1000)
    print(elapsed_ms, ' ms')

# init()
# timer = QtCore.QTimer()
# timer.timeout.connect(updateSelf)
# timer.start(20)

我该如何解决?

工作颜色

乱七八糟的颜色

标签: pythonpython-3.xpyqtgraph

解决方案


据我所知,统一的颜色是因为数据密度高。由于随着数据的增加而发生变化,因此您可以尝试缩小数据范围,即对于-50到50,先尝试将其除以10,以便得到-10到10范围内的值。应该有帮助。


推荐阅读