调整带有子图(当前与图重叠)的图的全局图例(标签)- matplotlib,python

Adjusting global legend (labels) of a plot with subplots (which currently overlaps with the plot) - matplotlib, python

我正在尝试将全局图例添加到绘图中,下面的代码执行此操作,但图例随后与给定绘图重叠(我将在下面添加绘图)。有没有办法调整图例? Whosebug 的其他答案都集中在单个图上,而不是处理子图(或者我做错了什么)。

编辑:现在两个图重叠,因此红色的不可见。

fig, axes = plt.subplots(n, m, figsize=(15, 8))
for var, ax in zip(vars_to_plot, axes.flat):
    for solution in solutions:
        ax.plot(
            solution.summary_variables["Cycle number"],
            solution.summary_variables[var],
        )
    ax.set_xlabel("Cycle number")
    ax.set_ylabel(var)
    ax.set_xlim([1, solution.summary_variables["Cycle number"][-1]])

fig.tight_layout()
fig.legend(['this is plot 1', 'this is plot 2'], loc='lower right')
plt.savefig("plot.png", dpi=300)

The plot

建议你把图例放在图外:

fig.legend(['this is plot 1', 'this is plot 2'], loc='lower left', 
           bbox_to_anchor=(1.0, 0.05))

然后用 plt.savefig("plot.png", bbox_inches='tight') 保存图形,使图形扩展以包含图例。

你还可以做其他事情,但我认为这是最简单的...