I'm sure I'm forgetting something very simple, but I cannot get certain plots to work with Seaborn.
If I do:
import seaborn as sns
Then any plots that I create as usual with matplotlib get the Seaborn styling (with the grey grid in the background).
However, if I try to do one of the examples, such as:
In [1]: import seaborn as sns
In [2]: sns.set()
In [3]: df = sns.load_dataset('iris')
In [4]: sns.pairplot(df, hue='species', size=2.5)
Out[4]: <seaborn.axisgrid.PairGrid at 0x3e59150>
The pairplot function returns a PairGrid object, but the plot doesn't show up.
I'm a little confused because matplotlib seems to be functioning properly, and the Seaborn styles are applied to other matplotlib plots, but the Seaborn functions don't seem to do anything. Does anybody have any idea what might be the problem?
To avoid confusion (as there seems to be some in the comments). Assuming you are on Jupyter:
%matplotlib inline
> displays the plots INSIDE the notebooksns.plt.show()
> displays the plots OUTSIDE of the notebook%matplotlib inline
will OVERRIDEsns.plt.show()
in the sense that plots will be shown IN the notebook even whensns.plt.show()
is called.And yes, it is easy to include the line in to your config:
Automatically run %matplotlib inline in IPython Notebook
But it seems a better convention to keep it together with imports in the actual code.
Plots created using seaborn need to be displayed like ordinary matplotlib plots. This can be done using the
function from matplotlib.
Originally I posted the solution to use the already imported matplotlib object from seaborn (
sns.plt.show()
) however this is considered to be a bad practice. Therefore, simply directly import the matplotlib.pyplot module and show your plots withIf the IPython notebook is used the inline backend can be invoked to remove the necessity of calling show after each plot. The respective magic is
My advice is just to give a
plt.figure()
and give some sns plot. For examplesns.distplot(data)
.Though it will look it doesnt show any plot, When you maximise the figure, you will be able to see the plot.
To tell from the style of your code snippet, I suppose you were using IPython rather than Jupyter Notebook.
In this issue on GitHub, it was made clear by a member of IPython in 2016 that the display of charts would only work when "only work when it's a Jupyter kernel". Thus, the
%matplotlib inline
would not work.I was just having the same issue and suggest you use Jupyter Notebook for the visualization.
I come to this question quite regularly and it always takes me a while to find what I search:
Please note: In Python 2, you can also use
sns.plt.show()
, but not in Python 3.Complete Example
Gives
If you plot in IPython console (where you can't use
%matplotlib inline
) instead of Jupyter notebook, and don't want to runplt.show()
repeatedly, you can start IPython console withipython --pylab
: