コード例 #1
0
        def train(scale, limits, trans, name):
            """
            Train a single coordinate axis
            """
            if limits is None:
                rangee = scale.dimension()
            else:
                rangee = scale.transform(limits)

            # data space
            out = scale.break_info(rangee)

            # trans'd range
            out['range'] = trans.transform(out['range'])

            if limits is None:
                expand = self.expand_default(scale)
                out['range'] = expand_range(out['range'], expand[0], expand[1])

            # major and minor breaks in plot space
            out['major'] = transform_value(trans, out['major'], out['range'])
            out['minor'] = transform_value(trans, out['minor'], out['range'])

            for key in list(out.keys()):
                new_key = '{}_{}'.format(name, key)
                out[new_key] = out.pop(key)

            return out
コード例 #2
0
        def train(scale, limits, name):
            """
            Train a single coordinate axis
            """
            # Which axis are we dealing with
            name = scale.aesthetics[0]

            if self.expand:
                expand = self.expand_default(scale)
            else:
                expand = (0, 0)

            if limits is None:
                rangee = scale.dimension(expand)
            else:
                rangee = scale.transform(limits)
                rangee = expand_range(rangee, expand[0], expand[1])

            out = scale.break_info(rangee)
            # This is where
            # x_major, x_labels, x_minor, ...
            # range keys are created
            for key in list(out.keys()):
                new_key = '{}_{}'.format(name, key)
                out[new_key] = out.pop(key)
            return out
コード例 #3
0
def test_expand_range():
    assert expand_range((0, 1)) == (0, 1)
    assert expand_range((0, 1), mul=2) == (-2, 3)
    assert expand_range((0, 1), add=2) == (-2, 3)
    assert expand_range((0, 1), mul=2, add=2) == (-4, 5)
    assert expand_range((1, 1), mul=2, add=2, zero_width=1) == (0.5, 1.5)
    assert expand_range(0) == (-0.5, 0.5)

    def diff(x):
        return x[1] - x[0]

    # datetime
    one_day = datetime(2010, 1, 2) - datetime(2010, 1, 1)
    limits = datetime(2010, 1, 1), datetime(2010, 1, 2)
    result = expand_range(limits, add=one_day)
    diff(result) == diff(limits) + 2 * one_day

    # PY2 does not support *, / operations of timedeltas
    if not six.PY2:
        limits = datetime(2010, 1, 1), datetime(2010, 1, 1)  # zero range
        result = expand_range(limits, zero_width=30 * one_day)
        diff(result) == diff(limits) + 30 * one_day

    # pd.Timestamp
    one_day = pd.Timestamp('2010-01-02') - pd.Timestamp('2010-01-01')
    limits = pd.Timestamp('2010-01-01'), pd.Timestamp('2010-01-02')
    result = expand_range(limits, add=one_day)
    diff(result) == diff(limits) + 2 * one_day

    if not six.PY2:
        result = expand_range(limits, mul=0.5, add=one_day)
        diff(result) == 2 * diff(limits) + 2 * one_day

        limits = pd.Timestamp('2010-01-01'), pd.Timestamp('2010-01-01')
        result = expand_range(limits, zero_width=30 * one_day)
        diff(result) == diff(limits) + 30 * one_day

    # np.datetime64
    one_day = np.datetime64(1, 'D') - np.datetime64(0, 'D')
    limits = np.datetime64(14610, 'D'), np.datetime64(14611, 'D')
    result = expand_range(limits, add=one_day)
    diff(result) == diff(limits) + 2 * one_day

    if not six.PY2:
        result = expand_range(limits, mul=0.5, add=one_day)
        diff(result) == 2 * diff(limits) + 2 * one_day

    limits = np.datetime64(14610, 'D'), np.datetime64(14611, 'D')
    result = expand_range(limits, zero_width=30 * one_day)
    diff(result) == diff(limits) + 30 * one_day

    # timedelta
    one_day = timedelta(days=1)
    limits = timedelta(days=1), timedelta(days=10)
    result = expand_range(limits, add=one_day, zero_width=30 * one_day)
    diff(result) == diff(limits) + 2 * one_day

    if not six.PY2:
        result = expand_range(limits, mul=0.5, add=one_day)
        diff(result) == 2 * diff(limits) + 2 * one_day

        limits = timedelta(days=10), timedelta(days=10)
        result = expand_range(limits, add=one_day, zero_width=30 * one_day)
        diff(result) == diff(limits) + 30 * one_day

    # pd.Timedelta
    one_day = pd.Timedelta(days=1)
    limits = pd.Timedelta(days=1), pd.Timedelta(days=10)
    result = expand_range(limits, add=one_day, zero_width=30 * one_day)
    diff(result) == diff(limits) + 2 * one_day

    if not six.PY2:
        result = expand_range(limits, mul=0.5, add=one_day)
        diff(result) == 2 * diff(limits) + 2 * one_day

    limits = pd.Timedelta(days=10), pd.Timedelta(days=10)
    result = expand_range(limits, add=one_day, zero_width=30 * one_day)
    diff(result) == diff(limits) + 30 * one_day

    # timedelta64
    one_day = np.timedelta64(1, unit='D')
    limits = np.timedelta64(1, 'D'), np.timedelta64(10, 'D')
    result = expand_range(limits, add=one_day, zero_width=30 * one_day)
    diff(result) == diff(limits) + 2 * one_day

    if not six.PY2:
        result = expand_range(limits, mul=0.5, add=one_day)
        diff(result) == 2 * diff(limits) + 2 * one_day

    limits = np.timedelta64(1, 'D'), np.timedelta64(1, 'D')
    result = expand_range(limits, add=one_day, zero_width=30 * one_day)
    diff(result) == diff(limits) + 30 * one_day
