def axis_limits(covm, mean, pair, covm2, mean2, pair2, scale_factor):
    """ Choose axis limits on the basis of two covariance matrices and a scale factor """
    lim = dict()
    lim2 = dict()
    if pair.__class__ == int:
        pair = [pair, pair]
        pair2 = [pair2[0], pair2[0]]
    lim['minvalx'] = mean[pair[1] - 1] - scale_factor * covm[pair[1] - 1][
        pair[1] - 1]
    lim['maxvalx'] = mean[pair[1] - 1] + scale_factor * covm[pair[1] - 1][
        pair[1] - 1]
    lim['minvaly'] = mean[pair[0] - 1] - scale_factor * covm[pair[0] - 1][
        pair[0] - 1]
    lim['maxvaly'] = mean[pair[0] - 1] + scale_factor * covm[pair[0] - 1][
        pair[0] - 1]
    if pair2[0] != None:
        lim2['minvalx'] = mean2[pair2[1] - 1] - scale_factor * covm2[pair2[
            1] - 1][pair2[1] - 1]
        lim2['maxvalx'] = mean2[pair2[1] - 1] + scale_factor * covm2[pair2[
            1] - 1][pair2[1] - 1]
        lim2['minvaly'] = mean2[pair2[0] - 1] - scale_factor * covm2[pair2[
            0] - 1][pair2[0] - 1]
        lim2['maxvaly'] = mean2[pair2[0] - 1] + scale_factor * covm2[pair2[
            0] - 1][pair2[0] - 1]
        lim['minvalx'] = min([lim['minvalx'], lim2['minvalx']])
        lim['maxvalx'] = max([lim['maxvalx'], lim2['maxvalx']])
        lim['minvaly'] = min([lim['minvaly'], lim2['minvaly']])
        lim['maxvaly'] = max([lim['maxvaly'], lim2['maxvaly']])
    return lim
