如何更改PYQTgraph中GlBarGraphItem的默认颜色

2024-10-03 00:19:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我的目标是创建堆叠的三维条形图,为此,我尝试从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)本身没有设置颜色的方法。在

有什么进展的提示吗?在


Tags: importsize颜色asnpaxpyqtgraphbg
1条回答
网友
1楼 · 发布于 2024-10-03 00:19:20

如果修订了GLMeshItempaint()方法的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个元素组成的元组,因此您可以使用以下方法:

^{pr2}$

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

相关问题 更多 >