【问题标题】:Surfaceplot HeightcolorsSurfaceplot 高度颜色
【发布时间】:2019-01-24 07:56:57
【问题描述】:

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

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

这是我的代码:

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)

我该如何解决这个问题?

工作颜色

颜色混乱

【问题讨论】:

    标签: python python-3.x pyqtgraph


    【解决方案1】:

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

    【讨论】:

    • 嘿 Nitin,缩小数据规模是可行的,但这并不是我一直在寻找的解决方案。我不认为数据的密度或大小是原因,因为发送更少的数据时问题仍然存在。我认为问题必须与某种间隔有关,因为表面底部有颜色,但之后会变成白色,例如:imgur.com/1mjJARE
    • 图像显示颜色变亮直到一个点,然后变得绝对明亮。您可以尝试从 10 增加深度值吗?我认为这可能会导致这种行为。
    • g.setDepthValue(10) 仅适用于网格,对吗?我刚才尝试增加它,并且还添加了p4.setDepthValue(1000)。遗憾的是两者都没有解决问题
    【解决方案2】:

    我已经解决了这个问题

    self.surfacePlot.shader()['colorMap'] = np.array([0.01, 40, 0.5, 0.01, 40, 1, 0.01, 40, 2])  # lut
    

    问题确实是颜色变亮的太快了。

    颜色图由 3 个三元组定义,其中 index_0^index_3 确定颜色。所以在我的情况下,它现在是 0.01^0.5、0.01^1 和 0.01^2。 遗憾的是,我在评论中找不到 wiki 代码示例

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-05
      • 1970-01-01
      • 2021-05-24
      • 2021-11-03
      • 2018-05-19
      • 2016-10-25
      • 2018-10-29
      • 2016-04-04
      相关资源
      最近更新 更多