23

Seaborn provides of a handful of graphics which are very interesting for scientifical data representation. Thus I started using these Seaborn graphics interspersed with other customized matplotlib plots. The problem is that once I do:

import seaborn as sb

This import seems to set the graphic parameters for seaborn globally and then all matplotlib graphics below the import get the seaborn parameters (they get a grey background, linewithd changes, etc, etc).

In SO there is an answer explaining how to produce seaborn plots with matplotlib configuration, but what I want is to keep the matplotlib configuration parameters unaltered when using both libraries together and at the same time be able to produce, when needed, original seaborn plots.

Community
  • 1
  • 1
joaquin
  • 82,968
  • 29
  • 138
  • 152
  • The seaborn documentation is quite good. This is covered near the beginning of the installation docs: http://stanford.edu/~mwaskom/software/seaborn/installing.html?highlight=apionly#importing-seaborn – Paul H Oct 13 '15 at 21:17
  • 1
    I'm not certain this is an exact duplicate of that question: it also addresses the need to switch between seaborn and matplotlib defaults dynamically in a script, which is not covered in the suggested duplicate – tmdavison Oct 14 '15 at 09:35

5 Answers5

23

If you never want to use the seaborn style, but do want some of the seaborn functions, you can import seaborn using this following line (documentation):

import seaborn.apionly as sns

If you want to produce some plots with the seaborn style and some without, in the same script, you can turn the seaborn style off using the seaborn.reset_orig function.

It seems that doing the apionly import essentially sets reset_orig automatically on import, so its up to you which is most useful in your use case.

Here's an example of switching between matplotlib defaults and seaborn:

import matplotlib.pyplot as plt
import matplotlib
import numpy as np

# a simple plot function we can reuse (taken from the seaborn tutorial)
def sinplot(flip=1):
    x = np.linspace(0, 14, 100)
    for i in range(1, 7):
        plt.plot(x, np.sin(x + i * .5) * (7 - i) * flip)

sinplot()

# this will have the matplotlib defaults
plt.savefig('seaborn-off.png')
plt.clf()

# now import seaborn
import seaborn as sns

sinplot()

# this will have the seaborn style
plt.savefig('seaborn-on.png')
plt.clf()

# reset rc params to defaults
sns.reset_orig()

sinplot()

# this should look the same as the first plot (seaborn-off.png)
plt.savefig('seaborn-offagain.png')

which produces the following three plots:

seaborn-off.png: seaborn-off

seaborn-on.png: seaborn-on

seaborn-offagain.png: enter image description here

tmdavison
  • 64,360
  • 12
  • 187
  • 165
  • Thanks, reset_orig() is a good approach but still it is not producing exactly the original figure in my case. I produced and saved a matplolib figure (two consecutive `plot(xi, yi, '.')` where xi and yi were lists of some hundred elements) from two different ipython notebooks, one using straight matplotlib and another plotting a seaborn figure before resetting to original matplotlib conditions. The color of the dots in the post-seaborn figure was notably less intense\contrasted than in the one with the unaltered matplotlib configuration (like if they had some transparency). – joaquin Oct 14 '15 at 08:59
  • 2
    Maybe it would be best to share some code so others can reproduce that problem? – tmdavison Oct 14 '15 at 09:35
  • I am using the '0.9.0' version and, when I import it, it tells me that "the seaborn.apionly module is deprecated". Still, I need to use `.set_orig` to avoid the effect... – nocibambi Feb 11 '19 at 03:43
13

As of seaborn version 0.8 (July 2017) the graph style is not altered anymore on import:

The default [seaborn] style is no longer applied when seaborn is imported. It is now necessary to explicitly call set() or one or more of set_style(), set_context(), and set_palette(). Correspondingly, the seaborn.apionly module has been deprecated.

You can choose the style of any plot with plt.style.use().

import matplotlib.pyplot as plt
import seaborn as sns

plt.style.use('seaborn')     # switch to seaborn style
# plot code
# ...

plt.style.use('default')     # switches back to matplotlib style
# plot code
# ...


# to see all available styles
print(plt.style.available)

Read more about plt.style().

TrebledJ
  • 8,713
  • 7
  • 26
  • 48
Frâncio Rodrigues
  • 2,190
  • 3
  • 21
  • 30
  • I had to do `plt.style.use('classic')` to get the default matplotlib style. – Yibo Yang Mar 11 '18 at 01:26
  • Yes, good point. `plt.style.use('classic')` changes to the old matplotlib `1.X` default style `plt.style.use(default)` is the `2.X` new default. Check https://matplotlib.org/users/dflt_style_changes.html to see the changes in depth. – Frâncio Rodrigues May 16 '18 at 23:10
  • how can I see which style is currently used? – skjerns Apr 28 '20 at 09:15
2

You may use the matplotlib.style.context functionality as described in the style guide.

#%matplotlib inline #if used in jupyter notebook
import matplotlib.pyplot as plt
import seaborn as sns

# 1st plot 
with plt.style.context("seaborn-dark"):
    fig, ax = plt.subplots()
    ax.plot([1,2,3], label="First plot (seaborn-dark)")

# 2nd plot 
with plt.style.context("default"):
    fig, ax = plt.subplots()
    ax.plot([3,2,1], label="Second plot (matplotlib default)")

#  3rd plot 
with plt.style.context("seaborn-darkgrid"):
    fig, ax = plt.subplots()
    ax.plot([2,3,1], label="Third plot (seaborn-darkgrid)")

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
2

Restore all RC params to original settings (respects custom rc) is allowed by seaborn.reset_orig() function

pcu
  • 1,204
  • 11
  • 27
0

As explained in this other question you can import seaborn with:

import seaborn.apionly as sns

And the matplotlib styles will not be modified.

Community
  • 1
  • 1
Ramon Crehuet
  • 3,679
  • 1
  • 22
  • 37