1

I would like to toggle my plot between two different styles. But I'm having a problem finding out how to change the style after the plot is created.

import matplotlib.pyplot as plt

plt.style.use("dark_background")  # create plot in dark format

fig, (axes1, axes2) = plt.subplots(2)

axes1.plot(range(5))
axes2.plot(range(4, -1, -1))

"""other code here"""

if True:  # condintion for changing style later
    plt.style.use("default")  # depending on code, change plot style back

plt.show()

If using the plt.style.use function doesn't work, is it possible using the rcParams?

2
  • 2
    No, one cannot change the style after the axes is created. In such case one would need to change each element individually using .set_color() and similar functions. Best use the style in a context, with plt.style.context('dark_background'): Commented Oct 28, 2019 at 18:50
  • Okay thanks, the context might be hard since I call different parts of the plot in different functions, but I think i'll just try doing it with the individual set_color functions. Thanks! Commented Oct 28, 2019 at 19:47

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.