pyqtgraph:在 GLSurfacePlotItem 对象上设置 smooth=False 的问题

Posted

技术标签:

【中文标题】pyqtgraph:在 GLSurfacePlotItem 对象上设置 smooth=False 的问题【英文标题】:pyqtgraph: problem setting smooth=False on a GLSurfacePlotItem object 【发布时间】:2020-05-24 00:59:32 【问题描述】:

我目前正在尝试使用pyqtgraph 在 3D 中绘制一个基本的颜色编码“切片”。当我用smooth=True 初始化GLSurfacePlotItem 对象时,一切正常。但是,当我简单地设置smooth=False 时,我会收到大量错误和警告。不幸的是,文档并没有真正解释这个设置。

下面是我的示例代码。我错过了什么?

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
import numpy as np
import matplotlib
import matplotlib.pyplot as plt

from pyqtgraph.Qt import QtCore, QtGui
import pyqtgraph.opengl as gl
import pyqtgraph as pg

x, y, z = 30, 20, 10
data = np.random.rand(x,y,z)

cmap = matplotlib.cm.get_cmap('jet')
norm = matplotlib.colors.PowerNorm(vmin=0.0, vmax=1.0, gamma=2.5)
m = matplotlib.cm.ScalarMappable(norm=norm, cmap=cmap)

colors = np.zeros((x,y,z,4), dtype=np.float16)
for i in range(x):
    colors[i,:,:,:] = m.to_rgba(data[i,:,:])

app = QtGui.QApplication(sys.argv)
w = gl.GLViewWidget()

zslice = gl.GLSurfacePlotItem(x=np.arange(0,x,1), y=np.arange(0,y,1), z=np.ones((x,y))*0.5, colors=colors[:,:,0,:], smooth=True, computeNormals=False)

w.addItem(zslice)
w.setGeometry(100, 10, 1280, 720)
w.show()

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

我设置smooth=False时得到的错误信息:

[13:16:04]

    |==============================>>
    |  Traceback (most recent call last):
    |    File "./test5.py", line 34, in <module>
    |      QtGui.QApplication.instance().exec_()
    |    File "/home/steve/anaconda3/lib/python3.7/site-packages/pyqtgraph/opengl/GLViewWidget.py", line 179, in paintGL
    |      self.drawItemTree(useItemNames=useItemNames)
    |    File "/home/steve/anaconda3/lib/python3.7/site-packages/pyqtgraph/opengl/GLViewWidget.py", line 219, in drawItemTree
    |      self.drawItemTree(i, useItemNames=useItemNames)
    |    File "/home/steve/anaconda3/lib/python3.7/site-packages/pyqtgraph/opengl/GLViewWidget.py", line 200, in drawItemTree
    |      debug.printExc()
    |    --- exception caught here ---
    |    File "/home/steve/anaconda3/lib/python3.7/site-packages/pyqtgraph/opengl/GLViewWidget.py", line 197, in drawItemTree
    |      i.paint()
    |    File "/home/steve/anaconda3/lib/python3.7/site-packages/pyqtgraph/opengl/items/GLMeshItem.py", line 167, in paint
    |      self.parseMeshData()
    |    File "/home/steve/anaconda3/lib/python3.7/site-packages/pyqtgraph/opengl/items/GLMeshItem.py", line 151, in parseMeshData
    |      self.colors = md.vertexColors(indexed='faces')
    |    File "/home/steve/anaconda3/lib/python3.7/site-packages/pyqtgraph/opengl/MeshData.py", line 239, in vertexColors
    |      self._vertexColorsIndexedByFaces = self._vertexColors[self.faces()]
    |  IndexError: index 30 is out of bounds for axis 0 with size 30
    |==============================<<
Error while drawing item <pyqtgraph.opengl.items.GLSurfacePlotItem.GLSurfacePlotItem object at 0x7f9f0cf21410>.
[13:16:04]

    |==============================>>
    |  Traceback (most recent call last):
    |    File "./test5.py", line 34, in <module>
    |      QtGui.QApplication.instance().exec_()
    |    File "/home/steve/anaconda3/lib/python3.7/site-packages/pyqtgraph/opengl/GLViewWidget.py", line 179, in paintGL
    |      self.drawItemTree(useItemNames=useItemNames)
    |    File "/home/steve/anaconda3/lib/python3.7/site-packages/pyqtgraph/opengl/GLViewWidget.py", line 219, in drawItemTree
    |      self.drawItemTree(i, useItemNames=useItemNames)
    |    File "/home/steve/anaconda3/lib/python3.7/site-packages/pyqtgraph/opengl/GLViewWidget.py", line 200, in drawItemTree
    |      debug.printExc()
    |    --- exception caught here ---
    |    File "/home/steve/anaconda3/lib/python3.7/site-packages/pyqtgraph/opengl/GLViewWidget.py", line 197, in drawItemTree
    |      i.paint()
    |    File "/home/steve/anaconda3/lib/python3.7/site-packages/pyqtgraph/opengl/items/GLMeshItem.py", line 167, in paint
    |      self.parseMeshData()
    |    File "/home/steve/anaconda3/lib/python3.7/site-packages/pyqtgraph/opengl/items/GLMeshItem.py", line 151, in parseMeshData
    |      self.colors = md.vertexColors(indexed='faces')
    |    File "/home/steve/anaconda3/lib/python3.7/site-packages/pyqtgraph/opengl/MeshData.py", line 239, in vertexColors
    |      self._vertexColorsIndexedByFaces = self._vertexColors[self.faces()]
    |  IndexError: index 30 is out of bounds for axis 0 with size 30
    |==============================<<
