alwbtc
alwbtc

Reputation: 29485

How to disable xkcd in a matplotlib figure?

You turn on xkcd style by:

import matplotlib.pyplot as plt
plt.xkcd()

But how to disable it?

I try:

self.fig.clf()

But it won't work.

Upvotes: 24

Views: 13336

Answers (7)

hhp
hhp

Reputation: 1

simply use ,

plt.xkcd(False)

that worked good for me

Upvotes: 0

Umar Asghar
Umar Asghar

Reputation: 4064

Just use that

import matplotlib.pyplot as plt

plt.rcdefaults()
# before showing the plot

Upvotes: 7

coder boy
coder boy

Reputation: 3

you can simply use plt.rcdefaults or pyplot.rcdefaults before plt.show().

it will surely reset the rcparams to defaults. I tried and it worked.

Upvotes: 0

user13579058
user13579058

Reputation: 11

add this to the beginning of your code

import matplotlib as mpl

mpl.rcParams.update(mpl.rcParamsDefault)

Upvotes: 0

user3622450
user3622450

Reputation: 135

You could try

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

this should reset the previous state, emulating the context manager. Obviously, it has not all the features for a context manager, e.g., the reset in case of exceptions, etc.

Or, w/o messing with the internals of the context manager

saved_state = mpl.rcParams.copy()
mpl.xkcd()
# my xkcd plot here
mpl.rcParams.update(saved_state)

Upvotes: 7

Valentin Lorentz
Valentin Lorentz

Reputation: 9753

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

Upvotes: 18

Joe Kington
Joe Kington

Reputation: 284830

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().

Upvotes: 42

Related Questions