importing seaborn changes matplotlib graphs [duplicate] - python

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

Related

Why am I getting UserWarning: Matplotlib is currently using ps, which is a non-GUI backend, so cannot show the figure

This is not in a jupyter notebook so this is not a duplicate of this question, but my code is:
from gluoncv import model_zoo, data, utils
from matplotlib import pyplot as plt
...
plt.show()
The error I'm getting is:
/figure.py:445: UserWarning: Matplotlib is currently using ps, which is a non-GUI backend, so cannot show the figure.
% get_backend())
I created a repl at https://repl.it/#shamoons/WelloffHarmfulMineral
If it matters, I'm using OS X. What do I need to do to get the image to show?
You can use
matplotlib.use("TkAgg")
instead of
matplotlib.use("PS")
when developing on MacOS.
Please note that the import should be before importing plt, like this:
import matplotlib
matplotlib.use('TkAgg')
from matplotlib import pyplot as plt
matplotlib.use('PS') and plt.show() are mutually exclusive. You need to decide:
Do you want to show the figure on screen? Solution: Remove the line matplotlib.use('PS').
Do you want to use the PS backend? This seems unlikely, because there is rarely a reason to set the backend to something non-interactive unless working on a server. Anyways, solution: Replace plt.show() by plt.savefig("filname.ps").

Importing seaborn in python script messing up plot style

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

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:

What is the difference between pylab and pyplot? [duplicate]

This question already has answers here:
Which is the recommended way to plot: matplotlib or pylab?
(2 answers)
Closed 1 year ago.
What is the difference between
matplotlib.pyplot and matplotlib.pylab?
Which is preferred for what usage?
I am a little confused, because it seems like independent from which I import, I can do the same things. What am I missing?
This wording is no longer in the documentation.
Use of the pylab import is now discouraged and the OO interface is recommended for most non-interactive usage.
From the documentation, the emphasis is mine:
Matplotlib is the whole package; pylab is a module in matplotlib that gets installed alongside matplotlib; and matplotlib.pyplot is a module in matplotlib.
Pyplot provides the state-machine interface to the underlying plotting library in matplotlib. This means that figures and axes are implicitly and automatically created to achieve the desired plot. For example, calling plot from pyplot will automatically create the necessary figure and axes to achieve the desired plot. Setting a title will then automatically set that title to the current axes object:
Pylab combines the pyplot functionality (for plotting) with the numpy functionality (for mathematics and for working with arrays) in a single namespace, making that namespace (or environment) even more MATLAB-like. For example, one can call the sin and cos functions just like you could in MATLAB, as well as having all the features of pyplot.
The pyplot interface is generally preferred for non-interactive plotting (i.e., scripting). The pylab interface is convenient for interactive calculations and plotting, as it minimizes typing. Note that this is what you get if you use the ipython shell with the -pylab option, which imports everything from pylab and makes plotting fully interactive.

Categories

Resources