def subplot_index(nrow, ncol, k, kmin=1):
    """Return the i, j index for the k-th subplot."""
    i = 1 + (k - kmin) // ncol
    j = 1 + (k - kmin) % ncol
    if i > nrow:
        raise ValueError('k = %d exceeds number of rows' % k)
    return i, j