Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seaborn configuration hides default matplotlib [duplicate]

Tags:

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.

like image 867
joaquin Avatar asked Oct 13 '15 09:10

joaquin


People also ask

How do I change my default seaborn style?

To switch to seaborn defaults, simply call the set_theme() function.

How do I get rid of Xlabel in seaborn?

To hide or remove X-axis labels, use set(xlabel=None). To display the figure, use show() method.

Why is seaborn over matplotlib?

Seaborn is more comfortable in handling Pandas data frames. It uses basic sets of methods to provide beautiful graphics in python. Matplotlib works efficiently with data frames and arrays.It treats figures and axes as objects. It contains various stateful APIs for plotting.

Can you use seaborn and matplotlib together?

Seaborn provides an API on top of Matplotlib that offers sane choices for plot style and color defaults, defines simple high-level functions for common statistical plot types, and integrates with the functionality provided by Pandas DataFrame s.


2 Answers

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

like image 193
tmdavison Avatar answered Oct 20 '22 00:10

tmdavison


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

like image 21
Francio Rodrigues Avatar answered Oct 19 '22 23:10

Francio Rodrigues