Skip to content

Commit

Permalink
added tests for ticklabel updated after tick update
Browse files Browse the repository at this point in the history
  • Loading branch information
CDonnerer committed Dec 31, 2021
1 parent 2be2d9a commit cc036f4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/shellplot/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def _auto_nticks(self):
def _auto_ticks(self):
"""Automatically find good axis ticks"""
if self.limits is None:
return None
raise ValueError("Please fit axis or set limits first!")
elif not self._is_datetime:
return self._auto_numeric_ticks()
else:
Expand All @@ -210,7 +210,7 @@ def _auto_numeric_ticks(self, tol=0.05):
)
return np.around(
np.arange(self.limits[0], self.limits[1] + step, step), precision
)
)[: self.nticks]

def _auto_datetime_ticks(self):
axis_td = to_datetime(np.array(self.limits, dtype="timedelta64[ns]"))
Expand All @@ -223,7 +223,7 @@ def _auto_datetime_ticks(self):
np.datetime64(axis_td[0], unit),
np.datetime64(axis_td[1], unit) + td_step,
td_step,
)
)[: self.nticks]

def _auto_ticklabels(self):
if self._is_datetime:
Expand Down
41 changes: 31 additions & 10 deletions tests/test_axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,19 @@ def test_axis_display_ticks(limits, ticks, expected_tick_labels):
)
def test_axis_ticklabels_len_error(ticks, labels):
"""Test error raising when tick labels do not match ticks"""
axis = Axis(display_length=80)
axis = Axis()
axis.ticks = ticks

with pytest.raises(ValueError):
axis.ticklabels = labels


def test_axis_ticks_before_fit_error():
axis = Axis()
with pytest.raises(ValueError):
axis.ticks


# -----------------------------------------------------------------------------
# Test axis property setting
# -----------------------------------------------------------------------------
Expand Down Expand Up @@ -175,8 +181,7 @@ def test_axis_property_setting(axis_property, value, expected_value):
assert set_value == expected_value


def test_axis_limit_update_changes_ticks():
"""Check that updating limits leads to new axis ticks"""
def test_axis_limit_set_updates_ticks():
axis = Axis(display_length=80)

axis.limits = (0, 300)
Expand All @@ -188,10 +193,26 @@ def test_axis_limit_update_changes_ticks():
np.testing.assert_array_equal(axis.ticks, np.array([50, 55, 60, 65, 70, 75, 80]))


def test_axis_properties():
"""Faux test for property setting"""
axis = Axis(display_length=80)
axis.title = "title"
axis.limits = (1, 9)
axis.ticks = np.array([1, 3, 5, 7, 9])
axis.labels = np.array(["a", "b", "c", "d", "e"])
def test_axis_nticks_set_updates_ticks():
axis = Axis()
axis.limits = (0, 1)

axis.nticks = 2
np.testing.assert_array_equal(axis.ticks, np.array([0.0, 1.0]))

axis.nticks = 3
np.testing.assert_array_equal(axis.ticks, np.array([0.0, 0.5, 1.0]))

axis.nticks = 5
np.testing.assert_array_equal(axis.ticks, np.array([0.0, 0.25, 0.5, 0.75, 1.0]))


def test_axis_ticks_set_updates_ticklabels():
axis = Axis()
axis.limits = (0, 1)

axis.ticks = (0.0, 0.3, 1.0)
np.testing.assert_array_equal(axis.ticklabels, np.array([0.0, 0.3, 1.0]))

axis.ticks = (0.0, 0.2)
np.testing.assert_array_equal(axis.ticklabels, np.array([0.0, 0.2]))

0 comments on commit cc036f4

Please sign in to comment.