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 函数似乎没有做任何事情。有谁知道可能是什么问题?

原文由 rtc3po 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 307
2 个回答

使用 seaborn 创建的图需要像普通的 matplotlib 图一样显示。这可以使用

plt.show()

来自 matplotlib 的函数。

最初我发布的解决方案是使用已经从 seaborn ( sns.plt.show() ) 导入的 matplotlib 对象,但这被认为是一种不好的做法。因此,只需直接导入 _matplotlib.pyplot_ 模块并用

import matplotlib.pyplot as plt
plt.show()

如果使用 IPython notebook,则可以调用内联后端以消除在每个绘图后调用 show 的必要性。各自的魔法是

%matplotlib inline

原文由 Jakob 发布,翻译遵循 CC BY-SA 4.0 许可协议

我经常问这个问题,我总是需要一段时间才能找到我搜索的内容:

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

给予

在此处输入图像描述

原文由 Martin Thoma 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题