Source code for irfpy.util.plttools

""" In house matplotlib tools.
"""
import numpy as _np
import matplotlib.pyplot as _plt

def _subplot_coord(n, x0, x1, dx):
    """ Return the coordinate (0 to 1).

    :param n: number of axes
    :param x0: left (or bottom)
    :param x1: right (or top)
    :param dx: wspace (or hspace)
    :keyword fig: A Figure instance. If *None*, default values are used.
    :returns: a tuple with two arrays
        ``([x0_for_ax1, x0_for_ax2, ...], [x1_for_ax1, x1_for_ax2, ...])``
    """
    xi = _np.arange(n) * (1 + dx) / (n + dx * (n - 1)) * (x1 - x0) + x0
    xi2 = (1 + _np.arange(n) * (1 + dx)) / (n + dx * (n - 1)) * (x1 - x0) + x0

    return xi, xi2
    
[docs]def subplot_coords(nraw, ncol, iaxis, fig=None): """ Return the coordinate of the subplot. :param nraw: The number of raw :param ncol: The number of column :param iaxis: The index of the axis. From 1 to raw * column. :keyword fig: A Figure instance. If *None*, default values are used. :returns: A tuple representing *(x0, y0, x1, y1)* is returned. The coordinate of axis, (x0, y0, x1, y1), produced by ``fig.add_subplot(nraw, ncol, iaxis)`` is returned. >>> x0, y0, x1, y1 = subplot_coords(2, 2, 3) >>> print('{:.3f} {:.3f} {:.3f} {:.3f}'.format(x0, y0, x1, y1)) # doctest: +SKIP 0.125 0.100 0.477 0.464 This is the coordinates for an axis produced by ``fig.add_subplot(2, 2, 3)``. """ if iaxis <= 0 or iaxis > nraw * ncol: raise IndexError('iaxis should be between 1 to {}'.format(nraw * ncol)) c = _all_subplot_coords(nraw, ncol, fig=fig) return c[iaxis - 1]
[docs]def subplot_rect(nraw, ncol, iaxis, fig=None): """ Return the rectangle (x, y, w, h). Similar to :func:`subplot_coords`, but returning (x, y, w, h). """ c = subplot_coords(nraw, ncol, iaxis, fig=fig) return (c[0], c[1], c[2] - c[0], c[3] - c[1])
def _all_subplot_coords(nraw, ncol, fig=None): """ Return the subplots coordinate systems. .. note:: Use :func:`subplot_coords` as user interface This function returns the coordinate system of the subplots. :param nraw: The number of raw :param ncol: The number of column :keyword fig: A Figure instance. If *None*, default values are used. :type fig: matplotlib.Figure :returns: A list with (raw * column) elements. The order is from top-left to top-right, followed by second-top-left to right, and downward. (It is the same as ``fig.add_subplot(nraw, ncol, iax + 1)`` where ``iax`` is the index representing the axis. (1 is added because the subplot is 1-based indexing). Each element is a tuple with four elements, ``(x0, y0, x1, y1)``. Example: We want to produce 2 raw times 3 column figure. >>> coords = _all_subplot_coords(2, 3) There are six axes, thus, the length is 6: >>> print(len(coords)) 6 The first axis is for the top-left one: ``subplot(2, 3, 1)``. The index should be zero. >>> x0, y0, x1, y1 = coords[0] """ if fig is None: l = _plt.rcParams['figure.subplot.left'] r = _plt.rcParams['figure.subplot.right'] w = _plt.rcParams['figure.subplot.wspace'] b = _plt.rcParams['figure.subplot.bottom'] t = _plt.rcParams['figure.subplot.top'] h = _plt.rcParams['figure.subplot.hspace'] else: l = fig.subplotpars.left r = fig.subplotpars.right w = fig.subplotpars.wspace b = fig.subplotpars.bottom t = fig.subplotpars.top h = fig.subplotpars.hspace x0, x1 = _subplot_coord(ncol, l, r, w) y0, y1 = _subplot_coord(nraw, b, t, h) rect = [] for iraw in range(nraw): y0raw = y0[-iraw-1] y1raw = y1[-iraw-1] for icol in range(ncol): rect.append((x0[icol], y0raw, x1[icol], y1raw)) return rect