【问题标题】:How to set the background color for individual PlotItem in pyqtgraph?如何在 pyqtgraph 中为单个 PlotItem 设置背景颜色?
【发布时间】:2019-06-06 18:23:15
【问题描述】:

我想在 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 类或其任何继承类中设置此属性。

【问题讨论】:

    标签: python pyqtgraph


    【解决方案1】:

    您必须使用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_())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-26
      • 1970-01-01
      • 2010-12-11
      • 2020-12-14
      • 2019-11-02
      • 2022-11-18
      • 1970-01-01
      • 2018-03-31
      相关资源
      最近更新 更多