Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
144 views
in Technique[技术] by (71.8m points)

python - How to recover matplotlib defaults after setting stylesheet

In an ipython notebook, I used a matplotlib stylesheet to change the look of my plots using

from matplotlib.pyplot import *
%matplotlib inline
style.use('ggplot')

My version of matplotlib is 1.4.0. How do I go back to the default matplotlib styling? I tried all the available styles in

print style.available 

but there doesn't seem to be a "default" option. I also tried

matplotlib.rcdefaults() 

For some reason, this gives me a gray background. It also changes the text from gray (ggplot style) to black, which may be the default, but also could be another random style.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should be able to set it back to default by:

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

In ipython, things are a little different, especially with inline backend:

In [1]:

%matplotlib inline
In [2]:

import matplotlib as mpl
import matplotlib.pyplot as plt
In [3]:

inline_rc = dict(mpl.rcParams)
In [4]:

plt.plot(range(10))
Out[4]:
[<matplotlib.lines.Line2D at 0x72d2510>]

enter image description here

In [5]:

mpl.rcParams.update(mpl.rcParamsDefault)
plt.plot(range(10))
Out[5]:
[<matplotlib.lines.Line2D at 0x7354730>]

enter image description here

In [6]:

mpl.rcParams.update(inline_rc)
plt.plot(range(10))
Out[6]:
[<matplotlib.lines.Line2D at 0x75a8e10>] 

enter image description here

Basically, %matplotlib inline uses its own rcParams. You can grab that from the source, but the arguably easier way is probably just save the rcParams as inline_rc after %matplotlib inline cell magic in this example, and reuse that later.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...