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.
To switch to seaborn defaults, simply call the set_theme() function.
To hide or remove X-axis labels, use set(xlabel=None). To display the figure, use show() method.
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.
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.
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-on.png:
seaborn-offagain.png:
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 ofset_style()
,set_context()
, andset_palette()
. Correspondingly, theseaborn.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()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With