def set_limits(output, plot_limits=[None, None], legend_limits=[None, None]):
    """Set plot and legend limits according to inputs.
    If limits are input and valid, return them. Otherwise, set them to the according
    limit in the data. If limits are input and out of order, reverse them.

    Args:
        output (np array, nx1): output dataset being plotted
        plot_limits (float, 2x1 list, optional): [min, max] limits of values that will be
                    displayed in plot
        legend_limits (float, 2x1 list, optional): [min, max] limits of colorbar scale

    Returns:
        plot_limits (float, 2x1 list): [min, max] limits of plotted values
        legend_limits (float, 2x1 list): [min, max] limits of colorbar scale
    """
    if all(plot_limits) and plot_limits[0] > plot_limits[1]:
        plot_limits.reverse()
    if all(legend_limits) and legend_limits[0] > legend_limits[1]:
        legend_limits.reverse()

    if plot_limits[0] is None:
        plot_limits[0] = min(output)
    if plot_limits[1] is None:
        plot_limits[1] = max(output)
    if legend_limits[0] is None:
        legend_limits[0] = min(output)
    if legend_limits[1] is None:
        legend_limits[1] = max(output)

    return plot_limits, legend_limits