Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: 'function' object has no attribute 'add_subplot' when using matplotlib in ipython

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.

like image 973
zlqs1985 Avatar asked Oct 29 '25 07:10

zlqs1985


1 Answers

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)
like image 113
Mithun Mistry Avatar answered Nov 01 '25 07:11

Mithun Mistry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!