How to disable xkcd in a matplotlib figure?

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

What's happening is that the rc parameters are being changed by plt.xkcd() (which is basically how it works).

plt.xkcd() saves the current rc params returns a context manager (so that you can use a with statement) that resets them at the end. If you didn't hold on to the context manager that plt.xkcd() returns, then you can't revert to the exact same rc params that you had before.

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

However, you can revert back to the default rcParams by calling plt.rcdefaults(). You just won't retain any specific changes you made before calling plt.xkcd().


I see this in the doc, does it 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 matplotlib.pyplot.xkcd's code and see how they generate the context manager that allows reversing the config