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

Commitd1fa5cc

Browse files
committed
Expand example
1 parent32f7030 commitd1fa5cc

File tree

3 files changed

+99
-8
lines changed

3 files changed

+99
-8
lines changed

‎examples/subplots_axes_and_figures/subfigures.py

Lines changed: 92 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,13 @@
2222

2323

2424
defexample_plot(ax,fontsize=12,hide_labels=False):
25-
pc=ax.pcolormesh(np.random.randn(30,30))
25+
pc=ax.pcolormesh(np.random.randn(30,30),vmin=-2.5,vmax=2.5)
2626
ifnothide_labels:
2727
ax.set_xlabel('x-label',fontsize=fontsize)
2828
ax.set_ylabel('y-label',fontsize=fontsize)
2929
ax.set_title('Title',fontsize=fontsize)
3030
returnpc
3131

32-
3332
# gridspec inside gridspec
3433
fig=plt.figure(constrained_layout=True,figsize=(10,4))
3534
subfigs=fig.subfigures(1,2,wspace=0.07)
@@ -43,7 +42,7 @@ def example_plot(ax, fontsize=12, hide_labels=False):
4342

4443
axsRight=subfigs[1].subplots(3,1,sharex=True)
4544
fornn,axinenumerate(axsRight):
46-
#pc = example_plot(ax, hide_labels=True)
45+
pc=example_plot(ax,hide_labels=True)
4746
ifnn==2:
4847
ax.set_xlabel('xlabel')
4948
ifnn==1:
@@ -56,3 +55,93 @@ def example_plot(ax, fontsize=12, hide_labels=False):
5655
fig.suptitle('Figure suptitle',fontsize='xx-large')
5756

5857
plt.show()
58+
59+
##############################################################################
60+
# It is possible to mix subplots and subfigures using
61+
# `matplotlib.figure.Figure.add_subfigure`. This requires getting
62+
# the gridspec that the subplots are laid out on.
63+
64+
fig,axs=plt.subplots(2,3,constrained_layout=True,figsize=(10,4))
65+
66+
# clear the left column for the subfigure:
67+
forainaxs[:,0]:
68+
gridspec=a.get_subplotspec().get_gridspec()
69+
a.remove()
70+
71+
# plot data in remaining axes:
72+
forainaxs[:,1:].flat:
73+
a.plot(np.arange(10))
74+
75+
# make the subfigure in the empy gridspec slots:
76+
subfig=fig.add_subfigure(gridspec[:,0])
77+
78+
axsLeft=subfig.subplots(1,2,sharey=True)
79+
subfig.set_facecolor('0.75')
80+
foraxinaxsLeft:
81+
pc=example_plot(ax)
82+
subfig.suptitle('Left plots',fontsize='x-large')
83+
subfig.colorbar(pc,shrink=0.6,ax=axsLeft,location='bottom')
84+
85+
fig.suptitle('Figure suptitle',fontsize='xx-large')
86+
plt.show()
87+
88+
##############################################################################
89+
# Subfigures can have different widths and heights. This is exactly the
90+
# same example as the first example, but *width_ratios* has been changed:
91+
92+
fig=plt.figure(constrained_layout=True,figsize=(10,4))
93+
subfigs=fig.subfigures(1,2,wspace=0.07,width_ratios=[2,1])
94+
95+
axsLeft=subfigs[0].subplots(1,2,sharey=True)
96+
subfigs[0].set_facecolor('0.75')
97+
foraxinaxsLeft:
98+
pc=example_plot(ax)
99+
subfigs[0].suptitle('Left plots',fontsize='x-large')
100+
subfigs[0].colorbar(pc,shrink=0.6,ax=axsLeft,location='bottom')
101+
102+
axsRight=subfigs[1].subplots(3,1,sharex=True)
103+
fornn,axinenumerate(axsRight):
104+
pc=example_plot(ax,hide_labels=True)
105+
ifnn==2:
106+
ax.set_xlabel('xlabel')
107+
ifnn==1:
108+
ax.set_ylabel('ylabel')
109+
110+
subfigs[1].set_facecolor('0.85')
111+
subfigs[1].colorbar(pc,shrink=0.6,ax=axsRight)
112+
subfigs[1].suptitle('Right plots',fontsize='x-large')
113+
114+
fig.suptitle('Figure suptitle',fontsize='xx-large')
115+
116+
plt.show()
117+
118+
##############################################################################
119+
# Subfigures can be also be nested:
120+
121+
fig=plt.figure(constrained_layout=True,figsize=(10,8))
122+
123+
fig.suptitle('fig')
124+
125+
subfigs=fig.subfigures(1,2,wspace=0.07)
126+
127+
subfigs[0].set_facecolor('coral')
128+
subfigs[0].suptitle('subfigs[0]')
129+
130+
subfigs[1].set_facecolor('coral')
131+
subfigs[1].suptitle('subfigs[1]')
132+
133+
subfigsnest=subfigs[0].subfigures(2,1,height_ratios=[1,1.4])
134+
subfigsnest[0].suptitle('subfigsnest[0]')
135+
subfigsnest[0].set_facecolor('r')
136+
axsnest0=subfigsnest[0].subplots(1,2,sharey=True)
137+
fornn,axinenumerate(axsnest0):
138+
pc=example_plot(ax,hide_labels=True)
139+
subfigsnest[0].colorbar(pc,ax=axsnest0)
140+
141+
subfigsnest[1].suptitle('subfigsnest[1]')
142+
subfigsnest[1].set_facecolor('g')
143+
axsnest1=subfigsnest[1].subplots(3,1,sharex=True)
144+
145+
axsRight=subfigs[1].subplots(2,2)
146+
147+
plt.show()

‎lib/matplotlib/_constrained_layout.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ def do_constrained_layout(fig, renderer, h_pad, w_pad,
107107
# figure. Add margins for colorbars...
108108
_make_layout_margins(fig,renderer,h_pad=h_pad,w_pad=w_pad,
109109
hspace=hspace,wspace=wspace)
110-
111110
_make_margin_suptitles(fig,renderer,h_pad=h_pad,w_pad=w_pad)
112111

113112
# if a layout is such that a columns (or rows) margin has no
@@ -279,12 +278,15 @@ def _make_margin_suptitles(fig, renderer, *, w_pad=0, h_pad=0):
279278
# top level figure margin larger.
280279
forpanelinfig.panels:
281280
_make_margin_suptitles(panel,renderer,w_pad=w_pad,h_pad=h_pad)
282-
invTransFig=fig.transSubfigure.inverted().transform_bbox
283-
284-
w_pad,h_pad= (fig.transSubfigure-
285-
fig.transFigure).transform((w_pad,h_pad))
286281

287282
iffig._suptitleisnotNoneandfig._suptitle.get_in_layout():
283+
invTransFig=fig.transSubfigure.inverted().transform_bbox
284+
parenttrans=fig.transFigure
285+
w_pad,h_pad= (fig.transSubfigure-
286+
parenttrans).transform((w_pad,1-h_pad))
287+
w_pad,one= (fig.transSubfigure-
288+
parenttrans).transform((w_pad,1))
289+
h_pad=one-h_pad
288290
bbox=invTransFig(fig._suptitle.get_tightbbox(renderer))
289291
p=fig._suptitle.get_position()
290292
fig._suptitle.set_position((p[0],1-h_pad))

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp