Adjusting plt chart background when user changes theme

Hello ! Ideally, when a user switches between light and dark mode in the hamburger menu, I would like to adjust accordingly the background of the matplotlib charts I display.

The easiest way would be if I could pick up the theme chosen by the user in my code and then make use of plt.style.use(‘dark_background’). Unfortunately I don’t know how to pick up on that change made by the user…

Is there a good way to achieve my goal ?

Thanks !

You can do this to get the hexcode of the background color:

st.get_option('theme.backgroundColor')

Hmm thanks. Playing with it, I had the impression I was picking up the backgroundColor from my config.toml, rather than that corresponding to what was chosen by the user

Oh, i see. To be fair, I didn’t check if it also picks up the value of the default light and dark theme. Honestly, I am not sure about this and always thought it would pick it up. You may give it a try. Maybe someone else will come up with a better solution.