How to change Matplotlib Stylesheets programmatically?

1

I’m trying to change/toggle between two available matplotlib styles, “dark_background” & “classic”, based on user feedback through a gui button status. I tried using matplotlib.pyplot.style.use() and matplotlib.style.use() to change matplotlib styles during runtime but it is not working: plot background color does not change, no error msg.

however, plt.style.use("dark_background") and plt.style.use("classic") work fine when they are set in a Matplotlib class init function. plot background color change.

here is an example that replicate this issue. on_change() is the call back function where i’m trying to change style programatically:

import sys
import numpy as np
from scipy.stats import norm
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
from matplotlib.backends.backend_qtagg import FigureCanvas
from matplotlib.backends.backend_qtagg import NavigationToolbar2QT
from PySide6.QtCore import Slot
from PySide6.QtWidgets import (QApplication, QWidget, QDoubleSpinBox, QVBoxLayout, QHBoxLayout,)


class PlotWidget(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.style = "Dark"
        plt.style.use("dark_background")
        # plt.style.use("classic")
        self.view = FigureCanvas(Figure(figsize=(5, 3)))
        self.axes = self.view.figure.subplots()
        self.toolbar = NavigationToolbar2QT(self.view, self)
        self.mu_input = QDoubleSpinBox()
        self.std_input = QDoubleSpinBox()
        self.mu_input.setPrefix("μ: ")
        self.std_input.setPrefix("σ: ")
        self.std_input.setValue(10)
        input_layout = QHBoxLayout()
        input_layout.addWidget(self.mu_input)
        input_layout.addWidget(self.std_input)
        vlayout = QVBoxLayout()
        vlayout.addWidget(self.toolbar)
        vlayout.addWidget(self.view)
        vlayout.addLayout(input_layout)
        self.setLayout(vlayout)
        self.mu_input.valueChanged.connect(self.on_change)
        self.std_input.valueChanged.connect(self.on_change)
        self.on_change()

    @Slot()
    def on_change(self):
        """ Update the plot with the current input values """
        mu = self.mu_input.value()
        std = self.std_input.value()
        x = np.linspace(-100, 100)
        y = norm.pdf(x, mu, std)
        self.axes.clear()
        self.axes.plot(x, y)
        self.view.draw()

        if self.style == "Dark":
            plt.style.use("classic")
            self.style = "Light"
        else:
            plt.style.use("dark_background")
            self.style = "Dark"
        self.view.draw()

if __name__ == "__main__":

    app = QApplication(sys.argv)
    w = PlotWidget()
    w.show()
    sys.exit(app.exec())

All artist properties are only determined at creation time (and if not, it’s probably a bug.) Changing the style thus will not have any effect on existing figures or artists. You would need to re-plot or update the properties if you want to change styles on an existing figure.