Skip to content

Commit 1a814fc

Browse files
authored
Merge pull request #12413 from meeseeksmachine/auto-backport-of-pr-12394-on-v3.0.0-doc
Backport PR #12394 on branch v3.0.0-doc (DOC: fix CL tutorial to give same output from saved file and example)
2 parents d50fbeb + e30d055 commit 1a814fc

File tree

4 files changed

+38
-22
lines changed

4 files changed

+38
-22
lines changed
37.6 KB
Loading
10.3 KB
Loading
10.3 KB
Loading

tutorials/intermediate/constrainedlayout_guide.py

+38-22
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,10 @@ def example_plot(ax, fontsize=12, nodec=False):
194194
#############################################
195195
# However, this will steal space from a subplot layout:
196196

197-
fig, axs = plt.subplots(2, 2, constrained_layout=True)
198-
for ax in axs.flatten()[:-1]:
199-
ax.plot(np.arange(10))
200-
axs[1, 1].plot(np.arange(10), label='This is a plot')
201-
axs[1, 1].legend(loc='center left', bbox_to_anchor=(0.8, 0.5))
197+
fig, axs = plt.subplots(1, 2, figsize=(4, 2), constrained_layout=True)
198+
axs[0].plot(np.arange(10))
199+
axs[1].plot(np.arange(10), label='This is a plot')
200+
axs[1].legend(loc='center left', bbox_to_anchor=(0.8, 0.5))
202201

203202
#############################################
204203
# In order for a legend or other artist to *not* steal space
@@ -207,30 +206,47 @@ def example_plot(ax, fontsize=12, nodec=False):
207206
# cropped, but can be useful if the plot is subsequently called
208207
# with ``fig.savefig('outname.png', bbox_inches='tight')``. Note,
209208
# however, that the legend's ``get_in_layout`` status will have to be
210-
# toggled again to make the saved file work:
209+
# toggled again to make the saved file work, and we must manually
210+
# trigger a draw if we want constrained_layout to adjust the size
211+
# of the axes before printing.
211212

212-
fig, axs = plt.subplots(2, 2, constrained_layout=True)
213-
for ax in axs.flatten()[:-1]:
214-
ax.plot(np.arange(10))
215-
axs[1, 1].plot(np.arange(10), label='This is a plot')
216-
leg = axs[1, 1].legend(loc='center left', bbox_to_anchor=(0.8, 0.5))
213+
fig, axs = plt.subplots(1, 2, figsize=(4, 2), constrained_layout=True)
214+
215+
axs[0].plot(np.arange(10))
216+
axs[1].plot(np.arange(10), label='This is a plot')
217+
leg = axs[1].legend(loc='center left', bbox_to_anchor=(0.8, 0.5))
217218
leg.set_in_layout(False)
218-
wanttoprint = False
219-
if wanttoprint:
220-
leg.set_in_layout(True)
221-
fig.do_constrained_layout(False)
222-
fig.savefig('outname.png', bbox_inches='tight')
219+
# trigger a draw so that constrained_layout is executed once
220+
# before we turn it off when printing....
221+
fig.canvas.draw()
222+
# we want the legend included in the bbox_inches='tight' calcs.
223+
leg.set_in_layout(True)
224+
# we don't want the layout to change at this point.
225+
fig.set_constrained_layout(False)
226+
fig.savefig('CL01.png', bbox_inches='tight', dpi=100)
223227

224228
#############################################
229+
# The saved file looks like:
230+
#
231+
# .. image:: /_static/constrained_layout/CL01.png
232+
# :align: center
233+
#
225234
# A better way to get around this awkwardness is to simply
226-
# use a legend for the figure:
227-
fig, axs = plt.subplots(2, 2, constrained_layout=True)
228-
for ax in axs.flatten()[:-1]:
229-
ax.plot(np.arange(10))
230-
lines = axs[1, 1].plot(np.arange(10), label='This is a plot')
235+
# use the legend method provided by `.Figure.legend`:
236+
fig, axs = plt.subplots(1, 2, figsize=(4, 2), constrained_layout=True)
237+
axs[0].plot(np.arange(10))
238+
lines = axs[1].plot(np.arange(10), label='This is a plot')
231239
labels = [l.get_label() for l in lines]
232240
leg = fig.legend(lines, labels, loc='center left',
233-
bbox_to_anchor=(0.8, 0.5), bbox_transform=axs[1, 1].transAxes)
241+
bbox_to_anchor=(0.8, 0.5), bbox_transform=axs[1].transAxes)
242+
fig.savefig('CL02.png', bbox_inches='tight', dpi=100)
243+
244+
#############################################
245+
# The saved file looks like:
246+
#
247+
# .. image:: /_static/constrained_layout/CL02.png
248+
# :align: center
249+
#
234250

235251
###############################################################################
236252
# Padding and Spacing

0 commit comments

Comments
 (0)