如何在PYQTgraph

时间:2018-06-01 14:37:00

标签: python python-3.x pyqtgraph

我的目标是创建堆叠的3d条形图,为此,我试图从PYQTgraph库中更改GlBarGraphItem的颜色。

这是我的代码:

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

app = QtGui.QApplication([])
w = gl.GLViewWidget()
w.opts['distance'] = 100
w.showMaximized()
w.setWindowTitle('pyqtgraph example: GLViewWidget')

ax = gl.GLAxisItem()
ax.setSize(20,20,20)
w.addItem(ax)

pos = np.mgrid[0:1,0:1,0:1].reshape(3,1,1).transpose(1,2,0)

size = np.empty((1,1,3)) 
size[...,0:2] = 1
size[...,2] = 5

bg = gl.GLBarGraphItem(pos, size)
##bg.setColor(1., 1., 1., 1.)
w.addItem(bg)

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

我尝试使用.setColor()方法没有成功,对象(GlBarGraphItem)本身没有设置颜色的方法我相信。

任何暗示如何进步?

1 个答案:

答案 0 :(得分:1)

如果修改了GLMeshItem paint()方法的code

if self.colors is None:
    color = self.opts['color']
    if isinstance(color, QtGui.QColor):
        glColor4f(*fn.glColor(color))
    else:
        glColor4f(*color)

因此setColor()函数需要QColor或4个元素的元组,因此您可以使用以下方法:

bg.setColor((0., 1., 0., 1))

enter image description here

color = QtGui.QColor("pink")
bg.setColor(color)

enter image description here

color = QtGui.QColor(120, 14, 12)
bg.setColor(color)

enter image description here