22
22
23
23
24
24
def example_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 )
26
26
if not hide_labels :
27
27
ax .set_xlabel ('x-label' , fontsize = fontsize )
28
28
ax .set_ylabel ('y-label' , fontsize = fontsize )
29
29
ax .set_title ('Title' , fontsize = fontsize )
30
30
return pc
31
31
32
-
33
32
# gridspec inside gridspec
34
33
fig = plt .figure (constrained_layout = True , figsize = (10 , 4 ))
35
34
subfigs = fig .subfigures (1 , 2 , wspace = 0.07 )
@@ -43,7 +42,7 @@ def example_plot(ax, fontsize=12, hide_labels=False):
43
42
44
43
axsRight = subfigs [1 ].subplots (3 , 1 , sharex = True )
45
44
for nn , ax in enumerate (axsRight ):
46
- # pc = example_plot(ax, hide_labels=True)
45
+ pc = example_plot (ax , hide_labels = True )
47
46
if nn == 2 :
48
47
ax .set_xlabel ('xlabel' )
49
48
if nn == 1 :
@@ -56,3 +55,93 @@ def example_plot(ax, fontsize=12, hide_labels=False):
56
55
fig .suptitle ('Figure suptitle' , fontsize = 'xx-large' )
57
56
58
57
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
+ for a in axs [:, 0 ]:
68
+ gridspec = a .get_subplotspec ().get_gridspec ()
69
+ a .remove ()
70
+
71
+ # plot data in remaining axes:
72
+ for a in axs [:, 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
+ for ax in axsLeft :
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
+ for ax in axsLeft :
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
+ for nn , ax in enumerate (axsRight ):
104
+ pc = example_plot (ax , hide_labels = True )
105
+ if nn == 2 :
106
+ ax .set_xlabel ('xlabel' )
107
+ if nn == 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
+ for nn , ax in enumerate (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 ()
0 commit comments