You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Matplotlib调用plt.xticks时出现TypeError: 'int' object is not callable的问题求助

Matplotlib调用plt.xticks时出现TypeError: 'int' object is not callable的问题求助

各位大佬好,我在运行KNN模型的可视化代码时遇到了一个类型错误,折腾半天没找到原因,想请大家帮忙看看。

我用到的绘图代码如下:

plt.plot(neighbors, train_scores, label="Train Score")
plt.plot(neighbors, test_scores, label="Test Score")
plt.xticks(np.arange(1,21,1))
plt.xlabel("Number of Neighbors")
plt.ylabel("Score")
plt.legend()
plt.show()

还有一段打印最高分数的代码:

print(f"Maximum KNN score on the test data is: {max(test_scores)*100:.2f}%")

运行后出现了如下错误:

it gives me a Type error

TypeError                                 Traceback (most recent call last)
Cell In[62], line 5
      3 plt.plot(neighbors, train_scores, label="Train Score")
      4 plt.plot(neighbors, test_scores, label="Test Score")
----> 5 plt.xticks(np.arange(1,21,1))
      6 plt.xlabel("Number of Neighbors")
      7 plt.ylabel("Score")

TypeError: 'int' object is not callable

我已经检查过有没有不小心把xticks当成变量名使用,但确认没有这种情况,实在不知道问题出在哪了,求各位帮忙指点!

备注:内容来源于stack exchange,提问作者Atharv Patil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.16 11:44:39