Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
180 views
in Technique[技术] by (71.8m points)

python - Matplotlib pyplot.title(string) returns error

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...