def test_minor_breaks(): n = 10 x = np.arange(n) # Default s = scale_xy.scale_x_continuous() s.train(x) breaks = s.get_breaks() minor_breaks = s.get_minor_breaks(breaks) expected_minor_breaks = (breaks[:-1] + breaks[1:])/2 assert np.allclose(minor_breaks, expected_minor_breaks, rtol=1e-12) # List expected_minor_breaks = [2, 4, 6, 8] s = scale_xy.scale_x_continuous(minor_breaks=expected_minor_breaks) s.train(x) breaks = s.get_breaks() minor_breaks = s.get_minor_breaks(breaks) assert np.allclose(minor_breaks, expected_minor_breaks, rtol=1e-12) # Callable def func(limits): return np.linspace(limits[0], limits[1], n) s = scale_xy.scale_x_continuous(minor_breaks=func) s.train(x) breaks = s.get_breaks() minor_breaks = s.get_minor_breaks(breaks) _breaks = set(breaks) expected_minor_breaks = [x for x in np.arange(n) if x not in _breaks] assert np.allclose(minor_breaks, expected_minor_breaks, rtol=1e-12) assert not (_breaks & set(minor_breaks))
def test_scale_continous_breaks(): x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] breaks = [2, 4, 6, 8, 10] # Array breaks should not trip up the conditional checks s1 = scale_x_continuous(breaks=breaks, limits=(1, 10)) s2 = scale_x_continuous(breaks=np.array(breaks), limits=(1, 10)) s1.train(x) s2.train(x) assert list(s1.get_breaks()) == list(s2.get_breaks())
def test_changing_scale_transform(): # No warning with pytest.warns(None): scale_x_continuous(trans='reverse') scale_xy.scale_x_reverse(trans='reverse') scale_xy.scale_x_log10(trans='log10') # Warnings with pytest.warns(PlotnineWarning): scale_xy.scale_x_reverse(trans='log10') with pytest.warns(PlotnineWarning): scale_xy.scale_x_datetime(trans='identity') s = scale_xy.scale_x_reverse() with pytest.warns(PlotnineWarning): s.trans = 'log10'
def test_breaks_and_labels_outside_of_limits(): df = pd.DataFrame({'x': range(5, 11), 'y': range(5, 11)}) p = (ggplot(aes('x', 'y'), data=df) + geom_point() + scale_x_continuous(limits=[7, 9.5], breaks=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], labels=[ 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven' ])) assert p == 'breaks_and_labels_outside_of_limits'
def test_setting_limits(): lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] s = scale_x_continuous() s.train(lst) assert s.limits == (1, 10) s = scale_x_continuous(limits=(3, 7)) s.train(lst) assert s.limits == (3, 7) s = scale_x_continuous(limits=(3, None)) s.train(lst) assert s.limits == (3, 10) s = scale_x_continuous(limits=(None, 7)) s.train(lst) assert s.limits == (1, 7) s = scale_color.scale_color_hue(limits=tuple('abcdefg')) s.train(['a', 'b', 'a']) assert s.limits == tuple('abcdefg')
def test_xy_palette(): s = scale_x_discrete() value = s.palette(3) assert (value == 3) s = scale_y_discrete() value = s.palette(11.5) assert (value == 11.5) s = scale_x_continuous() value = s.palette(3.63) assert (value == 3.63) s = scale_y_continuous() value = s.palette(11.52) assert (value == 11.52)