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

Skip to content

Commit 4f398d3

Browse files
committed
update docs. for gridspec
svn path=/trunk/matplotlib/; revision=8321
1 parent 550ba7c commit 4f398d3

6 files changed

Lines changed: 253 additions & 0 deletions

File tree

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
2010-05-18 Merge mpl_toolkits.gridspec into the main tree. - JJL
2+
13
2010-05-04 Improve backend_qt4 so it displays figures with the
24
correct size - DSD
35

doc/users/gridspec.rst

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
.. _gridspec-guide:
2+
3+
4+
************************************************
5+
Customizing Location of Subplot Using GridSpec
6+
************************************************
7+
8+
``GridSpec``
9+
specifies the geometry of the grid that a subplot will be
10+
placed. The number of rows and number of columns of the grid
11+
need to be set. Optionally, the subplot layout parameters
12+
(e.g., left, right, etc.) can be tuned.
13+
14+
``SubplotSpec``
15+
specifies the location of the subplot in the given *GridSpec*.
16+
17+
``subplot2grid``
18+
a helper function that is similar to "pyplot.subplot" but uses
19+
0-based indexing and let subplot to occupy multiple cells.
20+
21+
22+
Basic Example of using subplot2grid
23+
=====================================
24+
25+
To use subplot2grid, you provide geometry of the grid and the location
26+
of the subplot in the grid. For a simple single-cell subplot, ::
27+
28+
ax = plt.subplot2grid((2,2),(0, 0))
29+
30+
is identical to ::
31+
32+
ax = plt.subplot(2,2,1)
33+
34+
Note that, unlike matplotlib's subplot, the index starts from 0 in gridspec.
35+
36+
To create a subplot that spans multiple cells, ::
37+
38+
ax2 = plt.subplot2grid((3,3), (1, 0), colspan=2)
39+
ax3 = plt.subplot2grid((3,3), (1, 2), rowspan=2)
40+
41+
For example, the following commands ::
42+
43+
ax1 = plt.subplot2grid((3,3), (0,0), colspan=3)
44+
ax2 = plt.subplot2grid((3,3), (1,0), colspan=2)
45+
ax3 = plt.subplot2grid((3,3), (1, 2), rowspan=2)
46+
ax4 = plt.subplot2grid((3,3), (2, 0))
47+
ax5 = plt.subplot2grid((3,3), (2, 1))
48+
49+
creates
50+
51+
.. plot:: users/plotting/examples/demo_gridspec01.py
52+
53+
54+
GridSpec and SubplotSpec
55+
========================
56+
57+
You can create GridSpec explicitly and use them to create a Subplot.
58+
59+
For example, ::
60+
61+
ax = plt.subplot2grid((2,2),(0, 0))
62+
63+
is equal to ::
64+
65+
import matplotlib.gridspec as gridspec
66+
gs = gridspec.GridSpec(2, 2)
67+
ax = plt.subplot(gs[0, 0])
68+
69+
A gridspec instance provides array-like (2d or 1d) indexing that
70+
returns the SubplotSpec instance. For, SubplotSpec that spans multiple
71+
cells, use slice. ::
72+
73+
ax2 = plt.subplot(gs[1,:-1])
74+
ax3 = plt.subplot(gs[1:, -1])
75+
76+
The above example becomes ::
77+
78+
gs = gridspec.GridSpec(3, 3)
79+
ax1 = plt.subplot(gs[0, :])
80+
ax2 = plt.subplot(gs[1,:-1])
81+
ax3 = plt.subplot(gs[1:, -1])
82+
ax4 = plt.subplot(gs[-1,0])
83+
ax5 = plt.subplot(gs[-1,-2])
84+
85+
.. plot:: users/plotting/examples/demo_gridspec02.py
86+
87+
Adjust GridSpec layout
88+
======================
89+
90+
When a GridSpec is explicitly used, you can adjust the layout
91+
parameters of subplots that are created from the gridspec. ::
92+
93+
gs1 = gridspec.GridSpec(3, 3)
94+
gs1.update(left=0.05, right=0.48, wspace=0.05)
95+
96+
This is similar to *subplots_adjust*, but it only affects the subplots
97+
that are created from the given GridSpec.
98+
99+
The code below ::
100+
101+
gs1 = gridspec.GridSpec(3, 3)
102+
gs1.update(left=0.05, right=0.48, wspace=0.05)
103+
ax1 = plt.subplot(gs1[:-1, :])
104+
ax2 = plt.subplot(gs1[-1, :-1])
105+
ax3 = plt.subplot(gs1[-1, -1])
106+
107+
gs2 = gridspec.GridSpec(3, 3)
108+
gs2.update(left=0.55, right=0.98, hspace=0.05)
109+
ax4 = plt.subplot(gs2[:, :-1])
110+
ax5 = plt.subplot(gs2[:-1, -1])
111+
ax6 = plt.subplot(gs2[-1, -1])
112+
113+
creates
114+
115+
.. plot:: users/plotting/examples/demo_gridspec03.py
116+
117+
GridSpec using SubplotSpec
118+
==========================
119+
120+
You can create GridSpec from the SubplotSpec, in which case its layout
121+
parameters are set to that of the locataion of the given SubplotSpec. ::
122+
123+
gs0 = gridspec.GridSpec(1, 2)
124+
125+
gs00 = gridspec.GridSpecFromSubplotSpec(3, 3, subplot_spec=gs0[0])
126+
gs01 = gridspec.GridSpecFromSubplotSpec(3, 3, subplot_spec=gs0[1])
127+
128+
129+
.. plot:: users/plotting/examples/demo_gridspec04.py
130+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import matplotlib.pyplot as plt
2+
3+
def make_ticklabels_invisible(fig):
4+
for i, ax in enumerate(fig.axes):
5+
ax.text(0.5, 0.5, "ax%d" % (i+1), va="center", ha="center")
6+
for tl in ax.get_xticklabels() + ax.get_yticklabels():
7+
tl.set_visible(False)
8+
9+
10+
plt.figure(0)
11+
ax1 = plt.subplot2grid((3,3), (0,0), colspan=3)
12+
ax2 = plt.subplot2grid((3,3), (1,0), colspan=2)
13+
ax3 = plt.subplot2grid((3,3), (1, 2), rowspan=2)
14+
ax4 = plt.subplot2grid((3,3), (2, 0))
15+
ax5 = plt.subplot2grid((3,3), (2, 1))
16+
17+
plt.suptitle("subplot2grid")
18+
make_ticklabels_invisible(plt.gcf())
19+
plt.show()
20+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import matplotlib.pyplot as plt
2+
from matplotlib.gridspec import GridSpec
3+
4+
5+
def make_ticklabels_invisible(fig):
6+
for i, ax in enumerate(fig.axes):
7+
ax.text(0.5, 0.5, "ax%d" % (i+1), va="center", ha="center")
8+
for tl in ax.get_xticklabels() + ax.get_yticklabels():
9+
tl.set_visible(False)
10+
11+
12+
plt.figure()
13+
14+
gs = GridSpec(3, 3)
15+
ax1 = plt.subplot(gs[0, :])
16+
# identical to ax1 = plt.subplot(gs.new_subplotspec((0,0), colspan=3))
17+
ax2 = plt.subplot(gs[1,:-1])
18+
ax3 = plt.subplot(gs[1:, -1])
19+
ax4 = plt.subplot(gs[-1,0])
20+
ax5 = plt.subplot(gs[-1,-2])
21+
22+
plt.suptitle("GridSpec")
23+
make_ticklabels_invisible(plt.gcf())
24+
25+
plt.show()
26+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import matplotlib.pyplot as plt
2+
from matplotlib.gridspec import GridSpec
3+
4+
5+
def make_ticklabels_invisible(fig):
6+
for i, ax in enumerate(fig.axes):
7+
ax.text(0.5, 0.5, "ax%d" % (i+1), va="center", ha="center")
8+
for tl in ax.get_xticklabels() + ax.get_yticklabels():
9+
tl.set_visible(False)
10+
11+
12+
13+
# demo 3 : gridspec with subplotpars set.
14+
15+
f = plt.figure()
16+
17+
plt.suptitle("GirdSpec w/ different subplotpars")
18+
19+
gs1 = GridSpec(3, 3)
20+
gs1.update(left=0.05, right=0.48, wspace=0.05)
21+
ax1 = plt.subplot(gs1[:-1, :])
22+
ax2 = plt.subplot(gs1[-1, :-1])
23+
ax3 = plt.subplot(gs1[-1, -1])
24+
25+
gs2 = GridSpec(3, 3)
26+
gs2.update(left=0.55, right=0.98, hspace=0.05)
27+
ax4 = plt.subplot(gs2[:, :-1])
28+
ax5 = plt.subplot(gs2[:-1, -1])
29+
ax6 = plt.subplot(gs2[-1, -1])
30+
31+
make_ticklabels_invisible(plt.gcf())
32+
33+
plt.show()
34+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import matplotlib.pyplot as plt
2+
import matplotlib.gridspec as gridspec
3+
4+
def make_ticklabels_invisible(fig):
5+
for i, ax in enumerate(fig.axes):
6+
ax.text(0.5, 0.5, "ax%d" % (i+1), va="center", ha="center")
7+
for tl in ax.get_xticklabels() + ax.get_yticklabels():
8+
tl.set_visible(False)
9+
10+
11+
12+
# gridspec inside gridspec
13+
14+
f = plt.figure()
15+
16+
gs0 = gridspec.GridSpec(1, 2)
17+
18+
gs00 = gridspec.GridSpecFromSubplotSpec(3, 3, subplot_spec=gs0[0])
19+
20+
ax1 = plt.Subplot(f, gs00[:-1, :])
21+
f.add_subplot(ax1)
22+
ax2 = plt.Subplot(f, gs00[-1, :-1])
23+
f.add_subplot(ax2)
24+
ax3 = plt.Subplot(f, gs00[-1, -1])
25+
f.add_subplot(ax3)
26+
27+
28+
gs01 = gridspec.GridSpecFromSubplotSpec(3, 3, subplot_spec=gs0[1])
29+
30+
ax4 = plt.Subplot(f, gs01[:, :-1])
31+
f.add_subplot(ax4)
32+
ax5 = plt.Subplot(f, gs01[:-1, -1])
33+
f.add_subplot(ax5)
34+
ax6 = plt.Subplot(f, gs01[-1, -1])
35+
f.add_subplot(ax6)
36+
37+
plt.suptitle("GirdSpec Inside GridSpec")
38+
make_ticklabels_invisible(plt.gcf())
39+
40+
plt.show()
41+

0 commit comments

Comments
 (0)