hko
hko

Reputation: 169

How to change the Figure Canvas Color (PyQt)

How can I set the background color of my figures? I would like to use FigureCanvasQTAgg.

from PyQt5 import QtCore, QtWidgets, QtWidgets
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg
from matplotlib.backends.backend_qt5 import NavigationToolbar2QT
from matplotlib.figure import Figure


class FigureCanvas(FigureCanvasQTAgg):
    def __init__(self, parent=None, width=5, height=4, dpi=100):
        self.fig = Figure(figsize=(width, height), dpi=dpi)
        FigureCanvasQTAgg.__init__(self, self.fig)
        self.setParent(parent)
        FigureCanvas.setSizePolicy(
            self,
            QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding,
        )
        FigureCanvas.updateGeometry(self)   

Upvotes: 0

Views: 4531

Answers (2)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339580

Under usual circumstances there is no difference between the figure background and the canvas background. So you may set the figure background to some color

self.fig.set_facecolor("blue")

In case you do not want to change the figure's background color (I can't think of any reason right now, but maybe there is one use case), you would need to make the figure background transparent, such that the canvas background color can shine through,

self.fig.set_facecolor("none")
self.setStyleSheet("background-color:blue;")

This answer of mine gives some insight and also a test code to play with.

Upvotes: 3

Grzegorz Oledzki
Grzegorz Oledzki

Reputation: 24281

I have never used matplotlib, nor tested this answer, but guessing here...

self.setStyleSheet("background-color:red;")

?

(based on some other answer)

Upvotes: 1

Related Questions