Skip to content

Commit 78426fa

Browse files
authored
Merge pull request #16744 from anntzer/axes_grid-tut
Reword axes_divider tutorial.
2 parents e283ff6 + 9f15e85 commit 78426fa

File tree

1 file changed

+41
-52
lines changed

1 file changed

+41
-52
lines changed

tutorials/toolkits/axes_grid.py

Lines changed: 41 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -328,74 +328,67 @@
328328
units that are used to determine the size of each axes. For example,
329329
you can specify a fixed size.
330330
331-
* :class:`~mpl_toolkits.axes_grid1.axes_divider.Divider` is the class that
331+
* `~mpl_toolkits.axes_grid1.axes_divider.Divider` is the class that
332332
calculates the axes position. It divides the given rectangular area into
333333
several areas. The divider is initialized by setting the lists of horizontal
334334
and vertical sizes on which the division will be based. Then use
335335
:meth:`~mpl_toolkits.axes_grid1.axes_divider.Divider.new_locator`, which
336336
returns a callable object that can be used to set the axes_locator of the
337337
axes.
338338
339+
Here, we demonstrate how to achieve the following layout: we want to position
340+
axes in a 3x4 grid (note that `.Divider` makes row indices start from the
341+
*bottom*\(!) of the grid):
339342
340-
First, initialize the divider by specifying its grids, i.e.,
341-
horizontal and vertical::
343+
.. code-block:: none
342344
343-
rect = [0.2, 0.2, 0.6, 0.6]
344-
horiz = [h0, h1, h2, h3]
345-
vert = [v0, v1, v2]
346-
divider = Divider(fig, rect, horiz, vert)
345+
+--------+--------+--------+--------+
346+
| (2, 0) | (2, 1) | (2, 2) | (2, 3) |
347+
+--------+--------+--------+--------+
348+
| (1, 0) | (1, 1) | (1, 2) | (1, 3) |
349+
+--------+--------+--------+--------+
350+
| (0, 0) | (0, 1) | (0, 2) | (0, 3) |
351+
+--------+--------+--------+--------+
352+
353+
such that the bottom row has a fixed height of 2 (inches) and the top two rows
354+
have a height ratio of 2 (middle) to 3 (top). (For example, if the grid has
355+
a size of 7 inches, the bottom row will be 2 inches, the middle row also 2
356+
inches, and the top row 3 inches.)
347357
348-
where rect is a bounds of the box that will be divided and h0, ..., h3,
349-
v0, ..., v2 need to be instance of classes in the
350-
:mod:`~mpl_toolkits.axes_grid1.axes_size`. They have *get_size* method
351-
that returns a tuple of two floats. The first float is the relative
352-
size, and the second float is the absolute size. Consider a following
353-
grid.
358+
These constraints are specified using classes from the
359+
:mod:`~mpl_toolkits.axes_grid1.axes_size` module, namely::
354360
355-
+------+-----+-----+-----+
356-
| v0 | | | |
357-
+------+-----+-----+-----+
358-
| v1 | | | |
359-
+------+-----+-----+-----+
360-
|h0, v2| h1 | h2 | h3 |
361-
+------+-----+-----+-----+
361+
from mpl_toolkits.axes_grid1.axes_size import Fixed, Scaled
362+
vert = [Fixed(2), Scaled(2), Scaled(3)]
362363
364+
(More generally, :mod:`~mpl_toolkits.axes_grid1.axes_size` classes define a
365+
``get_size(renderer)`` method that returns a pair of floats -- a relative size,
366+
and an absolute size. ``Fixed(2).get_size(renderer)`` returns ``(0, 2)``;
367+
``Scaled(2).get_size(renderer)`` returns ``(2, 0)``.)
363368
364-
* v0 => 0, 2
365-
* v1 => 2, 0
366-
* v2 => 3, 0
369+
We use these constraints to initialize a `.Divider` object::
367370
368-
The height of the bottom row is always 2 (axes_divider internally
369-
assumes that the unit is inches). The first and the second rows have a
370-
height ratio of 2:3. For example, if the total height of the grid is 6,
371-
then the first and second row will each occupy 2/(2+3) and 3/(2+3) of
372-
(6-1) inches. The widths of the horizontal columns will be similarly
373-
determined. When the aspect ratio is set, the total height (or width) will
374-
be adjusted accordingly.
371+
rect = [0.2, 0.2, 0.6, 0.6] # Position of the grid in the figure.
372+
vert = [Fixed(2), Scaled(2), Scaled(3)] # As above.
373+
horiz = [...] # Some other horizontal constraints.
374+
divider = Divider(fig, rect, horiz, vert)
375375
376-
The :mod:`mpl_toolkits.axes_grid1.axes_size` contains several classes
377-
that can be used to set the horizontal and vertical configurations. For
378-
example, for vertical configuration one could use::
376+
then use `.Divider.new_locator` to create an `.AxesLocator` instance for a
377+
given grid entry::
379378
380-
from mpl_toolkits.axes_grid1.axes_size import Fixed, Scaled
381-
vert = [Fixed(2), Scaled(2), Scaled(3)]
379+
locator = divider.new_locator(nx=0, ny=1) # Grid entry (1, 0).
382380
383-
After you set up the divider object, then you create a locator
384-
instance that will be given to the axes object.::
381+
and make it responsible for locating the axes::
385382
386-
locator = divider.new_locator(nx=0, ny=1)
387-
ax.set_axes_locator(locator)
383+
ax.set_axes_locator(locator)
388384
389-
The return value of the new_locator method is an instance of the
390-
AxesLocator class. It is a callable object that returns the
391-
location and size of the cell at the first column and the second row.
392-
You may create a locator that spans over multiple cells.::
385+
The `.AxesLocator` is a callable object that returns the location and size of
386+
the cell at the first column and the second row.
393387
394-
locator = divider.new_locator(nx=0, nx=2, ny=1)
388+
Locators that spans over multiple cells can be created with, e.g.::
395389
396-
The above locator, when called, will return the position and size of
397-
the cells spanning the first and second column and the first row. In
398-
this example, it will return [0:2, 1].
390+
# Columns #0 and #1 ("0-2 range"), row #1.
391+
locator = divider.new_locator(nx=0, nx1=2, ny=1)
399392
400393
See the example,
401394
@@ -404,15 +397,11 @@
404397
:align: center
405398
:scale: 50
406399
407-
Simple Axes Divider2
408-
409-
You can adjust the size of each axes according to its x or y
400+
You can also adjust the size of each axes according to its x or y
410401
data limits (AxesX and AxesY).
411402
412403
.. figure:: ../../gallery/axes_grid1/images/sphx_glr_simple_axes_divider3_001.png
413404
:target: ../../gallery/axes_grid1/simple_axes_divider3.html
414405
:align: center
415406
:scale: 50
416-
417-
Simple Axes Divider3
418407
"""

0 commit comments

Comments
 (0)