0

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

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

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)

剧情

4

1 回答 1

0

建议你把图例放在图外:

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')以允许图形扩展以包含图例。

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

于 2021-07-06T15:42:48.127 回答