Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8ba6651

Browse files
Merge pull request #12394 from jklymak/doc-fix-cl-tutorial
DOC: fix CL tutorial to give same output from saved file and example
2 parents aac5726 + d71b44b commit 8ba6651

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

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,10 @@ def example_plot(ax, fontsize=12, nodec=False):
200200
#############################################
201201
# However, this will steal space from a subplot layout:
202202

203-
fig, axs = plt.subplots(2, 2, constrained_layout=True)
204-
for ax in axs.flatten()[:-1]:
205-
ax.plot(np.arange(10))
206-
axs[1, 1].plot(np.arange(10), label='This is a plot')
207-
axs[1, 1].legend(loc='center left', bbox_to_anchor=(0.8, 0.5))
203+
fig, axs = plt.subplots(1, 2, figsize=(4, 2), constrained_layout=True)
204+
axs[0].plot(np.arange(10))
205+
axs[1].plot(np.arange(10), label='This is a plot')
206+
axs[1].legend(loc='center left', bbox_to_anchor=(0.8, 0.5))
208207

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

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

230234
#############################################
235+
# The saved file looks like:
236+
#
237+
# .. image:: /_static/constrained_layout/CL01.png
238+
# :align: center
239+
#
231240
# A better way to get around this awkwardness is to simply
232-
# use a legend for the figure:
233-
fig, axs = plt.subplots(2, 2, constrained_layout=True)
234-
for ax in axs.flatten()[:-1]:
235-
ax.plot(np.arange(10))
236-
lines = axs[1, 1].plot(np.arange(10), label='This is a plot')
241+
# use the legend method provided by `.Figure.legend`:
242+
fig, axs = plt.subplots(1, 2, figsize=(4, 2), constrained_layout=True)
243+
axs[0].plot(np.arange(10))
244+
lines = axs[1].plot(np.arange(10), label='This is a plot')
237245
labels = [l.get_label() for l in lines]
238246
leg = fig.legend(lines, labels, loc='center left',
239-
bbox_to_anchor=(0.8, 0.5), bbox_transform=axs[1, 1].transAxes)
247+
bbox_to_anchor=(0.8, 0.5), bbox_transform=axs[1].transAxes)
248+
fig.savefig('CL02.png', bbox_inches='tight', dpi=100)
249+
250+
#############################################
251+
# The saved file looks like:
252+
#
253+
# .. image:: /_static/constrained_layout/CL02.png
254+
# :align: center
255+
#
240256

241257
###############################################################################
242258
# Padding and Spacing

0 commit comments

Comments
 (0)