4

Hi, I was trying some code from WesMckinney's python data analysis book in ipython environment,which is built in anaconda. When I typed the simple code like

import matplotlib.pyplot as plt

fig = plt.figure

ax1 = fig.add_subplot(2,2,1)
Traceback (most recent call last):

File "<ipython-input-9-559e30a6412a>", line 1, in <module>
ax1 = fig.add_subplot(2,2,1)

AttributeError: 'function' object has no attribute 'add_subplot'

An AttributeError arose, but it's weird since anaconda is surely installed with matplotlib module. So Any suggestion? thank you.

2
  • 3
    You forgot the brackets. It should be fig = plt.figure(). Otherwise, you assign the function to fig, not the resulting generated figure. Commented Jan 27, 2016 at 8:58
  • Thant works, thank you. Commented Jan 27, 2016 at 9:48

1 Answer 1

3

The issue is that you don't have the open and close brackets (()) at the end of plt.figure, so you haven't actually created a figure, just assigned a handle fig to the plt.figure function. Instead, try:

import matplotlib.pyplot as plt

fig = plt.figure() #Here is your error


ax1 = fig.add_subplot(2,2,1)
Sign up to request clarification or add additional context in comments.

1 Comment

I have plt.figure() but this problem is still coming up for me

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.