Simran Munot
Simran Munot

Reputation: 87

AttributeError: module 'matplotlib' has no attribute 'xlabel'

My code is :

import matplotlib as plt 
sns.distplot(CMSU['Spending'], kde = False)
plt.xlabel("Spending", size=15)
plt.ylabel("Probablity", size=15)
plt.title("Distribution for the variable - Spending", size=18);

I am getting the error:

AttributeError                            Traceback (most recent call last)
<ipython-input-32-1c6eb744a592> in <module>
      1 sns.distplot(CMSU['Spending'], kde = False)
----> 2 plt.xlabel("Spending", size=15)
      3 plt.ylabel("Probablity", size=15)
      4 plt.title("Distribution for the variable - Spending", size=18);

AttributeError: module 'matplotlib' has no attribute 'xlabel'

What possibly can go wrong?

Upvotes: 3

Views: 24034

Answers (3)

Mureinik
Mureinik

Reputation: 311143

Your import statement is wrong. Those methods belong to pyplot. I.e., you should have imported it like this:

import matplotlib.pyplot as plt

Upvotes: 6

Logan Tischler
Logan Tischler

Reputation: 115

Use:

matplotlib.pyplot.xlabel()

Same for ylabel and title:

matplotlib.pyplot.ylabel()
matplotlib.pyplot.title()

Upvotes: 2

monsieuralfonse64
monsieuralfonse64

Reputation: 666

I think you need to do import matplotlib.pyplot as plt rather than just matplotlib as plt, because xlabel and probably various other functions don't exist in matplotlib.

Upvotes: 2

Related Questions