Python Seaborn 地块没有出现

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26597116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:43:49  来源:igfitidea点击:

Seaborn plots not showing up

pythonmatplotlibseaborn

提问by robc89

I'm sure I'm forgetting something very simple, but I cannot get certain plots to work with Seaborn.

我确定我忘记了一些非常简单的事情,但我无法获得与 Seaborn 一起使用的某些情节。

If I do:

如果我做:

import seaborn as sns

Then any plots that I create as usual with matplotlib get the Seaborn styling (with the grey grid in the background).

然后我像往常一样使用 matplotlib 创建的任何图都会获得 Seaborn 样式(背景中带有灰色网格)。

However, if I try to do one of the examples, such as:

但是,如果我尝试执行以下示例之一,例如:

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>

The pairplot function returns a PairGrid object, but the plot doesn't show up.

pairplot 函数返回一个 PairGrid 对象,但该图未显示。

I'm a little confused because matplotlib seems to be functioning properly, and the Seaborn styles are applied to other matplotlib plots, but the Seaborn functions don't seem to do anything. Does anybody have any idea what might be the problem?

我有点困惑,因为 matplotlib 似乎运行正常,并且 Seaborn 样式应用于其他 matplotlib 绘图,但 Seaborn 函数似乎没有做任何事情。有人知道可能是什么问题吗?

采纳答案by Jakob

Plots created using seaborn need to be displayed like ordinary matplotlib plots. This can be done using the

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

plt.show()

function from matplotlib.

来自 matplotlib 的函数。

Originally I posted the solution to use the already imported matplotlib object from seaborn (sns.plt.show()) however this is considered to be a bad practice. Therefore, simply directly import the matplotlib.pyplotmodule and show your plots with

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

import matplotlib.pyplot as plt
plt.show()

If the IPython notebook is used the inline backend can be invoked to remove the necessity of calling show after each plot. The respective magic is

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

%matplotlib inline

回答by user 3317704

My advice is just to give a

我的建议只是给一个

plt.figure()and give some sns plot. For example

plt.figure()并给出一些sns情节。例如

sns.distplot(data).

sns.distplot(data).

Though it will look it doesnt show any plot, When you maximise the figure, you will be able to see the plot.

虽然看起来它没有显示任何情节,但是当您最大化图形时,您将能够看到情节。

回答by mikkokotila

To avoid confusion (as there seems to be some in the comments). Assuming you are on Jupyter:

为了避免混淆(因为评论中似乎有一些)。假设你在 Jupyter 上:

%matplotlib inline> displays the plots INSIDEthe notebook

%matplotlib inline>显示的曲线INSIDE笔记本

sns.plt.show()> displays the plots OUTSIDEof the notebook

sns.plt.show()>显示的曲线OUTSIDE笔记本

%matplotlib inlinewill OVERRIDEsns.plt.show()in the sense that plots will be shown INthe notebook even when sns.plt.show()is called.

%matplotlib inline覆盖sns.plt.show(),即使在调用时,绘图也会显示笔记本中sns.plt.show()

And yes, it is easy to include the line in to your config:

是的,很容易将这一行包含在您的配置中:

Automatically run %matplotlib inline in IPython Notebook

在 IPython Notebook 中自动内联运行 %matplotlib

But it seems a better convention to keep it together with imports in the actual code.

但是在实际代码中将它与导入保持在一起似乎是一个更好的约定。

回答by Martin Thoma

I come to this question quite regularly and it always takes me a while to find what I search:

我经常来问这个问题,我总是需要一段时间才能找到我搜索的内容:

import seaborn as sns
import matplotlib.pyplot as plt

plt.show()  # <--- This is what you are looking for

Please note: In Python 2, you can also use sns.plt.show(), but not in Python 3.

请注意:在 Python 2 中,您也可以使用sns.plt.show(),但不能在 Python 3 中使用。

Complete Example

完整示例

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""Visualize C_0.99 for all languages except the 10 with most characters."""

import seaborn as sns
import matplotlib.pyplot as plt

l = [41, 44, 46, 46, 47, 47, 48, 48, 49, 51, 52, 53, 53, 53, 53, 55, 55, 55,
     55, 56, 56, 56, 56, 56, 56, 57, 57, 57, 57, 57, 57, 57, 57, 58, 58, 58,
     58, 59, 59, 59, 59, 59, 59, 59, 59, 60, 60, 60, 60, 60, 60, 60, 60, 61,
     61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 62, 62, 62, 62, 62, 62, 62, 62,
     62, 63, 63, 63, 63, 63, 63, 63, 63, 63, 64, 64, 64, 64, 64, 64, 64, 65,
     65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 66, 66, 66, 66, 66, 66, 66,
     67, 67, 67, 67, 67, 67, 67, 67, 68, 68, 68, 68, 68, 69, 69, 69, 70, 70,
     70, 70, 71, 71, 71, 71, 71, 72, 72, 72, 72, 73, 73, 73, 73, 73, 73, 73,
     74, 74, 74, 74, 74, 75, 75, 75, 76, 77, 77, 78, 78, 79, 79, 79, 79, 80,
     80, 80, 80, 81, 81, 81, 81, 83, 84, 84, 85, 86, 86, 86, 86, 87, 87, 87,
     87, 87, 88, 90, 90, 90, 90, 90, 90, 91, 91, 91, 91, 91, 91, 91, 91, 92,
     92, 93, 93, 93, 94, 95, 95, 96, 98, 98, 99, 100, 102, 104, 105, 107, 108,
     109, 110, 110, 113, 113, 115, 116, 118, 119, 121]

sns.distplot(l, kde=True, rug=False)

plt.show()

Gives

enter image description here

在此处输入图片说明

回答by Weisi Zhan

To tell from the style of your code snippet, I suppose you were using IPython rather than Jupyter Notebook.

从你的代码片段的风格来看,我想你使用的是 IPython 而不是 Jupyter Notebook。

In this issueon GitHub, it was made clear by a member of IPython in 2016 that the display of charts would only work when "only work when it's a Jupyter kernel". Thus, the %matplotlib inlinewould not work.

在GitHub 上的这个issue 中,IPython 的一名成员在 2016 年明确表示,图表的显示只有在“只有在它是 Jupyter 内核时才能工作”时才有效。因此, %matplotlib inline将行不通。

I was just having the same issue and suggest you use Jupyter Notebook for the visualization.

我只是遇到了同样的问题,建议您使用 Jupyter Notebook 进行可视化。

回答by Chad

If you plot in IPython console(where you can't use %matplotlib inline) instead of Jupyter notebook, and don't want to run plt.show()repeatedly, you can start IPython console with ipython --pylab:

如果您在IPython 控制台(不能使用%matplotlib inline)而不是 Jupyter notebook 中绘图,并且不想plt.show()重复运行,则可以使用以下命令启动 IPython 控制台ipython --pylab

$ ipython --pylab     
Python 3.6.6 |Anaconda custom (64-bit)| (default, Jun 28 2018, 17:14:51) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.0.1 -- An enhanced Interactive Python. Type '?' for help.
Using matplotlib backend: Qt5Agg

In [1]: import seaborn as sns

In [2]: tips = sns.load_dataset("tips")

In [3]: sns.relplot(x="total_bill", y="tip", data=tips) # you can see the plot now

回答by emon

This worked for me

这对我有用

import matplotlib.pyplot as plt
import seaborn as sns
.
.
.
plt.show(sns)