[英]Adjusting global legend (labels) of a plot with subplots (which currently overlaps with the plot) - matplotlib, python
我正在尝试向绘图添加全局图例,下面的代码执行此操作,但图例随后与给定的绘图重叠(我将在下面添加绘图)。 有没有办法调整图例? 其他 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)
建议你把图例放在图外:
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')
保存图形以允许图形扩展以包含图例。
您还可以做其他事情,但我认为这是最简单的...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.