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

Skip to content

Commit 9c60f86

Browse files
committed
Add pyplot.fig_subplot, for easier creation of figures with multiple subplots.
svn path=/trunk/matplotlib/; revision=8151
1 parent 2a9a215 commit 9c60f86

3 files changed

Lines changed: 120 additions & 0 deletions

File tree

CHANGELOG

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
2010-02-24 Added pyplot.fig_subplot(), to create a figure and a group of
2+
subplots in a single call. This offers an easier pattern than
3+
manually making figures and calling add_subplot() multiple times. FP
4+
15
2010-02-17 Added Gokhan's and Mattias' customizable keybindings patch
26
for the toolbar. You can now set the keymap.* properties
37
in the matplotlibrc file. Newbindings were added for
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""
2+
"""
3+
import matplotlib.pyplot as plt
4+
import numpy as np
5+
6+
x = np.linspace(0, 2*np.pi, 400)
7+
y = np.sin(x**2)
8+
9+
plt.close('all')
10+
11+
# Just a figure and one subplot
12+
f, ax = plt.fig_subplot()
13+
ax.plot(x, y)
14+
ax.set_title('Simple plot')
15+
16+
# Two subplots, grab the whole fig_axes list
17+
fax = plt.fig_subplot(2, sharex=True)
18+
fax[1].plot(x, y)
19+
fax[1].set_title('Sharing X axis')
20+
fax[2].scatter(x, y)
21+
22+
# Two subplots, unpack the output immediately
23+
f, ax1, ax2 = plt.fig_subplot(1, 2, sharey=True)
24+
ax1.plot(x, y)
25+
ax1.set_title('Sharing Y axis')
26+
ax2.scatter(x, y)
27+
28+
# Three subplots sharing both x/y axes
29+
f, ax1, ax2, ax3 = plt.fig_subplot(3, sharex=True, sharey=True)
30+
ax1.plot(x, y)
31+
ax1.set_title('Sharing both axes')
32+
ax2.scatter(x, y)
33+
ax3.scatter(x, 2*y**2-1,color='r')
34+
# Fine-tune figure; make subplots close to each other and hide x ticks for
35+
# all but bottom plot.
36+
f.subplots_adjust(hspace=0)
37+
plt.setp([a.get_xticklabels() for a in f.axes[:-1]], visible=False)
38+
39+
# Four polar axes
40+
plt.fig_subplot(2, 2, subplot_kw=dict(polar=True))
41+
42+
plt.show()

lib/matplotlib/pyplot.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
652726
def twinx(ax=None):
653727
"""
654728
Make a second axes overlay *ax* (or the current axes if *ax* is

0 commit comments

Comments
 (0)