Python 同步图例在多个matplotlib轴中的位置
如何使用多个轴同步matplotlib中“最佳”图例位置的查找Python 同步图例在多个matplotlib轴中的位置,python,matplotlib,legend,Python,Matplotlib,Legend,如何使用多个轴同步matplotlib中“最佳”图例位置的查找 import matplotlib.pyplot as plt fig, axes = plt.subplots(nrows = 2) axes[0].plot([0, 1], [0, 1], label="label1") axes[1].plot([0, 1], [1, 0], label="label2") for ax in axes: ax.legend(loc="best") 上面的代码将创建两个轴,其中“最
import matplotlib.pyplot as plt
fig, axes = plt.subplots(nrows = 2)
axes[0].plot([0, 1], [0, 1], label="label1")
axes[1].plot([0, 1], [1, 0], label="label2")
for ax in axes:
ax.legend(loc="best")
上面的代码将创建两个轴,其中“最佳”位置将分别针对每个图进行评估。但是,当有一个由多个图组成的网格时,对于每个子图,自动将位置放置在同一个点上(即,此处loc=5就可以了)将是我要搜索的
显然,解决办法不是让算法决定最佳位置,而是手动设置位置。然而,在我的例子中,这个位置因模型而异,因此我希望避免使用这个选项。第二种解决方法是,将图例放置在轴外每个子图的固定位置。
“best”
适用于每个轴。因此,如果你不想这样做,你需要自己指定位置。你可以运行第一个绘图,查看第一个图例的最佳放置位置,然后强制其他图例执行相同的操作。