#StackBounty: #python-3.x #matplotlib #pyqt5 Why Matplotlib style do not update facecolor PyQt5?

Bounty: 50

I am using matplotlib styles and I try to change the styles dynamically when the style is chosen in the combobox. I am using Matplotlib 2.2.3, Python 3.6.6, PyQt5, Windows 10. But when I choose the dark_background style, the figure facecolor and the axes facecolor do not change. Here an animation:
dark_bakground error
This is the code:

File IHMDrawDates.py generated with pyuic5:

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MplMainWindow(object):
    def setupUi(self, MplMainWindow):
        MplMainWindow.setObjectName("MplMainWindow")
        MplMainWindow.resize(628, 416)
        self.centralwidget = QtWidgets.QWidget(MplMainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout_2 = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout_2.setObjectName("gridLayout_2")
        self.mpl = MplWidgetTest(self.centralwidget)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.mpl.sizePolicy().hasHeightForWidth())
        self.mpl.setSizePolicy(sizePolicy)
        self.mpl.setObjectName("mpl")
        self.gridLayout_2.addWidget(self.mpl, 0, 0, 1, 1)
        self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
        self.groupBox.setMaximumSize(QtCore.QSize(95, 16777215))
        self.groupBox.setObjectName("groupBox")
        self.gridLayout = QtWidgets.QGridLayout(self.groupBox)
        self.gridLayout.setObjectName("gridLayout")
        self.buttonDrawDate = QtWidgets.QPushButton(self.groupBox)
        self.buttonDrawDate.setMaximumSize(QtCore.QSize(75, 16777215))
        self.buttonDrawDate.setObjectName("buttonDrawDate")
        self.gridLayout.addWidget(self.buttonDrawDate, 1, 0, 1, 1)
        self.buttonErase = QtWidgets.QPushButton(self.groupBox)
        self.buttonErase.setMaximumSize(QtCore.QSize(75, 16777215))
        self.buttonErase.setObjectName("buttonErase")
        self.gridLayout.addWidget(self.buttonErase, 2, 0, 1, 1)
        spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
        self.gridLayout.addItem(spacerItem, 3, 0, 1, 1)
        self.comboTema = QtWidgets.QComboBox(self.groupBox)
        self.comboTema.setObjectName("comboTema")
        self.gridLayout.addWidget(self.comboTema, 0, 0, 1, 1)
        self.gridLayout_2.addWidget(self.groupBox, 0, 1, 1, 1)
        MplMainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MplMainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 628, 21))
        self.menubar.setObjectName("menubar")
        MplMainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MplMainWindow)
        self.statusbar.setObjectName("statusbar")
        MplMainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MplMainWindow)
        QtCore.QMetaObject.connectSlotsByName(MplMainWindow)

    def retranslateUi(self, MplMainWindow):
        _translate = QtCore.QCoreApplication.translate
        MplMainWindow.setWindowTitle(_translate("MplMainWindow", "MainWindow"))
        self.groupBox.setTitle(_translate("MplMainWindow", "GroupBox"))
        self.buttonDrawDate.setText(_translate("MplMainWindow", "Draw"))
        self.buttonErase.setText(_translate("MplMainWindow", "Erase"))

from mplwidgettest import MplWidgetTest

mplwidgettest.py file that contains the method ‘setTema’ to update matplotlib’s style. When I print the variable rcParams it contains axes.facecolor: black but it does not apply

from PyQt5.QtWidgets import QSizePolicy, QWidget, QVBoxLayout
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import (
    FigureCanvasQTAgg as FigureCanvas,
    NavigationToolbar2QT as NavigationToolbar)
import matplotlib as mplib


class MplCanvas(FigureCanvas):
    """Class to represent the FigureCanvas widget"""
    def __init__(self):
        # setup Matplotlib Figure and Axis
        mplib.rcParams.update(mplib.rcParamsDefault)
        mplib.style.use('bmh')
        self.fig = Figure()
        self.ax = self.fig.add_subplot(111)
        # initialization of the canvas
        FigureCanvas.__init__(self, self.fig)
        # we define the widget as expandable
        FigureCanvas.setSizePolicy(self,
                                   QSizePolicy.Expanding,
                                   QSizePolicy.Expanding)
        # notify the system of updated policy
        FigureCanvas.updateGeometry(self)

    def setTema(self, tema='classic'):
        print(tema)
        mplib.style.use(tema)
        print(mplib.rcParams)
        mplib.rcParams.update(mplib.rcParams)


class MplWidgetTest(QWidget):
    """Widget defined in Qt Designer"""
    def __init__(self, parent=None):
        # initialization of Qt MainWindow widget
        QWidget.__init__(self, parent)
        # set the canvas to the Matplotlib widget
        self.canvas = MplCanvas()
        # create a NavigatioToolbar
        self.ntb = NavigationToolbar(self.canvas, self)
        # create a vertical box layout
        self.vbl = QVBoxLayout()
        # add mpl widget to vertical box
        self.vbl.addWidget(self.canvas)
        # add NavigationToolBar to vertical box
        self.vbl.addWidget(self.ntb)
        # set the layout to th vertical box
        self.setLayout(self.vbl)

mainMplWidget.py File that calls the two previous files, and contains the method ‘cambiarTema’ that changes the matplotlib’s style

import sys
from IHMDrawDates import Ui_MplMainWindow
from PyQt5.QtWidgets import QMainWindow, QApplication
from PyQt5.QtGui import QCursor
import numpy as np


class DesignerMainWindow(QMainWindow, Ui_MplMainWindow):

    def __init__(self, parent=None):
        super(DesignerMainWindow, self).__init__(parent)
        self.setupUi(self)
        self.posX = []
        self.posY = []
        temas = ['bmh', 'classic', 'grayscale', 'seaborn-bright',
                 'ggplot', 'dark_background']
        self.comboTema.addItems(temas)
        # connect the signals with the slots
        self.buttonDrawDate.clicked.connect(self.drawDate)
        self.buttonErase.clicked.connect(self.eraseDate)
        self.comboTema.currentIndexChanged.connect(self.cambiarTema)

    def cambiarTema(self):
        tema = self.comboTema.currentText()
        self.mpl.canvas.setTema(str(tema))
        self.mpl.canvas.ax.clear()
        self.mpl.canvas.draw()
        self.mpl.canvas.flush_events()

    def drawDate(self):
        x = np.arange(0, 100, 0.1)
        y = np.sin(x)
        self.mpl.canvas.ax.plot(x, y)
        self.mpl.canvas.ax.relim()
        self.mpl.canvas.ax.autoscale(True)
        self.mpl.ntb.update()
        self.mpl.ntb.push_current()
        self.mpl.canvas.draw()

    def eraseDate(self):
        self.mpl.canvas.ax.clear()
        self.mpl.ntb.update()
        self.mpl.ntb.push_current()
        self.mpl.canvas.draw()

if __name__ == '__main__':
    app = 0
    app = QApplication(sys.argv)
    dmw = DesignerMainWindow()
    # show it
    dmw.show()
    sys.exit(app.exec_())


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.