Anthraxff
Anthraxff

Reputation: 172

'numpy.ndarray' object is not callable error on 'AxesSubplot' object

I'm trying to draw several pie charts next to each other using this code:

import matplotlib.pyplot as plt

list_categorical_column = ['gender','race/ethnicity','parental level of education','lunch','test preparation course']
dict_data = df['gender'].value_counts()
fig, ((ax1,ax2),(ax3,ax4),(ax5,ax6)) = plt.subplots(3,2,figsize=(10,10)) 
ax_list = [ax1, ax2, ax3, ax4, ax5, ax6]
i = 0
for column in list_categorical_column :
    dict_data = df[column].value_counts()
    ax_list[i].pie(list(dict_data.keys()), list(dict_data.values()))
    ax_list[i].set_title(column)
    i += 1

plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace=0.5)
plt.show()

But I get this error :

TypeError                                 Traceback (most recent call last)
<ipython-input-9-d6c8c5c74b07> in <module>
      7 for column in list_categorical_column :
      8     dict_data = df[column].value_counts()
----> 9     ax_list[i].pie(list(dict_data.keys()), list(dict_data.values()))
     10     ax_list[i].set_title(column)
     11     i +=1

TypeError: 'numpy.ndarray' object is not callable

When I try to iterate through ax objects it returns this error:

ax1[1,1]
TypeError                                 Traceback (most recent call last)
<ipython-input-11-e981b338b40e> in <module>
      4 ax_list=[ax1,ax2,ax3,ax4,ax5,ax6]
      5 i=0
----> 6 ax1[1,1]
      7 for column in list_categorical_column :
      8     dict_data = df[column].value_counts()

TypeError: 'AxesSubplot' object is not subscriptable

What am I doing wrong here?

Upvotes: 0

Views: 176

Answers (1)

ptyshevs
ptyshevs

Reputation: 1672

pd.Series.value_counts method returns pd.Series type into dict_data. Hence, when you do dict_data.values(), you do a function call on pd.Series.values attribute, which has np.ndarray type. This should work:

import matplotlib.pyplot as plt

list_categorical_column = ['gender','race/ethnicity','parental level of education','lunch','test preparation course']
dict_data = df['gender'].value_counts()
fig, ((ax1,ax2),(ax3,ax4),(ax5,ax6)) = plt.subplots(3,2,figsize=(10,10)) 
ax_list = [ax1,ax2,ax3,ax4,ax5,ax6]
i = 0
for column in list_categorical_column:
    dict_data = df[column].value_counts().to_dict()
    ax_list[i].pie(list(dict_data.keys()), list(dict_data.values()))
    ax_list[i].set_title(column)
    i += 1

plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace=0.5)
plt.show()

Upvotes: 2

Related Questions