GLBarGraphItem PYQTgraph python中的颜色重叠

问题描述 投票:0回答:1

我正在尝试删除使用GLBarGraphItem绘制堆叠的3d条时发生的颜色重叠。以下是我的代码:

from pyqtgraph.Qt import QtCore, QtGui
import pyqtgraph.opengl as gl
import numpy as np
import pyqtgraph as pg
from itertools import groupby
#pg.setConfigOptions(antialias=False)

mainList = [['BM', 'Butterfly', 'E-shop', '1400'],
            ['BM', 'Butterfly', 'Fler', '2450'],
            ['BM', 'Butterfly', 'Holesovice', '2450'],
            ['Climbing presents', 'Ear-rings', 'Holesovice', '136'],
            ['Climbing presents', 'Other jewellery', 'E-shop', '160'],
            ['Climbing presents', 'Other jewellery', 'Other', '112'],
            ['PP', 'Skirts', 'Fler', '1380'],
            ['PP', 'Skirts', 'Holesovice', '1320'],
            ['PP', 'Skirts', 'Sashe', '450'],
            ['PP', 'Bags', 'E-shop', '2500'],
            ['PP', 'Skirts', 'E-shop', '5600'],
            ['PP', 'Dresses', 'Other', '6551'],
            ['Mar', 'Dresses', 'Holesovice', '1000'],
            ['Mar', 'Skirts', 'Holesovice', '3000'],
            ['Mar', 'Ear-rings', 'Holesovice', '2000']]


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

ax = gl.GLAxisItem()
ax.setSize(10,10,10)
w.addItem(ax)
labels = dict(zip(list(set([x[0] for x in mainList])), [2*i for i,j in enumerate(list(set([x[0] for x in mainList])))]))
shops = dict(zip(list(set([x[2] for x in mainList])), [2*i for i,j in enumerate(list(set([x[2] for x in mainList])))]))
items = dict(zip(list(set([x[1] for x in mainList])), [2*i for i,j in enumerate(list(set([x[1] for x in mainList])))]))

emptyL = []
for index, row in enumerate(mainList):
    label = labels.get(row[0])
    shop = shops.get(row[2])
    if index != 0 and mainList[index-1][0] == mainList[index][0] and mainList[index-1][2] == mainList[index][2]:
        emptyL.append([[label, shop, float(mainList[index-1][3])/1000]])
    else:
        emptyL.append([[label, shop, 0]])

colors = {}
for i in items.keys():
    a = round(5*np.random.rand()*np.random.rand(),2)
    b = round(5*np.random.rand()*np.random.rand(),2)
    c = round(5*np.random.rand()*np.random.rand(),2)
    d = round(5*np.random.rand()*np.random.rand(),2)
    colors[i] = (a,b,c,d)

for index, row in enumerate(emptyL):
    size = np.empty((1,1,3))
    size[...,0:2] = 1
    size[...,2] = float(mainList[index][3])/1000
    bg = gl.GLBarGraphItem(np.array([row]), size)
    bg.setColor(colors[mainList[index][1]])
    w.addItem(bg)

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

下面是一张展示上述问题的照片:enter image description here

我不确定这种颜色重叠是如何发生的。我尝试使用抗病毒药物而没有任何成功。任何人都可以建议潜在的问题在哪里?

python python-3.x pyqtgraph
1个回答
0
投票

如果有人遇到类似的问题,很可能是由于错误计算的绘图坐标造成的。

在修改代码之后,它会正确地绘制所有内容:

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

class NestedDict(dict):
    def __getitem__(self, item):
        if item not in self:
            self[item] = NestedDict()
        return super().__getitem__(item)

mainList = [['BM', 'Butterfly', 'E-shop', '1400'],
            ['BM', 'Butterfly', 'Fler', '2450'],
            ['BM', 'Butterfly', 'Holesovice', '2450'],
            ['Climbing presents', 'Ear-rings', 'Holesovice', '136'],
            ['Climbing presents', 'Other jewellery', 'E-shop', '160'],
            ['Climbing presents', 'Other jewellery', 'Other', '112'],
            ['PP', 'Skirts', 'Fler', '1380'],
            ['PP', 'Skirts', 'Holesovice', '1320'],
            ['PP', 'Skirts', 'Sashe', '450'],
            ['PP', 'Bags', 'E-shop', '2500'],
            ['PP', 'Skirts', 'E-shop', '5600'],
            ['PP', 'Dresses', 'Other', '6551'],
            ['Mar', 'Dresses', 'Holesovice', '1000'],
            ['Mar', 'Skirts', 'Holesovice', '3000'],
            ['Mar', 'Ear-rings', 'Holesovice', '2000']]

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

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

labels = dict(zip(list(set([x[0] for x in mainList])), [2*i for i,j in enumerate(list(set([x[0] for x in mainList])))]))
shops = dict(zip(list(set([x[2] for x in mainList])), [2*i for i,j in enumerate(list(set([x[2] for x in mainList])))]))
items = dict(zip(list(set([x[1] for x in mainList])), [2*i for i,j in enumerate(list(set([x[1] for x in mainList])))]))

d = NestedDict()

for tag, item, source, qty in mainList:
    d[tag][source][item] = qty

for key, value in d.items():
    for nkey, nvalue in value.items():
        val = []
        for index, nvalue_ in enumerate(nvalue.values()):
            if index == 0:
                val.append(0)
            else:
                val.append(float(list(nvalue.values())[index-1])/1000)
            size = np.empty((1,1,3))
            size[...,0:2] = 1
            size[...,2] = float(list(nvalue.values())[index])/1000
            bg = gl.GLBarGraphItem(np.array([[[labels[key], shops[nkey], sum(val)]]]), size)
            bg.setColor((round(5*np.random.rand()*np.random.rand(),2),
                         round(5*np.random.rand()*np.random.rand(),2),
                         round(5*np.random.rand()*np.random.rand(),2),
                         round(5*np.random.rand()*np.random.rand(),2)))
            w.addItem(bg)

if __name__ == '__main__':
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()
© www.soinside.com 2019 - 2024. All rights reserved.