栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

使用matplotlib绘图添加标题title时出现TypeError: ‘Text‘ object is not callable,这么改就对了

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用matplotlib绘图添加标题title时出现TypeError: ‘Text‘ object is not callable,这么改就对了

文章目录
  • 错误复现
  • 报错
  • 解决办法
  • plt.title和ax.set_title
  • 添加title的两种方式
  • see also

错误复现
import matplotlib.pyplot as plt
import numpy as np
import math

x = np.arange(1000)
y = np.sin(2*math.pi*x/1000)

fig, ax = plt.subplots()
ax.plot(x, y)
ax.title("sine")  # 报错:TypeError: 'Text' object is not callable
报错
TypeError: 'Text' object is not callable
解决办法

仔细翻阅matplotlib的官方API手册发现ax根本没有title这个接口,正确的接口是set_tile。所以,ax.title → ax.set_tile,就行了。

plt.title和ax.set_title

matplotlib的官方API手册还是很有用的,仔细看plt.title和ax.set_title实现的是同样的功能。很想给作者团队提意见:为什么不统一接口?

不过使用者也应该认真阅读手册啊。

看不懂手册怎么办?没关系,关注我就行啦,带你看手册。

添加title的两种方式
import matplotlib.pyplot as plt
import numpy as np
import math

x = np.arange(1000)
y = np.sin(2*math.pi*x/1000)


# 使用plt.title
plt.plot(x, y)
plt.title("sine")


# 使用ax.set_tile
fig, ax = plt.subplots()
ax.plot(x, y)
# ax.title("sine")  # 报错:TypeError: 'Text' object is not callable
ax.set_title("sine")  # 正确的打开方式
see also
  1. matplotlib官方API手册:ax.set_title
  2. matplotlib官方API手册:plt.title
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/329203.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号