python seaborn to reset back to the matplotlib
Asked Answered
M

7

64

I'm using seaborn version o.4 and matplotlib version 1.42 I have a chart displays both line and marker through simple plot command eg.

plt.plot([1,5,3,8,4],'-bo');

Due to a potential bug (https://github.com/mwaskom/seaborn/issues/344), after import seaborn, same code shows line only without marker.

import seaborn as sb 
plt.plot([1,5,3,8,4],'-bo');

So my question is: after import seaborn, Is there a way to reset all the parameters back to original?

Murmuration answered 13/11, 2014 at 0:30 Comment(0)
I
53

Yes, call seaborn.reset_orig.

Ine answered 13/11, 2014 at 2:43 Comment(1)
This sort-of works, but plots made after import seaborn and seaborn.reset_orig() still look quite different to plots made before these lines. Is there a way to get rid of all seaborn effects, as if it had never been imported?Washtub
T
40

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/stable/api/matplotlib_configuration_api.html#matplotlib.rc_file_defaults

Trine answered 15/10, 2019 at 11:14 Comment(2)
This solution worked for me with Python 3. seaborn.reset_orig did not work.Broadfaced
The correct link is now matplotlib.org/stable/api/…Doubtless
M
11

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)
Mournful answered 19/8, 2016 at 23:35 Comment(3)
Importlib imports fine, but when I try using reload I get AttributeError: 'module' object has no attribute 'reload'. Is this only available in Python 3.x?Telespectroscope
doh, updated for Python 2: import imp; imp.reload(somemodule)Mournful
For anyone reading this in the near future: I tried this with several different versions of matplotlib and seaborn inside Jupyter and could not get it to work, I don't think this is a general solution (maybe it used to be).Gunboat
S
7

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

Sneak answered 24/6, 2019 at 15:55 Comment(0)
P
5

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
Palazzo answered 18/10, 2017 at 3:15 Comment(0)
I
5

You can set them back to default by doing:

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

Also, check this post from Jake VanderPlas to learn more about customizing matplotlib.

Hope it helps!

Infanticide answered 15/5, 2020 at 20:11 Comment(0)
M
-4

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

Mccowan answered 9/10, 2016 at 10:54 Comment(2)
This does nothingHeteronomy
The solution I provided with above was working at the time I replied. No need to down vote me.Mccowan

© 2022 - 2024 — McMap. All rights reserved.