matplotlib设置标题
时间: 2023-11-28 12:45:28 浏览: 70
以下是两种在matplotlib中设置标题的方法:
1. 设置子图标题和总标题
```python
import matplotlib.pyplot as plt
import numpy as np
# 创建两个子图
fig, (ax1, ax2) = plt.subplots(1, 2)
# 在每个子图中添加数据
x = np.linspace(0, 2 * np.pi, 100)
y1 = np.sin(x)
y2 = np.cos(x)
ax1.plot(x, y1)
ax2.plot(x, y2)
# 设置子图标题
ax1.set_title('Sine')
ax2.set_title('Cosine')
# 设置总标题
fig.suptitle('Sine and Cosine Curves')
# 显示图形
plt.show()
```
2. 设置一个大标题来概括多个子图
```python
import matplotlib.pyplot as plt
import numpy as np
# 创建两个子图
fig, (ax1, ax2) = plt.subplots(1, 2)
# 在每个子图中添加数据
x = np.linspace(0, 2 * np.pi, 100)
y1 = np.sin(x)
y2 = np.cos(x)
ax1.plot(x, y1)
ax2.plot(x, y2)
# 设置大标题
fig.suptitle('Sine and Cosine Curves', fontsize=16, fontweight='bold', y=1.05)
# 调整子图位置
fig.subplots_adjust(top=0.8)
# 显示图形
plt.show()
```
阅读全文
相关推荐


