Error while drawing item <pyqtgraph.opengl.items.GLSurfacePlotItem.GLSurfacePlotItem object at 0x7f9f0cf21410>.
[13:16:04]

    |==============================>>
    |  Traceback (most recent call last):
    |    File "./test5.py", line 34, in <module>
    |      QtGui.QApplication.instance().exec_()
    |    File "/home/steve/anaconda3/lib/python3.7/site-packages/pyqtgraph/opengl/GLViewWidget.py", line 179, in paintGL
    |      self.drawItemTree(useItemNames=useItemNames)
    |    File "/home/steve/anaconda3/lib/python3.7/site-packages/pyqtgraph/opengl/GLViewWidget.py", line 219, in drawItemTree
    |      self.drawItemTree(i, useItemNames=useItemNames)
    |    File "/home/steve/anaconda3/lib/python3.7/site-packages/pyqtgraph/opengl/GLViewWidget.py", line 200, in drawItemTree
    |      debug.printExc()
    |    --- exception caught here ---
    |    File "/home/steve/anaconda3/lib/python3.7/site-packages/pyqtgraph/opengl/GLViewWidget.py", line 197, in drawItemTree
    |      i.paint()
    |    File "/home/steve/anaconda3/lib/python3.7/site-packages/pyqtgraph/opengl/items/GLMeshItem.py", line 167, in paint
    |      self.parseMeshData()
    |    File "/home/steve/anaconda3/lib/python3.7/site-packages/pyqtgraph/opengl/items/GLMeshItem.py", line 151, in parseMeshData
    |      self.colors = md.vertexColors(indexed='faces')
    |    File "/home/steve/anaconda3/lib/python3.7/site-packages/pyqtgraph/opengl/MeshData.py", line 239, in vertexColors
    |      self._vertexColorsIndexedByFaces = self._vertexColors[self.faces()]
    |  IndexError: index 30 is out of bounds for axis 0 with size 30
    |==============================<<
Error while drawing item <pyqtgraph.opengl.items.GLSurfacePlotItem.GLSurfacePlotItem object at 0x7f9f0cf21410>.
[13:16:06]

    |==============================>>
    |  Traceback (most recent call last):
    |    File "./test5.py", line 34, in <module>
    |      QtGui.QApplication.instance().exec_()
    |    File "/home/steve/anaconda3/lib/python3.7/site-packages/pyqtgraph/opengl/GLViewWidget.py", line 179, in paintGL
    |      self.drawItemTree(useItemNames=useItemNames)
    |    File "/home/steve/anaconda3/lib/python3.7/site-packages/pyqtgraph/opengl/GLViewWidget.py", line 219, in drawItemTree
    |      self.drawItemTree(i, useItemNames=useItemNames)
    |    File "/home/steve/anaconda3/lib/python3.7/site-packages/pyqtgraph/opengl/GLViewWidget.py", line 200, in drawItemTree
    |      debug.printExc()
    |    --- exception caught here ---
    |    File "/home/steve/anaconda3/lib/python3.7/site-packages/pyqtgraph/opengl/GLViewWidget.py", line 197, in drawItemTree
    |      i.paint()
    |    File "/home/steve/anaconda3/lib/python3.7/site-packages/pyqtgraph/opengl/items/GLMeshItem.py", line 167, in paint
    |      self.parseMeshData()
    |    File "/home/steve/anaconda3/lib/python3.7/site-packages/pyqtgraph/opengl/items/GLMeshItem.py", line 151, in parseMeshData
    |      self.colors = md.vertexColors(indexed='faces')
    |    File "/home/steve/anaconda3/lib/python3.7/site-packages/pyqtgraph/opengl/MeshData.py", line 239, in vertexColors
    |      self._vertexColorsIndexedByFaces = self._vertexColors[self.faces()]
    |  IndexError: index 30 is out of bounds for axis 0 with size 30
    |==============================<<
Error while drawing item <pyqtgraph.opengl.items.GLSurfacePlotItem.GLSurfacePlotItem object at 0x7f9f0cf21410>.

【问题讨论】:

@eyllanesc 我已经在上面添加了 【参考方案1】:

错误提示“IndexError: index 30 is out of bounds for axis 0 with size 30”这表明您或您正在使用的库可能正在循环 1 到 30 而不是 0 到 29。(设置 smooth=False可能会将您的代码发送到库中包含错误的部分。)由于您的 x 变量是 30,我猜这是原点。错误消息说问题出在 ./test5.py",第 34 行,所以我将从那里开始。

【讨论】:

以上是关于pyqtgraph:在 GLSurfacePlotItem 对象上设置 smooth=False 的问题的主要内容,如果未能解决你的问题,请参考以下文章

pyqtgraph绘图如何使用pyqtgraph

如何在 pyqtgraph 中写 \latex 字符?

从 pyqtgraph.Qt 导入 QtGui 为 *

在 PlotWidget GUI 中运行实时 pyqtgraph

在pyqtgraph中实现实时绘图的最简单方法是啥

pyqtgraph绘图安装pyqtgraph