【问题标题】:Seaborn plot is not showing [duplicate]Seaborn情节未显示[重复]
【发布时间】:2016-03-30 11:44:39
【问题描述】:

我正在关注here 的教程,这似乎是最简单的示例 - 线性关系。

我不清楚非常宽松的导入,并且似乎无法展示我的情节。

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

【问题讨论】:

  • 你可以import matplotlib.pyplot as plt 最后plt.show()

标签: python seaborn


【解决方案1】:

正如上面另一个答案中提到的,在 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 内联调用实际上是告诉 IPython 捕获这些对象并显示它们)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-23
    • 2020-11-11
    • 1970-01-01
    相关资源
    最近更新 更多