我确定我忘记了一些非常简单的事情,但我无法获得与Seaborn合作的确定情节。
如果我这样做:
import seaborn as sns
然后我用matplotlib像往常一样创建的任何绘图都会获得Seaborn样式(背景中的灰色网格)。
但是,如果我尝试做其中一个例子,例如:
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>
pairplot函数返回一个PairGrid对象,但该图不会显示。
我有点困惑,因为matplotlib似乎运作正常,而Seaborn风格适用于其他matplotlib情节,但Seaborn功能似乎没有做任何事情。有没有人知道可能是什么问题?
答案 0 :(得分:243)
使用seaborn创建的图需要像普通的matplotlib图一样显示。 这可以使用
完成plt.show()
来自matplotlib的功能。
最初我发布了解决方案,使用来自seaborn(sns.plt.show()
)的已导入的matplotlib对象,但这被认为是一种不好的做法。因此,只需直接导入 matplotlib.pyplot 模块并使用
import matplotlib.pyplot as plt
plt.show()
如果使用IPython笔记本,可以调用内联后端,以消除在每个绘图后调用show的必要性。各自的魔力是
%matplotlib inline
答案 1 :(得分:20)
我经常来这个问题,我总是花一点时间找到我搜索的内容:
import seaborn as sns
import matplotlib.pyplot as plt
plt.show() # <--- This is what you are looking for
请注意:在Python 2中,您也可以使用sns.plt.show()
,但不能使用Python 3。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Visualize C_0.99 for all languages except the 10 with most characters."""
import seaborn as sns
import matplotlib.pyplot as plt
l = [41, 44, 46, 46, 47, 47, 48, 48, 49, 51, 52, 53, 53, 53, 53, 55, 55, 55,
55, 56, 56, 56, 56, 56, 56, 57, 57, 57, 57, 57, 57, 57, 57, 58, 58, 58,
58, 59, 59, 59, 59, 59, 59, 59, 59, 60, 60, 60, 60, 60, 60, 60, 60, 61,
61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 62, 62, 62, 62, 62, 62, 62, 62,
62, 63, 63, 63, 63, 63, 63, 63, 63, 63, 64, 64, 64, 64, 64, 64, 64, 65,
65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 66, 66, 66, 66, 66, 66, 66,
67, 67, 67, 67, 67, 67, 67, 67, 68, 68, 68, 68, 68, 69, 69, 69, 70, 70,
70, 70, 71, 71, 71, 71, 71, 72, 72, 72, 72, 73, 73, 73, 73, 73, 73, 73,
74, 74, 74, 74, 74, 75, 75, 75, 76, 77, 77, 78, 78, 79, 79, 79, 79, 80,
80, 80, 80, 81, 81, 81, 81, 83, 84, 84, 85, 86, 86, 86, 86, 87, 87, 87,
87, 87, 88, 90, 90, 90, 90, 90, 90, 91, 91, 91, 91, 91, 91, 91, 91, 92,
92, 93, 93, 93, 94, 95, 95, 96, 98, 98, 99, 100, 102, 104, 105, 107, 108,
109, 110, 110, 113, 113, 115, 116, 118, 119, 121]
sns.distplot(l, kde=True, rug=False)
plt.show()
给出
答案 2 :(得分:12)
避免混淆(因为评论中似乎有一些)。假设你在Jupyter:
%matplotlib inline
&gt;显示 INSIDE 笔记本
sns.plt.show()
&gt;显示笔记本
%matplotlib inline
OVERRIDE sns.plt.show()
,即使调用sns.plt.show()
,笔记本中的图表也会显示 IN 。
是的,很容易将该行包含在您的配置中:
Automatically run %matplotlib inline in IPython Notebook
但在实际代码中将导入与导入保持在一起似乎是一个更好的约定。
答案 3 :(得分:3)
我的建议只是提供一个
plt.figure()
并给出一些sns情节。例如
sns.distplot(data)
。
虽然它看起来并没有显示任何情节,但当你最大化这个数字时,你将能够看到情节。
答案 4 :(得分:1)
如果您是在 IPython控制台(无法使用%matplotlib inline
)(而不是Jupyter笔记本)中进行绘图,并且不想重复运行plt.show()
,则可以使用ipython --pylab
启动IPython控制台:
$ ipython --pylab
Python 3.6.6 |Anaconda custom (64-bit)| (default, Jun 28 2018, 17:14:51)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.0.1 -- An enhanced Interactive Python. Type '?' for help.
Using matplotlib backend: Qt5Agg
In [1]: import seaborn as sns
In [2]: tips = sns.load_dataset("tips")
In [3]: sns.relplot(x="total_bill", y="tip", data=tips) # you can see the plot now
答案 5 :(得分:0)
从您的代码片段的风格来看,我想您使用的是IPython而不是Jupyter Notebook。
在GitHub上的issue中,IPython的一名成员在2016年明确表示,图表显示仅在“仅在Jupyter内核中有效”时才起作用。因此,%matplotlib inline
无法正常工作。
我只是遇到了同样的问题,建议您使用Jupyter Notebook进行可视化。
答案 6 :(得分:0)
这对我有用
import matplotlib.pyplot as plt
import seaborn as sns
.
.
.
plt.show(sns)