在有多个子图的图片中任意位置添加文字
时间: 2024-03-02 21:53:11 浏览: 111
你可以使用`fig.text()`函数在画布的任意位置添加文字。首先,你需要获取子图对象,然后使用子图对象的方法添加文字。例如:
```
import matplotlib.pyplot as plt
fig, axs = plt.subplots(2, 2, figsize=(10, 8))
# 在第一个子图中添加文字
axs[0, 0].text(0.5, 0.5, 'Hello World!', fontsize=20, ha='center', va='center')
# 在第二个子图中添加文字
axs[0, 1].text(0.2, 0.8, 'Text', fontsize=15, ha='left', va='center')
# 在第三个子图中添加文字
axs[1, 0].text(0.8, 0.2, 'Python', fontsize=25, ha='center', va='top')
# 在第四个子图中添加文字
axs[1, 1].text(0.1, 0.1, 'Matplotlib', fontsize=18, ha='left', va='bottom')
plt.show()
```
这段代码创建了一个2x2的子图,然后在每个子图的不同位置添加了不同的文字。你可以根据自己的需要修改坐标、字体大小、颜色等参数。
相关问题
在有多个子图的图片中,在两子图位置添加文字
你可以使用`fig.text()`函数在画布的任意位置添加文字。首先,你需要获取子图对象,然后使用子图对象的方法添加文字。例如:
```
import matplotlib.pyplot as plt
fig, axs = plt.subplots(2, 2, figsize=(10, 8))
# 在第一个子图中添加文字
axs[0, 0].text(0.5, 0.5, 'Hello World!', fontsize=20, ha='center', va='center')
# 在第二个子图中添加文字
axs[0, 1].text(0.2, 0.8, 'Text', fontsize=15, ha='left', va='center')
# 在第三个子图中添加文字
axs[1, 0].text(0.8, 0.2, 'Python', fontsize=25, ha='center', va='top')
# 在第四个子图中添加文字
axs[1, 1].text(0.1, 0.1, 'Matplotlib', fontsize=18, ha='left', va='bottom')
# 在两个子图之间添加文字
fig.text(0.48, 0.5, 'This is between the two subplots', fontsize=16, ha='center', va='center')
plt.show()
```
这段代码创建了一个2x2的子图,然后在每个子图的不同位置添加了不同的文字。最后,使用`fig.text()`函数在两个子图中间添加了一条文字。你可以根据自己的需要修改坐标、字体大小、颜色等参数。
阅读全文
相关推荐

















