5

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?

uhoh
  • 3,713
  • 6
  • 42
  • 95
  • Have you checked in the /site-packages/matplotlib/mpl-data folder for the file named matplotlibrc? Per my understanding, it should have the entire default values there. – Rahul P Dec 25 '19 at 00:59
  • I'm not sure how that helps. I don't want to look them up manually like that, I would like my script to restore the defaults itself. – uhoh Dec 25 '19 at 01:01

1 Answers1

16

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.

uhoh
  • 3,713
  • 6
  • 42
  • 95
Rahul P
  • 2,493
  • 2
  • 17
  • 31
  • A quick check shows `update(matplotlib.rcParamsDefault)` does just what I need, I'll give it a try *in situ* and check it again, *thanks!* – uhoh Dec 25 '19 at 01:07
  • @uhoh Did this method work? I didn't see my answer marked as the accepted answer so I just wanted to follow up to make sure it worked. – Rahul P Dec 26 '19 at 21:09
  • No I haven't field-tested it yet. Right now the only verification I have that this is correct is a quick test of my own and a single vote by someone else. The "Per my understanding you should be able to...." indicates that you feel this might work but are also less than certain it is correct and safe, and you haven't linked to any documentation showing this to be a proper and accepted technique. In this case acceptance is premature. Feel free to ping me again in a week though, thanks! – uhoh Dec 26 '19 at 23:05
  • 1
    @uhoh checking back on this :) – Rahul P Jan 06 '20 at 22:15
  • 1
    I found some confirmation that this is probably okay to do and included in your answer. Thanks for the ping! – uhoh Jan 06 '20 at 22:41
  • When I include this in a notebook (Jupyter Lab v3.2.1) figures no longer display until the kernel is reset. – Paidoo Mar 06 '23 at 11:57