I’m using
plt.legend(bbox_to_anchor = (1,1))
to put the legend outside my figure. The journal to which I’m submitting requires specific sizes for the figures. When I use this method, it increases the total width of my figure beyond the required size. I want to have the figure sized exactly to specification. Is there a way to calculate the total width of the figure including the external legend, so that I can reduce my figsize parameter accordingly?
Advertisement
Answer
The following works fine; I’ve just coloured the figure so you can see its size.
JavaScript
x
10
10
1
import matplotlib.pyplot as plt
2
import numpy as np
3
import matplotlib as mpl
4
5
fig, ax = plt.subplots(figsize=(3, 3), constrained_layout=True)
6
fig.set_facecolor('0.5')
7
ax.plot(np.arange(10), label='Boo')
8
ax.legend(bbox_to_anchor=(1, 1))
9
fig.savefig('boo.png')
10