TypeError: 'AxesSubplot' object does not support indexing

0 votes

I have a question about an error I receive when looping to plot multiple subplots from a data frame.

My data frame has many columns, of which I loop over to have a subplot of each column.

This is my code

 def plot(df):
    channels=[]
    for i in df:
        channels.append(i)

    fig, ax = plt.subplots(len(channels), sharex=True, figsize=(50,100))

    plot=0    
    for j in df: 

        ax[plot].plot(df["%s" % j])
        ax[plot].set_xlabel('%s' % j)
        plot=plot+1

    plt.tight_layout()
    plt.show() 

I get the plot produced fine, but also an empty frame and the error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\AClayton\WinPython-64bit-2.7.5.3\python-2.7.5.amd64\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 538, in runfile
    execfile(filename, namespace)
  File "C:/Users/AClayton/Desktop/Data/TS.py", line 67, in <module>
    plot(all_data)
  File "C:/Users/AClayton/Desktop/Data/TS.py", line 49, in plot
    ax[plot].plot(reader["%s" % j])
TypeError: 'AxesSubplot' object does not support indexing

I can't see where this error comes from if the first plot is produced fine, or why the second figure is produced?

May 7 in Python by kartik
• 11,890 points
122 views

1 answer to this question.

0 votes

Hello ,

  • If you plot multiple subplots, the plt.subplots() returns the axes in an array, that array allows indexing like you do with ax[plot]. When only 1 subplot is created, by default it returns the axes itself, not the axes within an array.
  • So your error occurs when len(channels) equals 1. You can suppress this behavior by setting squeeze=False in the .subplots() command. This forces it to always return a 'Rows x Cols' sized array with the axes, even if its a single one.

So:

 def plot(df):
    channels=[]
    for i in df:
        channels.append(i)

    fig, ax = plt.subplots(len(channels),1, sharex=True, figsize=(50,100), squeeze=False)

    plot=0    
    for j in df: 

        ax[plot,0].plot(df["%s" % j])
        ax[plot,0].set_xlabel('%s' % j)
        plot=plot+1

    plt.tight_layout()
    plt.show() 

By adding the squeeze keyword you always get a 2D array in return, so the indexing for a subplot changes to ax[plot,0]. I have also specifically added the amount of columns .

Thank You!

answered May 7 by Niroj
• 23,950 points

Related Questions In Python

0 votes
1 answer

print(instance(0).id) TypeError: 'list' object is not callable

Hey @Suraj, For me its working fine. ...READ MORE

answered Jan 18, 2019 in Python by Priyaj
• 57,530 points
593 views
0 votes
1 answer

Python TypeError: 'list' object is not callable.

The error says the list is not ...READ MORE

answered Feb 8, 2019 in Python by Omkar
• 69,060 points
11,346 views
+1 vote
1 answer

Does python support all the object oriented concepts?

Python is indeed an object oriented programming ...READ MORE

answered May 20, 2019 in Python by Mohammad
• 3,230 points
119 views
0 votes
1 answer

i am normalizing the data set iris in python and get the error ::TypeError: 'numpy.float64' object is not callable

TRY THIS   #Nomalisation for i in names:     print(i)   ...READ MORE

answered Aug 19, 2019 in Python by Noel Deepak Palle
1,864 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 963 views
0 votes
0 answers
0 votes
1 answer

How to return None if Dictionary key is not available?

Hello @kartik, You should use the get() method from the dict class d ...READ MORE

answered Apr 13 in Python by Niroj
• 23,950 points
32 views
0 votes
1 answer

Error: “ 'dict' object has no attribute 'iteritems' ”?

Hii, In Python2, dictionary.iteritems() is more efficient than ...READ MORE

answered Apr 13 in Python by Niroj
• 23,950 points
253 views