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

后端 未结 5 1911
没有蜡笔的小新
没有蜡笔的小新 2021-01-07 20:05

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
im         


        
相关标签:
5条回答
  • 2021-01-07 20:15

    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 here but the error occured. It turns out that I need to simply add

    import matplotlib.pyplot as plt
    

    on top of

    import seaborn as sns
    

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

    0 讨论(0)
  • 2021-01-07 20:23

    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
    0 讨论(0)
  • 2021-01-07 20:30

    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.

    0 讨论(0)
  • 2021-01-07 20:32

    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.

    0 讨论(0)
  • 2021-01-07 20:35

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

    sns.plot() also raises an error; these methods are not in Seaborn's 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.

    This script shows 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.

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

    0 讨论(0)
提交回复
热议问题