Home > Net >  Matplotlib : plt.legend() Throwing TypeError
Matplotlib : plt.legend() Throwing TypeError

Time:07-25

I am building a histogram from a pd.DataFrame. I am attempting to customize the legend, however, any call to plt.legend() throws a TypeError. Everything else regarding the graph is working as intended. Any thoughts as to why?

Code:

df.plot(x='Bucket', 
             kind='bar', 
             stacked=True,
             figsize=(10, 10),
          )


plt.ylabel('Emails', fontsize=20)
plt.xlabel('Character Length of Emails', fontsize=20)
plt.title('Email Topics vs Email Length', fontsize=30)
plt.xticks(rotation = 25, fontsize=15)
plt.yticks(rotation = 25, fontsize=15)

plt.legend()

Error :

 ---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-554-876a190bbc34> in <module>
     22 plt.yticks(rotation = 25, fontsize=15)
     23 
---> 24 plt.legend()
     25 
     26 

TypeError: 'list' object is not callable

The same error is thrown when I call

plt.legend([])
plt.legend(loc='upper center')

etc.

CodePudding user response:

Did you do this somewhere?

plt.legend = [1,2,3,4,5]

If you did, remove that line of code. I was able to duplicate your error by doing the below code:

df = pd.DataFrame({"Bucket":[1,2,3,4,5], "Y":[1,2,3,4,5]})

df.plot(x='Bucket', 
        kind='bar', 
        stacked=True,
        figsize=(10, 10),
       )

plt.legend = [1,2,3,4,5]
plt.ylabel('Emails', fontsize=20)
plt.xlabel('Character Length of Emails', fontsize=20)
plt.title('Email Topics vs Email Length', fontsize=30)
plt.xticks(rotation = 25, fontsize=15)
plt.yticks(rotation = 25, fontsize=15)

plt.legend()

Error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_26684/882015504.py in <module>
     14 plt.yticks(rotation = 25, fontsize=15)
     15 
---> 16 plt.legend()

TypeError: 'list' object is not callable
  • Related