图例三行三列显示 python
时间: 2024-01-11 13:21:21 浏览: 19
以下是使用Matplotlib库在Python中实现图例三行三列显示的示例代码:
```python
import matplotlib.pyplot as plt
# 创建一个3x3的子图布局
fig, axs = plt.subplots(3, 3)
# 在每个子图中绘制示例图形
axs[0, 0].plot([1, 2, 3, 4], [1, 4, 2, 3])
axs[0, 0].set_title('Plot 1')
axs[0, 1].scatter([1, 2, 3, 4], [1, 4, 2, 3])
axs[0, 1].set_title('Plot 2')
axs[0, 2].bar([1, 2,3, 4], [1, 4, 2, 3])
axs[0, 2].set_title('Plot 3')
axs[1, 0].hist([1, 2, 3, 4])
axs[1, 0].set_title('Plot 4')
axs[1, 1].plot([1, 2, 3, 4], [4, 3, 2, 1])
axs[1, 1].set_title('Plot 5')
axs[1, 2].scatter([1, 2, 3, 4], [4, 3, 2, 1])
axs[1, 2].set_title('Plot 6')
axs[2, 0].bar([1, 2, 3, 4], [4, 3, 2, 1])
axs[2, 0].set_title('Plot 7')
axs[2, 1].hist([1, 2, 3, 4])
axs[2, 1].set_title('Plot 8')
axs[2, 2].plot([1, 2, 3, 4], [1, 2, 1, 2])
axs[2, 2].set_title('Plot 9')
# 调整子图之间的间距
plt.tight_layout()
# 显示图例
plt.legend()
# 显示图形
plt.show()
```
这段代码创建了一个3x3的子图布局,并在每个子图中绘制了不同的示例图形。通过调整子图之间的间距和显示图例,可以实现图例三行三列显示的效果。