xlabel、ylabel无效。'str'对象不可调用。

4

我正在尝试添加坐标轴标签,但由于语法错误,它们无法显示。这是我的代码。

file = 'SAW data omgekeerd goed.csv'
data = pd.read_csv(file, sep = ';', parse_dates= True, index_col = 0, dayfirst = True)

plt.plot(data['0.005 (B4.01) Studielandschap'])
plt.xlabel('Month')
plt.ylabel('Value')
plt.show()

文件"C:/Users/steven/Documents/BIM/Minor Data Science/Outlier detection SAW/Anomaly detection 0.1.py",第24行,plt.xlabel('Month'),出现错误:TypeError: 'str' object is not callable。


2
完整的错误是什么? - DavidG
2
我可能不小心编辑掉了语法错误... 在 plt.xlabel('Month')' 之后有一个撇号。这可能是原因,所以我已经把它放回去,直到 OP 澄清。 - DavidG
错误: File "C:/Users/steven/Documents/BIM/Minor Data Science/Outlier detection SAW/Anomaly detection 0.1.py", line 24, in <module> plt.xlabel('Month')TypeError: 'str' object is not callable我删除了撇号,现在它给出了不同的错误。Plt可以用于创建图表。 - Steven Wetstein
重新启动了Spyder,现在它可以正常运行了。我应该先这样做的,因为我对编程还很陌生。抱歉。 - Steven Wetstein
4个回答

20

在此之前,您是否执行了plt.xlabel='month'? 这将导致导入的函数被改变,从而导致类型错误。 重新启动并运行应该没问题。

编辑:如果由于某些原因您不想重新启动。 您可以通过重新加载模块来解决。

from importlib import reload
matplotlib=reload(matplotlib)

如果版本不是3.4+,请使用imp而不是importlib。


2
这个问题通常发生在导入函数被更改的情况下。 如果我们使用plt.ylabel='test'代替plt.ylabel('test'),那么可能会改变导入。 为了解决这个问题,只需重新启动内核即可。

1
如果您尝试这样做
    plt.xlabel = "Label"
    plt.ylabel = "Label"

还有这个。
    plt.xlabel("Label")
    plt.ylabel("Label")

还是不起作用?那就试试这个。
    import matplotlib.pyplot as plt
    from importlib import reload
    plt=reload(plt)

祝你好运


0

也许你使用

plt.xlabel = "something"

所以你和我有问题。

我使用的是anaconda-jupyter,我的解决方案是:

  1. 点击“运行” - 点击“重新启动内核并运行所有单元格…”

enter image description here

然后,问题就会消失,因为您重新启动了内核。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接