python - Seaborn 情节未显示

标签 python seaborn

<分区>

我正在学习教程 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))

最佳答案

正如上面另一个答案中提到的,在 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 的典型行为,因此是建立在 matplotlib 之上的 seaborn。您必须指定需要对图形对象执行的操作(IPython 中的 %matplotlib 内联调用实际上是告诉 IPython 捕获这些对象并显示它们)。

关于python - Seaborn 情节未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34445857/

相关文章:

hex - Python-sns.color_palette输出为Bokeh的十六进制数字

python - seaborn:lmplot() 得到一个意外的关键字参数 'figsize'

pandas - 可视化 pandas 分组数据

python - 如何在一个元素上使用多个自定义的Jinja过滤器

python - python lambda函数返回2个值

python - Django hstore 字段和索引

python - 使用 pymssql 库 - 即使没有任何可用值,查询也会返回未知值?

python - 有没有办法限制 Python SEaborn 条形图(或任何图表)的结果数量?

python - 如何 reshape 多个时间序列信号以与 sns.tsplot 一起使用?

python - 无法调用实现递归调用的方法