@@ -649,6 +649,80 @@ def subplot(*args, **kwargs):
649649 return a
650650
651651
652+ def fig_subplot (nrows = 1 , ncols = 1 , sharex = False , sharey = False ,
653+ subplot_kw = None , ** fig_kw ):
654+ """Create a figure with a set of subplots already made.
655+
656+ This utility wrapper makes it convenient to create common layouts of
657+ subplots, including the enclosing figure object, in a single call.
658+
659+ Keyword arguments:
660+
661+ nrows : int
662+ Number of rows of the subplot grid. Defaults to 1.
663+
664+ nrows : int
665+ Number of columns of the subplot grid. Defaults to 1.
666+
667+ sharex : bool
668+ If True, the X axis will be shared amongst all subplots.
669+
670+ sharex : bool
671+ If True, the Y axis will be shared amongst all subplots.
672+
673+ subplot_kw : dict
674+ Dict with keywords passed to the add_subplot() call used to create each
675+ subplots.
676+
677+ fig_kw : dict
678+ Dict with keywords passed to the figure() call. Note that all keywords
679+ not recognized above will be automatically included here.
680+
681+ Returns:
682+
683+ fig_axes : list
684+ A list containing [fig, ax1, ax2, ...], where fig is the Matplotlib
685+ Figure object and the rest are the axes.
686+
687+ **Examples:**
688+
689+ x = np.linspace(0, 2*np.pi, 400)
690+ y = np.sin(x**2)
691+
692+ # Just a figure and one subplot
693+ f, ax = plt.fig_subplot()
694+ ax.plot(x, y)
695+ ax.set_title('Simple plot')
696+
697+ # Two subplots, unpack the output immediately
698+ f, ax1, ax2 = plt.fig_subplot(1, 2, sharey=True)
699+ ax1.plot(x, y)
700+ ax1.set_title('Sharing Y axis')
701+ ax2.scatter(x, y)
702+
703+ # Four polar axes
704+ plt.fig_subplot(2, 2, subplot_kw=dict(polar=True))
705+ """
706+
707+ if subplot_kw is None :
708+ subplot_kw = {}
709+
710+ fig = figure (** fig_kw )
711+
712+ # Create first subplot separately, so we can share it if requested
713+ ax1 = fig .add_subplot (nrows , ncols , 1 , ** subplot_kw )
714+ if sharex :
715+ subplot_kw ['sharex' ] = ax1
716+ if sharey :
717+ subplot_kw ['sharey' ] = ax1
718+
719+ # Valid indices for axes start at 1, since fig is at 0:
720+ axes = [ fig .add_subplot (nrows , ncols , i , ** subplot_kw )
721+ for i in range (2 , nrows * ncols + 1 )]
722+
723+ return [fig , ax1 ] + axes
724+
725+
652726def twinx (ax = None ):
653727 """
654728 Make a second axes overlay *ax* (or the current axes if *ax* is
0 commit comments