Python Matplotlib pyplot.title(string) 返回错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19442060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:45:10  来源:igfitidea点击:

Matplotlib pyplot.title(string) returns error

pythonmatplotlibipython-notebook

提问by olben1

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:

当我调用pyplot.title('some string')它时抛出异常,'str' object is not callable'. 我从 matplotlib 在线文档中复制了以下内容:

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

pyplot.suptitle()工作正常

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.

我在带有 I7 处理器 OSX 10.8 和 8 gig ram 以及 ipython 笔记本的 iMac 上使用 python 2.7.5 和最新版本的 matplotlib。

Does anyone know what's going on?

有谁知道发生了什么?

回答by user3178165

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

有同样的问题 olben1,使用 ipython、anaconda 和 --pylab 标志。重新安装,它工作。Fwiw,使用 anaconda env 使卸载/重新安装更容易..

回答by fplandes

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 !

我有同样的问题。代码很好,但是在解释器中,我以前使用了不正确的 xlabel() 调用。重新启动解释器(关闭并重新打开它)对我来说就足够了,无需重新安装所有 python/matplotlib !

回答by steven2308

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.

它发生在我身上,因为我试图这样plot.title = "Some string"做重写了title()方法。这就是它发生的确切原因:)。正如其他人所说,您只需要重新启动内核,无需重新安装。

回答by Jonathan Anderson

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

我以前不小心 plt.title = '' 时发生过这种情况

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

之后那个函数就不再是函数了。重新启动python内核或重新导入plt库可以解决它。

Not re-installing. Re-IMPORTING.

不是重新安装。重新进口。

回答by alofgran

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.

无需重新启动,我发现更改为.set_title()遵循我的绘图方法的方法已成功通过而没有错误。