setBackgroundColor() removes axis and grid lines

1,463 views
Skip to first unread message

Diziet Asahi

unread,
Feb 24, 2016, 11:48:09 AM2/24/16
to pyqtgraph
Hi (again)

I have a software containing a pyqtgraph plot. In certain conditions, I want to change the background color of the graph (to alert the user). But I noticed that doing it the way I was removes the left axis line and the grid lines. What am I doing wrong?

Here is a self-contained example:

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

pg
.setConfigOption('background', (255, 255, 200))
pg
.setConfigOption('foreground', 'k')

app
= QtGui.QApplication([])
mw
= QtGui.QMainWindow()
mw
.setWindowTitle('pyqtgraph example: PlotWidget')
mw
.resize(800, 800)
cw
= QtGui.QWidget()
mw
.setCentralWidget(cw)
l
= QtGui.QVBoxLayout()
cw
.setLayout(l)

pw
= pg.PlotWidget(name='Plot1')  # giving the plots names allows us to link their axes together
p1
= pw.plotItem.plot([1, 2, 5], pen='r')
pw
.plotItem.showGrid(y=True)

def changeBackground():
   
global pw
    pw
.plotItem.vb.setBackgroundColor(np.random.randint(low=0, high=255, size=(3,)))

b
= QtGui.QPushButton("change background color")
b
.clicked.connect(changeBackground)

l
.addWidget(b)
l
.addWidget(pw)
mw
.show()

# Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
   
import sys

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



shaera...@gmail.com

unread,
Feb 25, 2016, 4:11:30 PM2/25/16
to pyqtgraph
do 

pw.setBackground(np.random.randint(low=0, high=255, size=(3,)))

instead of 

pw.plotItem.vb.setBackgroundColor(np.random.randint(low=0, high=255, size=(3,)))

Diziet Asahi

unread,
Feb 25, 2016, 4:40:07 PM2/25/16
to pyqtgraph
Hi, thanks so much for having a look at this.
Unfortunately, using
pw.setBackground(np.random.randint(low=0, high=255, size=(3,)))

changes the background color of the whole window, not just the plot area as I want (I have several plots on the same window).

-DA

Vincent Le Saux

unread,
Feb 26, 2016, 12:28:38 AM2/26/16
to pyqtgraph
Hi,

I aldready met this problem before. I did not find the time yet to fix this issue in the source code, but I found a problem to circumvent the problem.

When the backgound of the viewbox is changed, the axis goes behind it (don't know why though). All you have to do is change the ZValue of the axis to force them to go on the top level. This code should work

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

pg
.setConfigOption('background', (255, 255, 200))
pg
.setConfigOption('foreground', 'k')

app
= QtGui.QApplication([])
mw
= QtGui.QMainWindow()
mw
.setWindowTitle('pyqtgraph example: PlotWidget')
mw
.resize(800, 800)
cw
= QtGui.QWidget()
mw
.setCentralWidget(cw)
l
= QtGui.QVBoxLayout()
cw
.setLayout(l)

pw
= pg.PlotWidget(name='Plot1')  # giving the plots names allows us to link their axes together
p1
= pw.plotItem.plot([1, 2, 5], pen='r')
pw
.plotItem.showGrid(y=True)

def changeBackground():
   
global
pw
    pw
.plotItem.vb.setBackgroundColor((255, 255, 255, 255))
    top
= pw.plotItem.getAxis("top")
    bottom
= pw.plotItem.getAxis("bottom")
    left
= pw.plotItem.getAxis("left")
    right
= pw.plotItem.getAxis("right")
    top
.setZValue(0)
    bottom
.setZValue(0)
    left
.setZValue(0)
    right
.setZValue(0)


b
= QtGui.QPushButton("change background color")
b
.clicked.connect(changeBackground)

l
.addWidget(b)
l
.addWidget(pw)
mw
.show()

# Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
   
import sys

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



Vincent

Diziet Asahi

unread,
Feb 26, 2016, 4:20:28 AM2/26/16
to pyqtgraph
Hey Vincent,

Thanks again for your help. I had seen your fix searching on the web before, but for some reason it was not working for me. But obviously I must have been doing something stupid. It works like a charm now.

Thanks,
-DA
Reply all
Reply to author
Forward
0 new messages