如何在pyqtgraph中为单个PlotItem设置背景颜色?

时间:2019-06-05 13:05:31

标签: python pyqtgraph

我想为GraphicsLayoutWidget中的一系列子图(即PlotItem对象)设置不同的背景颜色,如下所示:

import pyqtgraph as pg

win = pg.GraphicsLayoutWidget()
win.resize(1200,600)
win.setBackground('w')

color_list = [(r1,g1,b1), (r2,g2,b2), ...]
for j in range(10):
    p = win.addPlot(title="Ch #"+str(j))
    p.plot(y=Y_mean, pen=(0,0,0))
    p.setBackgroundColor(color_list[j]) # <---  Or something similar to that

这引起了

AttributeError: 'PlotItem' object has no attribute 'setBackgroundColor'

我找不到在类pyqtgraph.PlotItem或任何继承的类中设置此属性的方法。

1 个答案:

答案 0 :(得分:2)

您必须使用setBackgroundColor()方法在PlotItem的ViewBox中设置颜色:

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

if __name__ == "__main__":
    import sys

    app = QtGui.QApplication(sys.argv)
    win = pg.GraphicsLayoutWidget()
    win.resize(1200, 600)
    win.setBackground("w")

    color_list = [(100, 10, 34), (20, 30, 40), (40, 146, 10)]
    for j, color in zip(range(3), color_list):
        p = win.addPlot(title="Ch #{}".format(j))
        p.plot(y=np.random.rand(200), pen=(0, 0, 0))
        vb = p.getViewBox()
        vb.setBackgroundColor(color)
    win.show()
    sys.exit(app.exec_())

enter image description here