我对 Python 和一般编程都很陌生,所以如果我遗漏了一些明显的东西,请提前道歉。我正在尝试绘制图表并标记轴,但每次尝试标记 y 轴时都会引发异常。我在新脚本中编写了下面的代码,以确保问题不是来自模块中的其他地方。我正在使用Python 3.4。
from numpy import *
from matplotlib import *
a = [1, 2, 3, 4, 5]
b = [2, 3, 2, 3, 2]
pyplot.plot(a, b)
pylab.xlabel("Time")
pylab.ylabel("Speed")
每次,我都会在最后一行收到错误“TypeError: 'str' object is not callable”。如果我将 y 更改为 x,一切都很好。如果我将 x 更改为 y,则会出现相同的错误。但是, ylabel 出现在 ylabel 的下拉列表中,因此该函数确实存在,并且文档说字符串是唯一必要的参数,与 xlabel 完全相同(matplotlib.pyplot.ylabel(s, *args, **kwargs) 和matplotlib.pyplot.xlabel(s, *args, **kwargs))。这里到底发生了什么?
我在 iPython 笔记本中工作时遇到了同样的问题。
我认为可以按如下方式重新创建:
import matplotlib.pyplot as plt
plt.ylabel = 'somestring' # oh wait this isn't the right syntax.
...
plt.ylabel('somestring') # now this breaks because the function has been turned into a string
重新启动内核或重新导入库会将 plt.ylabel 恢复为函数。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)