【问题标题】:Pyqtgraph & Changing color base on height for surfaceplotPyqtgraph & 根据表面图的高度更改颜色
【发布时间】:2018-11-04 20:49:53
【问题描述】:

我正在寻找一种方法来使我的表面绘图项目根据高度改变颜色。以下是我目前的方法:

def __init__(self, s):

    self.traces = dict()
    self.app = QtGui.QApplication(sys.argv)
    self.w = gl.GLViewWidget()
    self.w.opts['distance'] = 2000
    self.w.setWindowTitle('pyqtgraph example: GLLinePlotItem')
    self.w.setGeometry(0, 0, 600, 600)
    self.w.show()
    self.socket = s

    self.timer = QtCore.QTimer()
    self.timer.setInterval(1) # in milliseconds
    self.timer.start()
    self.timer.timeout.connect(self.onNewData)

    # create the background grids
    #gx is the y grid
    #gz is the x gid
    gx = gl.GLGridItem()
    gx.rotate(90, 0, 1, 0)
    gx.translate(0, 0, 0)
    self.w.addItem(gx)
    gz = gl.GLGridItem()
    gz.translate(200, 0, -500)
    self.w.addItem(gz)
    gx.scale(100, 10, 100)
    gz.scale(20, 10, 100)


    self.y = np.linspace(0, 100, 10)
    self.x = np.linspace(60,400, 708)
    temp_z = np.zeros((10,708))
    self.surf = gl.GLSurfacePlotItem(x=self.y, y=self.x, z=temp_z, shader='heightColor',
                                     computeNormals=False, smooth=False)
    self.surf.scale(3,1,1)
    self.surf.shader()['colorMap'] = np.array([0.7, 2, 0.5, 0.2, 0.7, 0.7, 0.2, 0, 2])
    self.w.addItem(self.surf)

但该方法效果不佳。随着 Z 值变得非常高,表面变得完全白色。顺便说一句,我不知道我在用颜色图做什么,我只是把它从示例中删除。

【问题讨论】:

    标签: python opengl pyqtgraph


    【解决方案1】:

    我建议你使用GLSurfacePlotItemcolors 选项。 这个想法是计算与表面的 z 值(高度)相关的颜色,并使它们标准化(在 0 和 1 之间)。有了这个,您可以计算表面每个点的颜色,例如 cmapmatlotlib

    # -*- coding: utf-8 -*-
    from __future__ import print_function, absolute_import
    from pyqtgraph.Qt import QtCore, QtGui
    import pyqtgraph as pg
    import pyqtgraph.opengl as gl
    import matplotlib.pyplot as plt
    import numpy as np
    import os
    from PyQt4.QtGui import QFileDialog
    import sys
    
    if not( 'app' in locals()):
        app = QtGui.QApplication([])
    
    traces = dict()
    # app = QtGui.QApplication(sys.argv)
    w = gl.GLViewWidget()
    w.opts['distance'] = 2000
    w.setWindowTitle('pyqtgraph example: GLLinePlotItem')
    w.setGeometry(0, 0, 600, 600)
    w.show()
    # socket = s
    
    # timer = QtCore.QTimer()
    # timer.setInterval(1) # in milliseconds
    # timer.start()
    # timer.timeout.connect(onNewData)
    
    # create the background grids
    #gx is the y grid
    #gz is the x gid
    gx = gl.GLGridItem()
    gx.rotate(90, 0, 1, 0)
    gx.translate(0, 0, 0)
    w.addItem(gx)
    gz = gl.GLGridItem()
    gz.translate(200, 0, -500)
    w.addItem(gz)
    gx.scale(100, 10, 100)
    gz.scale(20, 10, 100)
    
    
    y = np.linspace(0, 100, 10)
    print(y)
    x = np.linspace(0,100, 10)
    print(x)
    temp_z = np.random.rand(len(x),len(y))*100.
    
    cmap = plt.get_cmap('jet')
    
    minZ=np.min(temp_z)
    maxZ=np.max(temp_z)
    rgba_img = cmap((temp_z-minZ)/(maxZ -minZ))
    
    
    surf = gl.GLSurfacePlotItem(x=y, y=x, z=temp_z, colors = rgba_img )
    
    surf.scale(3,1,1)
    # surf.shader()['colorMap'] = np.array(list(np.linspace(-100, 100, 1000)))
    w.addItem(surf)
    
    if __name__ == '__main__':
        import sys
    
        if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
            QtGui.QApplication.instance().exec_()
    

    给:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-20
      • 1970-01-01
      • 2020-09-26
      • 1970-01-01
      • 2021-11-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多