diff --git a/doc/_static/constrained_layout/CL00.png b/doc/_static/constrained_layout/CL00.png new file mode 100644 index 000000000000..5963429df0f8 Binary files /dev/null and b/doc/_static/constrained_layout/CL00.png differ diff --git a/doc/_static/constrained_layout/CL01.png b/doc/_static/constrained_layout/CL01.png new file mode 100644 index 000000000000..5b08c6be2425 Binary files /dev/null and b/doc/_static/constrained_layout/CL01.png differ diff --git a/doc/_static/constrained_layout/CL02.png b/doc/_static/constrained_layout/CL02.png new file mode 100644 index 000000000000..bba3cf15b891 Binary files /dev/null and b/doc/_static/constrained_layout/CL02.png differ diff --git a/tutorials/intermediate/constrainedlayout_guide.py b/tutorials/intermediate/constrainedlayout_guide.py index b1888d548522..f806f388d275 100644 --- a/tutorials/intermediate/constrainedlayout_guide.py +++ b/tutorials/intermediate/constrainedlayout_guide.py @@ -200,11 +200,10 @@ def example_plot(ax, fontsize=12, nodec=False): ############################################# # However, this will steal space from a subplot layout: -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') -axs[1, 1].legend(loc='center left', bbox_to_anchor=(0.8, 0.5)) +fig, axs = plt.subplots(1, 2, figsize=(4, 2), constrained_layout=True) +axs[0].plot(np.arange(10)) +axs[1].plot(np.arange(10), label='This is a plot') +axs[1].legend(loc='center left', bbox_to_anchor=(0.8, 0.5)) ############################################# # In order for a legend or other artist to *not* steal space @@ -213,30 +212,47 @@ def example_plot(ax, fontsize=12, nodec=False): # cropped, but can be useful if the plot is subsequently called # with ``fig.savefig('outname.png', bbox_inches='tight')``. Note, # however, that the legend's ``get_in_layout`` status will have to be -# toggled again to make the saved file work: +# toggled again to make the saved file work, and we must manually +# trigger a draw if we want constrained_layout to adjust the size +# of the axes before printing. -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)) +fig, axs = plt.subplots(1, 2, figsize=(4, 2), constrained_layout=True) + +axs[0].plot(np.arange(10)) +axs[1].plot(np.arange(10), label='This is a plot') +leg = axs[1].legend(loc='center left', bbox_to_anchor=(0.8, 0.5)) leg.set_in_layout(False) -wanttoprint = False -if wanttoprint: - leg.set_in_layout(True) - fig.do_constrained_layout(False) - fig.savefig('outname.png', bbox_inches='tight') +# trigger a draw so that constrained_layout is executed once +# before we turn it off when printing.... +fig.canvas.draw() +# we want the legend included in the bbox_inches='tight' calcs. +leg.set_in_layout(True) +# we don't want the layout to change at this point. +fig.set_constrained_layout(False) +fig.savefig('CL01.png', bbox_inches='tight', dpi=100) ############################################# +# The saved file looks like: +# +# .. image:: /_static/constrained_layout/CL01.png +# :align: center +# # A better way to get around this awkwardness is to simply -# use a legend for the figure: -fig, axs = plt.subplots(2, 2, constrained_layout=True) -for ax in axs.flatten()[:-1]: - ax.plot(np.arange(10)) -lines = axs[1, 1].plot(np.arange(10), label='This is a plot') +# use the legend method provided by `.Figure.legend`: +fig, axs = plt.subplots(1, 2, figsize=(4, 2), constrained_layout=True) +axs[0].plot(np.arange(10)) +lines = axs[1].plot(np.arange(10), label='This is a plot') labels = [l.get_label() for l in lines] leg = fig.legend(lines, labels, loc='center left', - bbox_to_anchor=(0.8, 0.5), bbox_transform=axs[1, 1].transAxes) + bbox_to_anchor=(0.8, 0.5), bbox_transform=axs[1].transAxes) +fig.savefig('CL02.png', bbox_inches='tight', dpi=100) + +############################################# +# The saved file looks like: +# +# .. image:: /_static/constrained_layout/CL02.png +# :align: center +# ############################################################################### # Padding and Spacing