Seaborn绘图不显示

4

我正在遵循这里的教程,这似乎是最简单的例子——一个线性关系。

对于非常宽松的导入,我不太清楚,并且似乎无法显示我的图表。

my_seaborn_test.py:

import numpy as np
import seaborn as sns

class SeabornTest(object): 
    def run_example(self):
        sns.set(color_codes=True)
        np.random.seed(sum(map(ord, "regression")))
        tips = sns.load_dataset("tips")
        sns.lmplot(x="size", y="tip", data=tips, x_estimator=np.mean)

在命令行中:
python
from my_seaborn_test import SeabornTest
st = SeabornTest()
st.run_example()

我得到的只有沉默和这个警告:

/home/me/anaconda3/envs/myenv/lib/python3.5/site-packages/matplotlib/__init__.py:892: UserWarning: axes.color_cycle is deprecated and replaced with axes.prop_cycle; please use the latter.
  warnings.warn(self.msg_depr % (key, alt_key))

2
你可以使用 import matplotlib.pyplot as plt,然后在最后使用 plt.show() - Luyi Tian
1个回答

3
如上所述,在IPython中运行将允许您查看它,但这不是唯一的方法。
您的方法的最后一行返回一个FacetGrid对象,在方法退出后被静默丢弃,这就是为什么除了产生的警告之外,您看不到任何东西。 您需要对此对象进行某些操作(当它被生成时,IPython会自动“打印”它)。
像这样更改您的方法:
def run_example(self):
    sns.set(color_codes=True)
    np.random.seed(sum(map(ord, "regression")))
    tips = sns.load_dataset("tips")
    sns.lmplot(x="size", y="tip", data=tips, x_estimator=np.mean).fig.show()

现在你的示例将打开一个图形窗口显示该图。如果您想保存它,只需将最后一行更改为:
sns.lmplot(x="size", y="tip", data=tips, x_estimator=np.mean).savefig("testing.png")

这种行为是matplotlib的典型,因此seaborn也建立在matplotlib之上。您需要指定图形对象的处理方式(IPython中的%matplotlib inline调用实际上是告诉IPython捕获这些对象并显示它们)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接