57

When I call pyplot.title('some string') it throws the exception, 'str' object is not callable'. I copied the following from the matplotlib online documentation:

mu, sigma = 100, 15
x = mu + sigma * np.random.randn(10000)

# the histogram of the data
n, bins, patches = plt.hist(x, 50, normed=1, facecolor='g', alpha=0.75)


plt.xlabel('Smarts')
plt.ylabel('Probability')
plt.title('Histogram of IQ')
plt.text(60, .025, r'$\mu=100,\ \sigma=15$')
plt.axis([40, 160, 0, 0.03])
plt.grid(True)
plt.show()

and get

TypeError                                 Traceback (most recent call last)
<ipython-input-158-40fe7a831b06> in <module>()
      8 plt.xlabel('Smarts')
      9 plt.ylabel('Probability')
---> 10 plt.title('Histogram of IQ')
     11 plt.text(60, .025, r'$\mu=100,\ \sigma=15$')
     12 plt.axis([40, 160, 0, 0.03])

TypeError: 'str' object is not callable

pyplot.suptitle() works OK

I'm using python 2.7.5 and the latest release of matplotlib on an iMac with an I7 processor OSX 10.8 and 8 gig ram and ipython notebook.

Does anyone know what's going on?

3
  • 1
    It works for me on OSX 10.8.5 with Python 2.7.5 and matplotlib 1.3.0. How did you install ipython and matplotlib? Is the above code the only code you evoke? Do you run ipython notebook --pylab or how do you initialize it? Commented Oct 18, 2013 at 8:25
  • 1
    I also can not re-produce this. Do you have a plt.title = 'blah' someplace in your code above this?
    – tacaswell
    Commented Oct 18, 2013 at 15:19
  • 1
    I reinstalled matplotlib (pip uninstall then pip install) and all is OK. Before this ?plt.title told me that plt.title was a string. Now it confirms that plt.title is a function. Not sure I undetstand all of it but it's working. Thanks guys.
    – olben1
    Commented Oct 18, 2013 at 15:50

13 Answers 13

131

It happened to me because I tried to do plot.title = "Some string" so that rewrote the title() method. That's the exact reason why it happens :) . As others have said you just need to restart the kernel, no need to reinstall.

3
  • 13
    This should be the accepted answer here... having rewritten the 'title' is the root cause to the probem
    – Kickaha
    Commented May 3, 2016 at 13:58
  • 2
    I accidentally assigned the title method to a string and that caused this issue. After restarted the kernel it worked.
    – smishra
    Commented Oct 10, 2020 at 14:04
  • 4
    Thank you! I think I made the same mistake, and restarting the kernel solved my problem also.
    – KBurchfiel
    Commented Mar 9, 2021 at 17:54
30

I had the same problem. The code was fine, but in the interpreter, I had previoulsy used incorrect xlabel() calls. re-starting the interpreter (close and reopen it) was enough for me, no need to reinstall all python/matplotlib !

1
  • 1
    Same problem - I'm working in Jupyter and once I restarted the kernel I was all set.
    – nanselm2
    Commented Jul 10, 2017 at 21:41
14

I've had this happen when I've previously accidentally plt.title = ''

After that that function is no longer a function. Restarting python kernel or re-importing plt library can fix it.

Not re-installing. Re-IMPORTING.

1
  • No kidding. If you previously accidentally assign a string to function it becomes a string?? Commented Mar 15, 2019 at 9:18
12

Try to reload matplotlib by running the following code:

import matplotlib.pyplot as plt
from importlib import reload
plt=reload(plt)

Good Luck

5

I encountered the same problem where I wrote plt.title = "This is a sample plot" instead of plt.title("This is a sample plot"). I was getting the error:

typeerror 'str' object is not callable

Restarting the Spyder IDE fixed the issue for me.

4

No need to Reinstall any libraries. To overcome the issue, you can just restart the Jupyter kernel. This happens when you set plt.title = 'something'. It overrides the PyPlot functions and make them inaccessible. Restarting the kernel will help you out.

2

Without restarting, I've found that changing to a .set_title() method following my plotting method has resulted in a successful pass without an error.

2
  1. Restart your kernel

  2.  plt.suptitle("title name") 
    
  3. import matplotlib.pyplot as plt
    from importlib import reload
    plt=reload(plt)
    
2
  • the code is not well organized Commented Apr 6, 2022 at 11:50
  • Reloading worked great! But I didn't need to restart the kernel.
    – albielin
    Commented Oct 13, 2022 at 23:52
1

Had the same problem olben1, using ipython, anaconda and the --pylab flag. Reinstalled and it worked. Fwiw, using an anaconda env makes the uninstall/reinstall easier..

1

I experienced the same problem after removing the line

%matplotlib inline

and Restarting the notebook again solved my problem.

0

plt.title('xyz') :'str' object is not callable.

just give a restart of your terminal/ide. That ll fix the issue .

0

This issue is not only with plt.title() but also with plt.xlabel() and plt.ylabe(). To solve this issue you need to add:

%matplotlib inline

at the time of importing. this is to enable the addition of codes on the go. Then add the code:

import matplotlib.pyplot as plt

from importlib import reload

plt = reload(plt)

Then write the rest of the code.

New contributor
thumssypn is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
-1

Just restart the kernel. It works. Worked for me.

1
  • At least three other answers already mention this. Please don't repeat answers.
    – Chris
    Commented Nov 15, 2023 at 3:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.