将matplotlib标签放在subp的上方

2024-04-29 13:11:08 发布

您现在位置:Python中文网/ 问答频道 /正文

现在默认情况下,matplotlib legend位于所有子图的右上角(比如一组水平3×垂直3)。在

然而,在某些情况下,图例会模糊一些趋势线,所以我想把图例放在子区上方,这可能吗?在


Tags: matplotlib水平情况趋势图例legend
1条回答
网友
1楼 · 发布于 2024-04-29 13:11:08

图例的位置是使用loc参数指定给^{}。您可以尝试使用'best'选项,看看它是否避免了这些行。如果要将图例框放在绘图区域之外,可以查看legend guide中描述的技术。在

下面是一个三乘三的子地块网格,图右上方有一个图例:

import matplotlib.pyplot as plt
for i in range(9):
    plt.subplot(3, 3, i+1)
    plt.plot([0, 1], [0, 1])
plt.subplot(3, 3, 3)
plt.legend(['test'], bbox_to_anchor=(0, 1, 1, 0.5), mode='expand')
plt.tight_layout()

enter image description here

相关问题 更多 >