Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable xkcd in a matplotlib figure?

Tags:

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.

like image 827
alwbtc Avatar asked Mar 09 '14 16:03

alwbtc


2 Answers

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

like image 133
Joe Kington Avatar answered Sep 20 '22 15:09

Joe Kington


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

like image 30
Valentin Lorentz Avatar answered Sep 17 '22 15:09

Valentin Lorentz