I am trying to add a global legend to a plot, the code below does that but the legend then overlaps with the given plot (I’ll add the plot below). Is there a way to adjust the legends? The other StackOverflow answers are focused on a single plot and are not working with subplots (or maybe I am doing something wrong).
Edit: Right now both the graphs are overlapping and hence the red one isn’t visible.
JavaScript
x
15
15
1
fig, axes = plt.subplots(n, m, figsize=(15, 8))
2
for var, ax in zip(vars_to_plot, axes.flat):
3
for solution in solutions:
4
ax.plot(
5
solution.summary_variables["Cycle number"],
6
solution.summary_variables[var],
7
)
8
ax.set_xlabel("Cycle number")
9
ax.set_ylabel(var)
10
ax.set_xlim([1, solution.summary_variables["Cycle number"][-1]])
11
12
fig.tight_layout()
13
fig.legend(['this is plot 1', 'this is plot 2'], loc='lower right')
14
plt.savefig("plot.png", dpi=300)
15
Advertisement
Answer
Suggest you put the legend outside the figure:
JavaScript
1
3
1
fig.legend(['this is plot 1', 'this is plot 2'], loc='lower left',
2
bbox_to_anchor=(1.0, 0.05))
3
and then save the figure with plt.savefig("plot.png", bbox_inches='tight')
to allow the figure to expand to encompass the legend.
There are other things you can do, but I think this is the simplest…