0

I see I'm not the only one having a problem with background color in pyqtgraph - I'm writing a QGIS software plugin which has an additional dialog box with a graph. I'm trying to set the background color and it loads only after I reload the plugin using QGIS Plugin Reloader plugin (this plugin is made for people developing plugins, so after any change in the code, you refresh and have a new one loaded into QGIS. It is not used by a common user).

My piece of code below:

import pyqtgraph

...

def prepareGraph(self): # loads on button click

    self.graphTitle = 'Graph one'

    # prepare data - simplified, but data display correctly
    self.y = something
    self.x = something_else

    self.buildGraph() 


def buildGraph(self):
    """ Add data to the graph """
    pyqtgraph.setConfigOption('background', (230,230,230))
    pyqtgraph.setConfigOption('foreground', (100,100,100))
    dataColor = (102,178,255)
    dataBorderColor = (180,220,255)
    barGraph = self.graph.graphicsView
    barGraph.clear()
    barGraph.addItem(pyqtgraph.BarGraphItem(x=range(len(self.x)), height=self.y, width=0.5, brush=dataColor, pen=dataBorderColor))
    barGraph.addItem(pyqtgraph.GridItem())
    barGraph.getAxis('bottom').setTicks([self.x])
    barGraph.setTitle(title=self.graphTitle)

    self.showGraph()


def showGraph(self):
    self.graph.show()

Interesting thing is that all parts of buildGraph() load without any issue, (even the foreground color!) only the background color won't.

Is this a known bug or there is a difference between setting fore- and background color? Linked question didn't help me to solve this problem.

pyqtgraph==0.9.10 PyQt4==4.11.4 Python 2.7.3

adamczi
  • 343
  • 1
  • 7
  • 24

1 Answers1

0

The pyqtgraph documentation says about setConfigOption settings, that:

Note that this must be set before creating any widgets

In my code I have

def buildGraph(self):

    pyqtgraph.setConfigOption('background', (230,230,230))
    pyqtgraph.setConfigOption('foreground', (100,100,100))

    barGraph = self.graph.graphicsView

and that's what I thought is the "before" place, but it's creation of an object, not widget. One should write setConfigOption inside a class, that is responsible for storing pyqtgraph object. In my case it was __init__ function inside a separate file creating a separate dialog box:

from PyQt4 import QtGui, QtCore from plugin4_plot_widget import Ui_Dialog from plugin4_dialog import plugin4Dialog import pyqtgraph

class Graph(QtGui.QDialog, Ui_Dialog):
    def __init__(self):
        super(Graph, self).__init__()
        pyqtgraph.setConfigOption('background', (230,230,230))
        pyqtgraph.setConfigOption('foreground', (100,100,100))        

    self.setupUi(self)

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    main = Graph()
    main.show()
    sys.exit(app.exec_())
adamczi
  • 343
  • 1
  • 7
  • 24