def set_axlims(ax, lims, axname):
    """
    Set the data limits for the axis ax.

    Args:
        lims: tuple(2) for (left, right), tuple(1) or scalar for left only.
        axname: "x" for x-axis, "y" for y-axis.

    Return: (left, right)
    """
    left, right = None, None
    if lims is None:
        return left, right
    len_lims = None
    try:
        len_lims = len(lims)
    except TypeError:
        left = float(lims)
    if len_lims is not None:
        if len(lims) == 2:
            left, right = lims[0], lims[1]
        elif len(lims) == 1:
            left = lims[0]
    set_lim = getattr(ax, {'x': 'set_xlim', 'y': 'set_ylim'}[axname])
    if left != right:
        set_lim(left, right)
    return left, right
