python-3.xmatplotlib

Properly reset matplotlib.rcParams dictionary to its original defaults


This answer mentions that either

fig = plt.figure()
fig.patch.set_facecolor('black')

or

plt.rcParams['figure.facecolor'] = 'black'

will change the value in the rcParams dictionary for the key 'figure.facecolor'.

Suppose that my script has made several changes to the values in a nondeterministic way based on user interaction, and I want to undo all of that and go back to matplotlib's default parameters and behavior.

In the beginning of the script I could check matplotlib.rcParams and store either the whole dictionary, or values for certain keys, and then restore them one at a time or with the .update() method, but I don't know if that's wise because I don't know how else the matplotlib.RcParams instance is used (it's not just a dictionary). It does have a .setdefault() method but I can't understand what help returns on that:

Help on method setdefault in module collections.abc:

setdefault(key, default=None) method of matplotlib.RcParams instance
    D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D

Is there some kind of restore the original default values feature, or should I just wing-it by updating the whole thing with the copy that I've stored?


Solution

  • Per my understanding and answers to How to recover matplotlib defaults after setting stylesheet you should be able to do this:

    import matplotlib
    matplotlib.rcParams.update(matplotlib.rcParamsDefault)
    

    You could also check the site-packages/matplotlib/mpl-data folder for the file named matplotlibrc. It should have the entire default values there.