Aunque este método funciona con la leyenda, parece no estar funcionando bien con figlegend cuando hay múltiples subtramas y queremos una sola leyenda general. figlegend aún se recorta cuando savefig. Acabo de pegar mi solución temporal a continuación en caso de que alguien se enfrenta a un caso así.
import matplotlib.pyplot as plt
para = {
## this parameter will indicate the position of
## subplot within figure, but will not be shown
## if using bbox_inches='tight' when saving
'figure.subplot.top': 0.5
}
#plt.rcParams.update(para)
fig = plt.figure()
ax=fig.add_subplot(221)
## only needed when what to manually control
## subplot ration
#ax.set_position([0.1,0.6,0.5, 0.4])
ax.plot([1,1,1])
ax=fig.add_subplot(222)
#ax.set_position([0.7,0.6,0.5, 0.4])
ax.plot([2,2,2])
ax=fig.add_subplot(223)
#ax.set_position([0.1,0.1,0.5, 0.4])
ax.plot([3,3,3])
ax=fig.add_subplot(224)
#ax.set_position([0.7,0.1,0.5, 0.4])
p1, = ax.plot([4,4,4])
p2, = ax.plot([2,3,2])
## figlegend does not work fine with tight bbox
## the legend always get cropped by this option
## even add bbox extra will not help
## had to use legend, and manually adjust it to
## arbitary position such as (0.3, 2.5)
## http://matplotlib.org/users/tight_layout_guide.html
## according to this link, tight layout is only
## an experimental feature, might not support figlegend
#lgd = plt.figlend(
lgd = plt.legend(
[p1,p2],
['a', 'b'],
## by default, legend anchor to axis, but can
## also be anchored to arbitary position
## positions within [1,1] would be within the figure
## all numbers are ratio by default
bbox_to_anchor=(-0.1, 2.5),
## loc indicates the position within the figure
## it is defined consistent to the same Matlab function
loc='center',
ncol=2
#mode="expand",
#borderaxespad=0.
)
#plt.show()
plt.savefig('temp.png', bbox_inches='tight')#, bbox_extra_artist=[lgd])
(Veo esto es viejo hilo, pero es en primer lugar en Google) Hay una mejor solución al incluir actores para guardar: http://stackoverflow.com/questions/10101700/moving-matplotlib-legend-outside-of-the-axis-makes-it-cutoff-by-the-figure-box – Alleo