Importing seaborn in python script messing up plot style - python

Attached below are two plots. The only difference in the script that produced them is that the second one had an additional line:
import seaborn as sns
I am not setting any seaborn style yet. Just importing seaborn is changing plot style though, even in plots not using seaborn. Is there any way I can import seaborn (to be used in other plots), and not have the style changed for plots that do not use it?

Check this
import seaborn.apionly as sns or from seaborn.apionly import lmplot
http://stanford.edu/~mwaskom/software/seaborn/whatsnew.html

Related

importing seaborn changes matplotlib graphs [duplicate]

I am trying to use seaborn, because of its distplot function. But I prefer the default matplotlib settings. When I import seaborn, it changes automatically the appearance of my figure.
How can I use seaborn functions without changing the look of the plots?
Version 0.8 (july 2017) changed this behaviour. From https://seaborn.pydata.org/whatsnew.html#v0-8-0-july-2017:
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.
For older versions, Import seaborn like this:
import seaborn.apionly as sns
and then you should be able to use sns.distplot but maintain the default matplotlib styling + your personal rc configuration.
According to documentation reset_orig restore all RC params to original settings:
import seaborn as sns
# reset RC params to original
sns.reset_orig()

Ipython notebook 3 disables seaborn settings

I just upgraded to IPython Notebook version 3.0 and it's disabling the formatting for seaborn. Here's some sample code that replicates the problem
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
data = np.random.randn(100)
fig,ax = plt.subplots(figsize = (11,8.5))
ax.plot(data)
This code works just fine in IPython Notebook V2.4.1 (see http://nbviewer.ipython.org/gist/anonymous/71733c24a68ee464ca40), but in IPython Notebook v3.0, the axes become invisible (see http://nbviewer.ipython.org/gist/anonymous/7525146b07709206908c).
Strangely, in V3, when I switch the order of the seaborn import and the matplotlib inline magic, the plot renders normally the first time I run, then if I re-run, the axes and gridlines disappear. So it seems to have something to do with the inline magic disabling seaborn properties.
Any workarounds, other than not re-executing my imports after the first time?
In iPython Notebook 3.0, add:
seaborn.set_style('darkgrid')
to restore Seaborn default color schemes.

How do you make ggplot plots of numpy arrays?

I know how to use ggplot for data frames, but is there a good way to make plots from numpy arrays directly? Or do I have to convert?
If you just want to plot things in a "ggplot-like style", you can use the matplotlib.style package:
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import style
# use ggplot style sheet
style.use('ggplot')
plt.plot(np.random.randn(10))
Of course (as cel pointed out), with matplotlib it's still up to you to make sure your plots actually follow the conventions set out in Grammar of Graphics.

How can I use seaborn without changing the matplotlib defaults?

I am trying to use seaborn, because of its distplot function. But I prefer the default matplotlib settings. When I import seaborn, it changes automatically the appearance of my figure.
How can I use seaborn functions without changing the look of the plots?
Version 0.8 (july 2017) changed this behaviour. From https://seaborn.pydata.org/whatsnew.html#v0-8-0-july-2017:
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.
For older versions, Import seaborn like this:
import seaborn.apionly as sns
and then you should be able to use sns.distplot but maintain the default matplotlib styling + your personal rc configuration.
According to documentation reset_orig restore all RC params to original settings:
import seaborn as sns
# reset RC params to original
sns.reset_orig()

Figure-specific vs general properties in matplotlib

I am trying to understand how methods and attributes are organized in matplotlib. For example, say I have a figure:
import matplotlib.pyplot as plt
my_fig = plt.imshow(image)
I have noticed that some figure properties are set via module methods, e.g.:
plt.axis('off')
while others are set for the figure itself using object methods:
my_fig.set_cmap('hot')
Can figure properties be specified in either way?
How can I turn off the axis by calling methods on my object my_fig?
The plt methods are part of the pyplot API, which is intended to provide Matlab-like convenience for interactive use (and certainly appears to be very influenced by Matlab). But it's just one small facet of the whole matplotlib API (which is much more OOP). In practice I seem to end up mixing them both myself in SW; it's largely a matter of taste whether you go through the pyplot API or access the objects. pyplot is certainly very convenient although as you want to do more complex/exotic things you'll find what you can do with pyplot alone limited and you'll need to get to know at least the full API's Axes, Figure, Legend and Path objects better.
Pyplot is a collection of command style functions that make matplotlib work like MATLAB, matplotlib.figure.Figure is part of the object-oriented API.
In most cases you can configure figure settings via itself like this:
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
image=mpimg.imread('stinkbug.png')
my_fig = plt.imshow(image)
my_fig.axes.axes.get_xaxis().set_visible(False)
my_fig.axes.axes.get_yaxis().set_visible(False)
plt.show()
enter code here
required stinkbug.png:
result:

Categories

Resources