我确定我忘记了一些非常简单的事情,但我无法让某些情节与 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 许可协议
使用 seaborn 创建的图需要像普通的 matplotlib 图一样显示。这可以使用
来自 matplotlib 的函数。
最初我发布的解决方案是使用已经从 seaborn (
sns.plt.show()
) 导入的 matplotlib 对象,但这被认为是一种不好的做法。因此,只需直接导入_matplotlib.pyplot_
模块并用如果使用 IPython notebook,则可以调用内联后端以消除在每个绘图后调用 show 的必要性。各自的魔法是