Python AtributeError: 'module' 对象没有属性 'plt' - Seaborn

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45070959/
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 16:40:36  来源:igfitidea点击:

AtributeError: 'module' object has no attribute 'plt' - Seaborn

pythonpandasmatplotlibseaborn

提问by jscherman

I'm very new with these libraries and i'm having troubles while plotting this:

我对这些库很陌生,在绘制此图时遇到了麻烦:

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
import random

df5 = pd.read_csv('../../../../datos/tiempos-exacto-variando-n-m0.csv', sep=', ', engine='python')
print(df5)
df5['n'] = df5['n'].apply(lambda x: x**2)
sns.jointplot(df5['n'], df5['tiempoTotal'], kind="reg")
sns.plt.show()

And i'm getting this output:

我得到这个输出:

 n  m  tiempoTotal
0      1  0         2274
1      2  0         3370
2      3  0         5709
3      4  0         8959
4      5  0        13354
5      6  0        18503
6      7  0        26329
7      8  0        33859
8      9  0        41110
9     10  0        52710
10    11  0        64364
11    12  0        74142
12    13  0        81072
13    14  0        69332
14    15  0        71027
15    16  0        89721
16    17  0        85459
17    18  0        95217
18    19  0       119210
19    20  0       136888
20    21  0       131903
21    22  0       138395
22    23  0       151222
23    24  0       163542
24    25  0       177236
25    26  0       192475
26    27  0       240162
27    28  0       260701
28    29  0       235752
29    30  0       250835
..   ... ..          ...
580  581  0     88306854
581  582  0     89276420
582  583  0     87457875
583  584  0     90807004
584  585  0     87790003
585  586  0     89821530
586  587  0     89486585
587  588  0     88496901
588  589  0     89090661
589  590  0     89110803
590  591  0     90397942
591  592  0     94029839
592  593  0     92749859
593  594  0    105991135
594  595  0     95383921
595  596  0    105155207
596  597  0    114193414
597  598  0     98108892
598  599  0     97888966
599  600  0    103802453
600  601  0     97249346
601  602  0    101917488
602  603  0    104943847
603  604  0     98966140
604  605  0     97924262
605  606  0     97379587
606  607  0     97518808
607  608  0     99839892
608  609  0    100046492
609  610  0    103857464

[610 rows x 3 columns]
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-21-63146953b89d> in <module>()
      9 df5['n'] = df5['n'].apply(lambda x: x**2)
     10 sns.jointplot(df5['n'], df5['tiempoTotal'], kind="reg")
---> 11 sns.plt.show()

AttributeError: 'module' object has no attribute 'plt'

I'm running this in my Jupyter Notebookwith Python 2.7.12. Any ideas?

我在我的Jupyter Notebookwith 中运行这个Python 2.7.12。有任何想法吗?

采纳答案by ImportanceOfBeingErnest

sns.plt.show()works fine for me using seaborn 0.7.1. Could be that this is different in other versions. However, if you anyways import matplotlib.pyplot as pltyou may as well simply use plt.show(), as sns.plt.show()is only working because pyplotis available inside the seaborn namespace.

sns.plt.show()使用seaborn 0.7.1对我来说效果很好。可能这在其他版本中有所不同。但是,如果您无论如何,import matplotlib.pyplot as plt您也可以简单地使用plt.show(), 因为sns.plt.show()它只能pyplot在 seaborn 命名空间中使用。

回答by autonomy

Well, I ran into this issue as well with Seaborn 0.8.1. Turns out being able to call sns.plt.show()is bad practice and the fact that it worked was a bugwhich the developer fixed. Unfortunately, there are many tutorials out therethat still advise one to use sns.plt.show(). This is how I solved it:

好吧,我在 Seaborn 0.8.1 中也遇到了这个问题。事实证明,能够调用sns.plt.show()是不好的做法,而且它工作的事实开发人员修复的错误。不幸的是,有很多教程在那里仍然建议之一来使用sns.plt.show()。我是这样解决的:

  1. Import plt directly: import matplotlib.pyplot as plt
  2. Before you plot anything, set the default aesthetic parameters: sns.set()- important, because otherwise you won't get the Seaborn palettes.
  3. Replace all calls to sns.pltwith plt
  1. 直接导入plt: import matplotlib.pyplot as plt
  2. 在绘制任何内容之前,请设置默认的美学参数:sns.set()- 重要,否则您将无法获得 Seaborn 调色板。
  3. 更换所有来电sns.pltplt

回答by olisteadman

As of Seaborn 0.8.1, sns.plt.plot()raises the error module 'seaborn' has no attribute 'plt'.

从 Seaborn 0.8.1 开始,sns.plt.plot()引发错误module 'seaborn' has no attribute 'plt'

sns.plot()also raises an error; these methods are not in Seaborn's API.

sns.plot()也会引发错误;这些方法不在Seaborn 的 API 中

Dropping the “sns.” to leave “plt.plot()” (as other answers suggest) does work, but only because we've called the sns.set()method in place earlier in the script... i.e. Seaborn is making an aesthetic change: Matplotlib is still the object, which does the plotting, via its plt.plot()method.

删除“sns”。离开“plt.plot()”(正如其他答案所建议的)确实有效,但这只是因为我们在脚本的早期调用了sns.set()方法……即Seaborn 正在进行美学上的改变:Matplotlib仍然是对象,它通过plt.plot()方法进行绘图

This scriptshows sns.set()in action... if you follow the comments and swap sns.set()between different locations in the script, it changes the appearance of the subplots. They look like Seaborn plots, but Matplotlib is doing the plotting.

该脚本显示了sns.set()的作用...如果您按照注释并在脚本的不同位置之间交换sns.set(),它会改变子图的外观。它们看起来像 Seaborn 绘图,但 Matplotlib 正在绘图。

Seaborn does of course have a load of its own plot methods (like sns.boxplot(), sns.violinplot() etc) but there is no longer a method sns.plt.plot().

Seaborn 当然有很多自己的绘图方法(如 sns.boxplot()、sns.violinplot() 等),但不再有方法sns.plt.plot()

回答by Anugrah Andisetiawan

Can't comment because low reputation (newbie here)

由于声誉低而无法发表评论(这里是新手)

I just want to confirm that I got the same error using Jupyter inside Anaconda (Feb 2018). Got the code from herebut the error occured. It turns out that I need to simply add

我只想确认我在 Anaconda 中使用 Jupyter 时遇到了同样的错误(2018 年 2 月)。从这里得到了代码,但发生了错误。事实证明,我需要简单地添加

import matplotlib

on top of

在之上

import seaborn as sns

and it work just fine using plt.show()instead of sns.plt.show()

它使用plt.show()而不是sns.plt.show()

回答by Denis Mwaniki

Ensure you have updated your python shell as well IDE's like Anaconda. Like I had a constant error in Spyder (Hosted under Anaconda) with relplot and catplot until I updated Anaconda as well as seaborn (0.90). Updating via the Anaconda commandline should be pretty straightforward like in my case.

确保你已经更新了你的 python shell 以及 IDE 像 Anaconda。就像我在 Spyder(在 Anaconda 下托管)的 relplot 和 catplot 中一直出现错误,直到我更新了 Anaconda 和 seaborn (0.90)。通过 Anaconda 命令行进行更新应该非常简单,就像我的情况一样。