python - Seaborn地 block 没有出现

我确定我忘记了一些非常简单的事情,但我无法让某些情节与 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 函数似乎没有做任何事情。有人知道可能是什么问题吗?

最佳答案

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

plt.show()

matplotlib 中的函数。

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

显示您的绘图
import matplotlib.pyplot as plt
plt.show()

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

%matplotlib inline

https://stackoverflow.com/questions/26597116/

相关文章:

android - adb 不显示 nexus 5 设备

linux - 如何在不重新输入 SSH 密码的情况下执行 `git pull`?

python - pandas loc vs. iloc vs. at vs. iat?

python - 使用Python在SQLite中插入行后如何检索插入的ID?

linux - 什么包包括 AB Ubuntu 中的 Apache 服务器基准测试工具

python - bs4.FeatureNotFound : Couldn't find a tre

installation - 如何从命令行正确设置 CMAKE_INSTALL_PREFIX

python - 是否有可能 "hack"Python 的打印功能?

python - 如何在不导入的情况下检查 Python 模块是否存在

python - numpy.where() 详细的分步说明/示例