python - 如何在 pyqtgraph 中为单个 PlotItem 设置背景颜色?
问题描述
我想在 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
或它的任何继承类中设置此属性。
解决方案
您必须使用以下方法在 PlotItem 的 ViewBox 中设置颜色setBackgroundColor()
:
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_())
推荐阅读
- npm - 如何将 npm 包发布/部署到自定义工件
- javascript - Tensorflow.js:检查目标时出错...预期层具有 n 维
- flutter - 停止音频循环(audioplayers 包)
- javascript - 遍历数组时从状态访问值时遇到问题
- java - 已下载 Maven 依赖项,但构建失败并出现 StackOverflowError
- reactjs - 如何检查 API 的响应 JSON 是否为空或有错误?
- ios - 如何在 iOS 的 MaterialDesign Overlined Textfield 中更改文本的顶部和底部填充?
- angular - 离子选择设置起始值
- java - 在使用 Spring Data MongoDB 的事务中调用两个不同 ReactiveMongoRepository 中的方法?
- css - 选择正确的元素