How to dynamically change the sheet type theme during runtime

76 Views Asked by At

I am working on dynamically changing matplotlib style sheet during runtime. I have initialized a class for matplotlib widget and chosen two styles from matplotlib style sheet reference - let's say default and dark_background styles.

Refer link for matplotlib style sheet : https://matplotlib.org/stable/gallery/style_sheets/style_sheets_reference.html.

I have initialized a push button configured to switch between the two styles.

When the button is toggled, it gets detected. I have a function with input from the push button toggle and its input is used to enable the matplotlib style sheet.

When the code is executed, when I toggle the push button, the matplotlib style sheet change does not apply.

Here is the code snippet for reference:

def style_select(self):
    if self.style == "dark":
        plt.style.use('dark_background')
    else:
        plt.style.use('default')
    self.canvas.draw()
    pass
2

There are 2 best solutions below

0
Hannibal On

you need to place your cutom mplstyle file in the same directory where Matplotlib stores its mplstyle files:./Lib/site-packages/matplotlib/mpl-data/stylelib then set stylesheet in your code using the cutom mplstyle file name without its extension: plt.style.use('mystyle')

0
Hannibal On

plt.style.use() works only when the widget class is first instanciated (when the plot/figure is initially created). it works also when a subplot is added. you change the color scheme using the following instead for the current object:

*.canvas.figure.set_edgecolor("white")
*.canvas.figure.set_facecolor("white")
*.canvas.figure.axes.axe.set_facecolor("white")

*.canvas.figure.set_edgecolor("black")
*.canvas.figure.set_facecolor("black")
*.canvas.figure.axes.axe.set_facecolor("black")