python seaborn to reset back to the matplotlib

放肆的年华 提交于 2019-12-03 09:24:14

Yes, call seaborn.reset_orig.

To refresh Matplotlib's configuration side effects often encountered with Seaborn:

import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns

Run this:

import importlib
importlib.reload(mpl); importlib.reload(plt); importlib.reload(sns)

For old Python2 code:

import imp
imp.reload(mpl); imp.reload(plt); imp.reload(sns)

Note: None of the following correctly restores the state of matplotlib:

  • sns.reset_orig()
  • sns.reset_defaults()
  • mpl.rcParams.update(mpl.rcParamsDefault)

You can save the rcParams you want, before changing the style with seaborn (note that seaborn no longer changes the rcParams upon import):

import matplotlib as mpl

my_params = mpl.rcParams

# apply some change to the rcparams here

mpl.rcParams.update(my_params)

Note that both these

mpl.rcParams.update(mpl.rcParamsOrig)
mpl.rcParams.update(mpl.rcParamsDefault)

restores almost all rcParams to their default value. The few that will be different can easily be viewed by (I ran this in a Jupyter Notebook):

# Differences between current params and `Default`
for key in mpl.rcParamsDefault:
    if not mpl.rcParamsDefault[key] == mpl.rcParams[key]:
        print(key, mpl.rcParamsDefault[key], mpl.rcParams[key])

## backend agg module://ipykernel.pylab.backend_inline
## figure.dpi 100.0 72.0
## figure.edgecolor w (1, 1, 1, 0)
## figure.facecolor w (1, 1, 1, 0)
## figure.figsize [6.4, 4.8] [6.0, 4.0]
## figure.subplot.bottom 0.11 0.125

and

# Differences between `Default` and `Orig`
for key in mpl.rcParamsDefault:
    if not mpl.rcParamsDefault[key] == mpl.rcParamsOrig[key]:
        print(key, mpl.rcParamsDefault[key], mpl.rcParamsOrig[key])

## backend agg Qt5Agg
Eswar Chitirala

In my case I was searching to reset the plot sizes of the output specifically when I changed using the rc attribute used the following code to reset to default size sns.reset_defaults() (where sns is seaborn).

None of these solutions worked for me (Python 3.x, Jupyter). What worked was matplotlib.rc_file_defaults()

See the documentation here: https://matplotlib.org/3.1.1/api/matplotlib_configuration_api.html#matplotlib.rc_file_defaults

One can simply call the seaborn.set() function, with no function parameters, see [seaborn tutorial][1].

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!