Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitfaae77e

Browse files
authored
Merge pull request#12412 from meeseeksmachine/auto-backport-of-pr-12394-on-v3.0.x
Backport PR#12394 on branch v3.0.x (DOC: fix CL tutorial to give same output from saved file and example)
2 parents7042ab5 +0edb99f commitfaae77e

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
@@ -205,11 +205,10 @@ def example_plot(ax, fontsize=12, nodec=False):
205205
#############################################
206206
# However, this will steal space from a subplot layout:
207207

208-
fig,axs=plt.subplots(2,2,constrained_layout=True)
209-
foraxinaxs.flatten()[:-1]:
210-
ax.plot(np.arange(10))
211-
axs[1,1].plot(np.arange(10),label='This is a plot')
212-
axs[1,1].legend(loc='center left',bbox_to_anchor=(0.8,0.5))
208+
fig,axs=plt.subplots(1,2,figsize=(4,2),constrained_layout=True)
209+
axs[0].plot(np.arange(10))
210+
axs[1].plot(np.arange(10),label='This is a plot')
211+
axs[1].legend(loc='center left',bbox_to_anchor=(0.8,0.5))
213212

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

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

235239
#############################################
240+
# The saved file looks like:
241+
#
242+
# .. image:: /_static/constrained_layout/CL01.png
243+
# :align: center
244+
#
236245
# A better way to get around this awkwardness is to simply
237-
# use a legend for the figure:
238-
fig,axs=plt.subplots(2,2,constrained_layout=True)
239-
foraxinaxs.flatten()[:-1]:
240-
ax.plot(np.arange(10))
241-
lines=axs[1,1].plot(np.arange(10),label='This is a plot')
246+
# use the legend method provided by `.Figure.legend`:
247+
fig,axs=plt.subplots(1,2,figsize=(4,2),constrained_layout=True)
248+
axs[0].plot(np.arange(10))
249+
lines=axs[1].plot(np.arange(10),label='This is a plot')
242250
labels= [l.get_label()forlinlines]
243251
leg=fig.legend(lines,labels,loc='center left',
244-
bbox_to_anchor=(0.8,0.5),bbox_transform=axs[1,1].transAxes)
252+
bbox_to_anchor=(0.8,0.5),bbox_transform=axs[1].transAxes)
253+
fig.savefig('CL02.png',bbox_inches='tight',dpi=100)
254+
255+
#############################################
256+
# The saved file looks like:
257+
#
258+
# .. image:: /_static/constrained_layout/CL02.png
259+
# :align: center
260+
#
245261

246262
###############################################################################
247263
# Padding and Spacing

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp