Seaborn绘图不显示
我觉得我可能忘记了一些很简单的东西,但我在用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的函数似乎没有任何效果。有没有人知道可能是什么问题呢?
8 个回答
如果你在IPython 控制台中绘图(而不是在 Jupyter 笔记本里,那里你不能使用 %matplotlib inline
),而且不想每次都运行 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
这个对我有效
import matplotlib.pyplot as plt
import seaborn as sns
.
.
.
plt.show(sns)
为了避免混淆(评论中似乎有些混乱),假设你正在使用Jupyter:
%matplotlib inline
> 会把图表显示在笔记本里面
sns.plt.show()
> 会把图表显示在笔记本外面
%matplotlib inline
会覆盖 sns.plt.show()
,也就是说即使你调用了 sns.plt.show()
,图表还是会在笔记本里显示。
而且,是的,把这行代码加到你的配置里是很简单的:
自动在IPython Notebook中运行 %matplotlib inline
不过,把它和导入语句放在一起写在实际代码里似乎是个更好的习惯。
我经常会遇到这个问题,每次都要花一段时间才能找到我想要的东西:
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()
结果是
用seaborn创建的图表需要像普通的matplotlib图表一样显示。这可以通过使用
plt.show()
这个函数来实现。
最开始我提到过可以使用seaborn里已经导入的matplotlib对象来显示图表(sns.plt.show()
),但这被认为是一种不好的做法。所以,最好直接导入_matplotlib.pyplot_
模块,然后用
import matplotlib.pyplot as plt
plt.show()
来显示你的图表。
如果你在使用IPython笔记本,可以调用内联后端,这样就不需要在每个图表后面都调用显示函数。相关的魔法命令是
%matplotlib inline