コード例 #4
0
ファイル: test_bounds.py プロジェクト: has2k1/mizani
def test_expand_range():
    assert expand_range((0, 1)) == (0, 1)
    assert expand_range((0, 1), mul=2) == (-2, 3)
    assert expand_range((0, 1), add=2) == (-2, 3)
    assert expand_range((0, 1), mul=2, add=2) == (-4, 5)
    assert expand_range((1, 1), mul=2, add=2, zero_width=1) == (0.5, 1.5)
    assert expand_range(0) == (-0.5, 0.5)

    def diff(x):
        return x[1] - x[0]

    # datetime
    one_day = datetime(2010, 1, 2) - datetime(2010, 1, 1)
    limits = datetime(2010, 1, 1), datetime(2010, 1, 2)
    result = expand_range(limits, add=one_day)
    diff(result) == diff(limits) + 2*one_day

    limits = datetime(2010, 1, 1), datetime(2010, 1, 1)  # zero range
    result = expand_range(limits, zero_width=30*one_day)
    diff(result) == diff(limits) + 30*one_day

    # pd.Timestamp
    one_day = pd.Timestamp('2010-01-02') - pd.Timestamp('2010-01-01')
    limits = pd.Timestamp('2010-01-01'), pd.Timestamp('2010-01-02')
    result = expand_range(limits, add=one_day)
    diff(result) == diff(limits) + 2*one_day

    result = expand_range(limits, mul=0.5, add=one_day)
    diff(result) == 2*diff(limits) + 2*one_day

    limits = pd.Timestamp('2010-01-01'), pd.Timestamp('2010-01-01')
    result = expand_range(limits, zero_width=30*one_day)
    diff(result) == diff(limits) + 30*one_day

    # np.datetime64
    one_day = np.datetime64(1, 'D') - np.datetime64(0, 'D')
    limits = np.datetime64(14610, 'D'), np.datetime64(14611, 'D')
    result = expand_range(limits, add=one_day)
    diff(result) == diff(limits) + 2*one_day

    result = expand_range(limits, mul=0.5, add=one_day)
    diff(result) == 2*diff(limits) + 2*one_day

    limits = np.datetime64(14610, 'D'), np.datetime64(14611, 'D')
    result = expand_range(limits, zero_width=30*one_day)
    diff(result) == diff(limits) + 30*one_day

    # timedelta
    one_day = timedelta(days=1)
    limits = timedelta(days=1), timedelta(days=10)
    result = expand_range(limits, add=one_day, zero_width=30*one_day)
    diff(result) == diff(limits) + 2*one_day

    result = expand_range(limits, mul=0.5, add=one_day)
    diff(result) == 2*diff(limits) + 2*one_day

    limits = timedelta(days=10), timedelta(days=10)
    result = expand_range(limits, add=one_day, zero_width=30*one_day)
    diff(result) == diff(limits) + 30*one_day

    # pd.Timedelta
    one_day = pd.Timedelta(days=1)
    limits = pd.Timedelta(days=1), pd.Timedelta(days=10)
    result = expand_range(limits, add=one_day, zero_width=30*one_day)
    diff(result) == diff(limits) + 2*one_day

    result = expand_range(limits, mul=0.5, add=one_day)
    diff(result) == 2*diff(limits) + 2*one_day

    limits = pd.Timedelta(days=10), pd.Timedelta(days=10)
    result = expand_range(limits, add=one_day, zero_width=30*one_day)
    diff(result) == diff(limits) + 30*one_day

    # timedelta64
    one_day = np.timedelta64(1, unit='D')
    limits = np.timedelta64(1, 'D'), np.timedelta64(10, 'D')
    result = expand_range(limits, add=one_day, zero_width=30*one_day)
    diff(result) == diff(limits) + 2*one_day

    result = expand_range(limits, mul=0.5, add=one_day)
    diff(result) == 2*diff(limits) + 2*one_day

    limits = np.timedelta64(1, 'D'), np.timedelta64(1, 'D')
    result = expand_range(limits, add=one_day, zero_width=30*one_day)
    diff(result) == diff(limits) + 30*one_day
コード例 #5
0
 def dimension(self, expand=(0, 0)):
     """
     The phyical size of the scale, if a position scale
     Unlike limits, this always returns a numeric vector of length 2
     """
     return expand_range(self.limits, expand[0], expand[1])