Closed
Description
Originally posted by @ImportanceOfBeingErnest in #12377 (comment)
The code does not seem to be runnable and does not give a sensible result. I used this code
fig, axs = plt.subplots(2, 2, constrained_layout=True)
for ax in axs.flatten()[:-1]:
ax.plot(np.arange(10))
axs[1, 1].plot(np.arange(10), label='This is a plot')
leg = axs[1, 1].legend(loc='center left', bbox_to_anchor=(0.8, 0.5))
leg.set_in_layout(False)
wanttoprint = True
if wanttoprint:
leg.set_in_layout(True)
fig.do_constrained_layout(False)
fig.savefig('outname.png', bbox_inches='tight')
plt.show()
i.e. the code from the matplotlib 3.0 documentation with wanttoprint = True
but it gives an error AttributeError: 'Figure' object has no attribute 'do_constrained_layout'
.
Assuming that what is meant here is set_constrained_layout
, the problem is that the figure shown deviates from the one obtained via wanttoprint = False
. So it seems it's not possible to show the figure after saving it?
Using the second code (the one with fig.legend()
) everything works as expected.
In my eyes there are the following things to do:
- Correct the first code to be runnable and produce the expected outcome for both settings of
wanttoprint
on screen and as saved image. - Explain the reasoning behind turning the legend in layout option first off then on etc.
- Add a savefig line to the second example to make is clear that this is to be understood parallel to the previous example.