How to disable xkcd in matplotlib shape?

You turn on the xkcd style:

import matplotlib.pyplot as plt plt.xkcd() 

But how to disable it?

I'm trying to:

 self.fig.clf() 

But that will not work.

+7
python matplotlib
source share
3 answers

I see this in the document, does this help?

 with plt.xkcd(): # This figure will be in XKCD-style fig1 = plt.figure() # ... # This figure will be in regular style fig2 = plt.figure() 

If not, you can look at the matplotlib.pyplot.xkcd code and see how they generate a context manager that allows you to change the configuration

+9
source share

In a nutshell, either use the context manager, as mentioned by @Valentin, or call plt.rcdefaults() afterwards.

What happens when the rc parameters change to plt.xkcd() (which basically works).

plt.xkcd() saves the current parameters. rc returns a context manager (so you can use the with statement), which resets them at the end. If you did not save the context manager returned by plt.xkcd() , then you cannot return to the same rc parameters that you had before.

In other words, let's say you did something like plt.rc('lines', linewidth=2, color='r') before calling plt.xkcd() . If you did not do with plt.xkcd(): or manager = plt.xkcd() , then the state of rcParams after calling plt.rc will be lost.

However, you can revert to the standard rcParams by calling plt.rcdefaults() . You simply do not save any specific changes that you made before calling plt.xkcd() .

+19
source share

You can try

 manager = plt.xkcd() # my xkcd plot here mpl.rcParams.update(manager._rcparams) 

this should reset the previous state, emulation of the context manager. Obviously, it does not have all the functions for the context manager, for example, reset in case of exceptions, etc.

Or, without using the context manager functions of the context manager

 saved_state = mpl.rcParams.copy() mpl.xkcd() # my xkcd plot here mpl.rcParams.update(saved_state) 
+3
source share

All Articles