def test_label_base_from_transform(self, x): s = Continuous(trans="log") a = PseudoAxis(s._setup(x, Coordinate())._matplotlib_scale) a.set_view_interval(10, 1000) label, = a.major.formatter.format_ticks([100]) assert r"10^{2}" in label
def test_tick_upto(self, x): for n in [2, 5, 10]: s = Continuous().tick(upto=n).setup(x, Coordinate()) a = PseudoAxis(s.matplotlib_scale) a.set_view_interval(0, 1) assert len(a.major.locator()) <= (n + 1)
def setup_labels(self, x, *args, **kwargs): s = Continuous().label(*args, **kwargs)._setup(x, Coordinate()) a = PseudoAxis(s._matplotlib_scale) a.set_view_interval(0, 1) locs = a.major.locator() return a, locs
def test_coordinate_numeric_data(self, y): ax = mpl.figure.Figure().subplots() s = Nominal()._setup(y, Coordinate(), ax.yaxis) assert_array_equal(s(y), np.array([1, 0, 2, 0], float)) f = ax.yaxis.get_major_formatter() assert f.format_ticks([0, 1, 2]) == ["-1.5", "1.0", "3.0"]
def test_tick_locator(self, t): locator = mpl.dates.YearLocator(month=3, day=15) s = Temporal().tick(locator) a = PseudoAxis(s._setup(t, Coordinate())._matplotlib_scale) a.set_view_interval(0, 365) assert 73 in a.major.locator()
def test_log_tick_default(self, x): s = Continuous(trans="log")._setup(x, Coordinate()) a = PseudoAxis(s._matplotlib_scale) a.set_view_interval(.5, 1050) ticks = a.major.locator() assert np.allclose(np.diff(np.log10(ticks)), 1)
def test_tick_count(self, x): n = 8 s = Continuous().tick(count=n).setup(x, Coordinate()) a = PseudoAxis(s.matplotlib_scale) a.set_view_interval(0, 1) assert_array_equal(a.major.locator(), np.linspace(0, 1, n))
def test_tick_at(self, x): locs = [.2, .5, .9] s = Continuous().tick(at=locs).setup(x, Coordinate()) a = PseudoAxis(s.matplotlib_scale) a.set_view_interval(0, 1) assert_array_equal(a.major.locator(), locs)
def test_tick_upto(self, t, x): n = 8 ax = mpl.figure.Figure().subplots() Temporal().tick(upto=n)._setup(t, Coordinate(), ax.xaxis) locator = ax.xaxis.get_major_locator() assert set(locator.maxticks.values()) == {n}
def test_tick_every(self, x): for d in [.05, .2, .5]: s = Continuous().tick(every=d).setup(x, Coordinate()) a = PseudoAxis(s.matplotlib_scale) a.set_view_interval(0, 1) assert np.allclose(np.diff(a.major.locator()), d)
def test_coordinate_axis(self, x): ax = mpl.figure.Figure().subplots() s = Nominal()._setup(x, Coordinate(), ax.xaxis) assert_array_equal(s(x), np.array([0, 1, 2, 1], float)) f = ax.xaxis.get_major_formatter() assert f.format_ticks([0, 1, 2]) == ["a", "c", "b"]
def test_tick_count_between(self, x): n = 5 lo, hi = .2, .7 s = Continuous().tick(count=n, between=(lo, hi)).setup(x, Coordinate()) a = PseudoAxis(s.matplotlib_scale) a.set_view_interval(0, 1) assert_array_equal(a.major.locator(), np.linspace(lo, hi, n))
def test_coordinate_axis_with_subset_order(self, x): order = ["c", "a"] ax = mpl.figure.Figure().subplots() s = Nominal(order=order)._setup(x, Coordinate(), ax.xaxis) assert_array_equal(s(x), np.array([1, 0, np.nan, 0], float)) f = ax.xaxis.get_major_formatter() assert f.format_ticks([0, 1, 2]) == [*order, ""]
def test_tick_locator(self, x): locs = [.2, .5, .8] locator = mpl.ticker.FixedLocator(locs) s = Continuous().tick(locator).setup(x, Coordinate()) a = PseudoAxis(s.matplotlib_scale) a.set_view_interval(0, 1) assert_array_equal(a.major.locator(), locs)
def test_label_formatter(self, t): formatter = mpl.dates.DateFormatter("%Y") s = Temporal().label(formatter) a = PseudoAxis(s._setup(t, Coordinate())._matplotlib_scale) a.set_view_interval(10, 1000) label, = a.major.formatter.format_ticks([100]) assert label == "1970"
def test_coordinate_numeric_data_with_order(self, y): order = [1, 4, -1.5] ax = mpl.figure.Figure().subplots() s = Nominal(order=order)._setup(y, Coordinate(), ax.yaxis) assert_array_equal(s(y), np.array([0, 2, np.nan, 2], float)) f = ax.yaxis.get_major_formatter() assert f.format_ticks([0, 1, 2]) == ["1.0", "4.0", "-1.5"]
def test_tick_every_between(self, x): lo, hi = .2, .8 for d in [.05, .2, .5]: s = Continuous().tick(every=d, between=(lo, hi)).setup(x, Coordinate()) a = PseudoAxis(s.matplotlib_scale) a.set_view_interval(0, 1) expected = np.arange(lo, hi + d, d) assert_array_equal(a.major.locator(), expected)
def test_coordinate_axis(self, t, x): ax = mpl.figure.Figure().subplots() s = Temporal()._setup(t, Coordinate(), ax.xaxis) assert_array_equal(s(t), x) locator = ax.xaxis.get_major_locator() formatter = ax.xaxis.get_major_formatter() assert isinstance(locator, mpl.dates.AutoDateLocator) assert isinstance(formatter, mpl.dates.AutoDateFormatter)
def test_log_tick_count(self, x): with pytest.raises(RuntimeError, match="`count` requires"): Continuous(trans="log").tick(count=4) s = Continuous(trans="log").tick(count=4, between=(1, 1000)) a = PseudoAxis(s._setup(x, Coordinate())._matplotlib_scale) a.set_view_interval(.5, 1050) assert_array_equal(a.major.locator(), [1, 10, 100, 1000])
def test_coordinate_axis_with_category_dtype(self, x): order = ["b", "a", "d", "c"] x = x.astype(pd.CategoricalDtype(order)) ax = mpl.figure.Figure().subplots() s = Nominal()._setup(x, Coordinate(), ax.xaxis) assert_array_equal(s(x), np.array([1, 3, 0, 3], float)) f = ax.xaxis.get_major_formatter() assert f.format_ticks([0, 1, 2, 3]) == order
def test_tick_minor(self, x): n = 3 s = Continuous().tick(count=2, minor=n).setup(x, Coordinate()) a = PseudoAxis(s.matplotlib_scale) a.set_view_interval(0, 1) # I am not sure why matplotlib's minor ticks include the # largest major location but exclude the smalllest one ... expected = np.linspace(0, 1, n + 2)[1:] assert_array_equal(a.minor.locator(), expected)
def test_symlog_tick_default(self, x): s = Continuous(trans="symlog")._setup(x, Coordinate()) a = PseudoAxis(s._matplotlib_scale) a.set_view_interval(-1050, 1050) ticks = a.major.locator() assert ticks[0] == -ticks[-1] pos_ticks = np.sort(np.unique(np.abs(ticks))) assert np.allclose(np.diff(np.log10(pos_ticks[1:])), 1) assert pos_ticks[0] == 0
def test_coordinate_transform_error(self, x): s = Continuous(trans="bad") with pytest.raises(ValueError, match="Unknown value provided"): s._setup(x, Coordinate())
def test_label_concise(self, t, x): ax = mpl.figure.Figure().subplots() Temporal().label(concise=True)._setup(t, Coordinate(), ax.xaxis) formatter = ax.xaxis.get_major_formatter() assert isinstance(formatter, mpl.dates.ConciseDateFormatter)
def test_coordinate_transform_with_parameter(self, x): s = Continuous(trans="pow3")._setup(x, Coordinate()) assert_series_equal(s(x), np.power(x, 3))
def test_coordinate_defaults(self, t, x): s = Temporal()._setup(t, Coordinate()) assert_array_equal(s(t), x)
def test_coordinate_transform(self, x): s = Continuous(trans="log")._setup(x, Coordinate()) assert_series_equal(s(x), np.log10(x))
def test_coordinate_defaults(self, x): s = Continuous()._setup(x, Coordinate()) assert_series_equal(s(x), x)
def test_coordinate_with_subset_order(self, x): s = Nominal(order=["c", "a"])._setup(x, Coordinate()) assert_array_equal(s(x), np.array([1, 0, np.nan, 0], float))
def setup_ticks(self, x, *args, **kwargs): s = Continuous().tick(*args, **kwargs)._setup(x, Coordinate()) a = PseudoAxis(s._matplotlib_scale) a.set_view_interval(0, 1) return a