Exemplo n.º 1
0
def set_new_axlim(axs: plt.Axes,
                  data: np.ndarray,
                  err: _ty.Optional[np.ndarray] = None,
                  *,
                  yaxis: bool = True,
                  reset: bool = False,
                  buffer: float = 0.05) -> None:
    """Set axes limits that will show all data, including existing.

    Parameters
    ----------
    axs : mpl.axes.Axes
        `Axes` instance whose limits are to be adjusted.
    data : np.ndarray (n)
        Array of numbers plotted along the axis.
    err : None|np.ndarray (n,)|(2,n), optional
        Error bars for data, by default: `None`.
    yaxis : bool, optional
        Are we modifying the y axis? By default: `True`.
    reset : bool, optional
        Do we ignore the existing axis limits? By default: `False`.
    buffer : float, optional
        Fractional padding around data, by default `0.05`.
    """
    log = (axs.get_yscale() if yaxis else axs.get_xscale()) == 'log'
    lim = calc_axlim(data, err, log, buffer)
    if not reset:
        if yaxis:
            axlim = axs.get_ylim()
        else:
            axlim = axs.get_xlim()
        lim = min(lim[0], axlim[0]), max(lim[1], axlim[1])
    if yaxis:
        axs.set_ylim(lim)
    else:
        axs.set_xlim(lim)