Matplotlib pyplot.title(string) returns error

淺唱寂寞╮ 提交于 2019-11-29 16:39:49

问题


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?


回答1:


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.




回答2:


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 !




回答3:


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.




回答4:


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..




回答5:


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.



来源:https://stackoverflow.com/questions/19442060/matplotlib-pyplot-titlestring-returns-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!