Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 Notebook with Python 2.7.12. Any ideas?

like image 298
jscherman Avatar asked Jul 13 '17 03:07

jscherman


People also ask

Why is %Matplotlib inline?

%matplotlib inline sets the backend of matplotlib to the 'inline' backend: With this backend, the output of plotting commands is displayed inline within frontends like the Jupyter notebook, directly below the code cell that produced it. The resulting plots will then also be stored in the notebook document.

What is matplotlib PyLab?

MatPlotLib with Python PyLab is a procedural interface to the Matplotlib object-oriented plotting library. Matplotlib is the whole package; matplotlib. pyplot is a module in Matplotlib; and PyLab is a module that gets installed alongside Matplotlib. PyLab is a convenience module that bulk imports matplotlib.


2 Answers

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 plt you may as well simply use plt.show(), as sns.plt.show() is only working because pyplot is available inside the seaborn namespace.

like image 165
ImportanceOfBeingErnest Avatar answered Oct 06 '22 00:10

ImportanceOfBeingErnest


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 bug which the developer fixed. Unfortunately, there are many tutorials out there that still advise one to use sns.plt.show(). This is how I solved it:

  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.plt with plt
like image 43
autonomy Avatar answered Oct 05 '22 23:10

autonomy