Hi,
I can tell you why it doesn't work, but unfortunately can't immediately suggest a workaround. Here's some demo code which lists out the drawing order for all the plot items:
#!/usr/bin/env python3
from PyQt5 import QtWidgets
import pyqtgraph as pg
import numpy as np
class TestPlot(pg.GraphicsLayoutWidget):
def __init__(self):
super().__init__()
self.plotItem = self.addPlot()
self.plotItem.getViewBox().setBackgroundColor((192,192,192))
self.plotItem.showGrid(True)
self.plotItem.getAxis("bottom").setPen({"color": (255, 0, 0), "width": 5})
plot = self.plotItem.plot(np.random.rand(10), pen={"color": (0, 0, 0), "width": 3})
def format_graphicsitem_tree(gitem, newline=False):
returnstring = "{}: {}\n".format(gitem.zValue(), gitem)
for child in gitem.childItems():
returnstring += " {}\n".format(format_graphicsitem_tree(child).replace("\n", "\n "))
return returnstring.rpartition("\n")[0]
print(format_graphicsitem_tree(self.plotItem))
def main():
import sys
app = QtWidgets.QApplication(sys.argv)
mainwindow = TestPlot()
mainwindow.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Output:
0.0: <pyqtgraph.graphicsItems.PlotItem.PlotItem.PlotItem object at 0x7fc3371b13a8>
-1000.0: <pyqtgraph.graphicsItems.AxisItem.AxisItem object at 0x7fc334166048>
0.0: <PyQt5.QtWidgets.QGraphicsTextItem object at 0x7fc3341660d8>
-1000.0: <pyqtgraph.graphicsItems.AxisItem.AxisItem object at 0x7fc334166168>
0.0: <PyQt5.QtWidgets.QGraphicsTextItem object at 0x7fc3341661f8>
-1000.0: <pyqtgraph.graphicsItems.AxisItem.AxisItem object at 0x7fc334166288>
0.0: <PyQt5.QtWidgets.QGraphicsTextItem object at 0x7fc334166318>
-1000.0: <pyqtgraph.graphicsItems.AxisItem.AxisItem object at 0x7fc3341663a8>
0.0: <PyQt5.QtWidgets.QGraphicsTextItem object at 0x7fc334166438>
-100.0: <pyqtgraph.graphicsItems.ViewBox.ViewBox.ViewBox object at 0x7fc3371b1708>
-1000000.0: <PyQt5.QtWidgets.QGraphicsRectItem object at 0x7fc3371b1828>
0.0: <pyqtgraph.graphicsItems.ViewBox.ViewBox.ChildGroup object at 0x7fc3371b1798>
0.0: <pyqtgraph.graphicsItems.PlotDataItem.PlotDataItem object at 0x7fc3341665e8>
0.0: <pyqtgraph.graphicsItems.PlotCurveItem.PlotCurveItem object at 0x7fc3341764c8>
0.0: <pyqtgraph.graphicsItems.ScatterPlotItem.ScatterPlotItem object at 0x7fc334176288>
1000000000.0: <PyQt5.QtWidgets.QGraphicsRectItem object at 0x7fc3371b18b8>
0.0: <PyQt5.QtWidgets.QGraphicsRectItem object at 0x7fc3371b1948>
0.0: <pyqtgraph.graphicsItems.ButtonItem.ButtonItem object at 0x7fc3371b15e8>
0.0: <pyqtgraph.graphicsItems.LabelItem.LabelItem object at 0x7fc3341664c8>
0.0: <PyQt5.QtWidgets.QGraphicsTextItem object at 0x7fc334166558>
The scene is laid out in that order. You can also see each GraphicsItem can have child items. Setting a zValue rearranges the order of sibling items (at the same indent level) only. So the background (QGraphicsRectItem with zValue -1e6) is a child of the ViewBox, as are the PlotItems. The AxisItems are siblings of the ViewBox. So changing the ZValue of an AxisItem can push the grid lines below or above all of those, but you can't fit it in between just by changing the zValue.
You may be able to do some magic by swapping graphicsItem parents around (setParent), but you may have issues with position and scaling. You could also see if you can overlay two linked ViewBoxes, one holding the background (but no axes etc) and another holding the plot and axis labels as normal.
Patrick