diff options
author | Picca Frédéric-Emmanuel <picca@debian.org> | 2018-03-04 10:20:27 +0100 |
---|---|---|
committer | Picca Frédéric-Emmanuel <picca@debian.org> | 2018-03-04 10:20:27 +0100 |
commit | 270d5ddc31c26b62379e3caa9044dd75ccc71847 (patch) | |
tree | 55c5bfc851dfce7172d335cd2405b214323e3caf /silx/gui/plot/_utils | |
parent | e19c96eff0c310c06c4f268c8b80cb33bd08996f (diff) |
New upstream version 0.7.0+dfsg
Diffstat (limited to 'silx/gui/plot/_utils')
-rw-r--r-- | silx/gui/plot/_utils/test/test_ticklayout.py | 18 | ||||
-rw-r--r-- | silx/gui/plot/_utils/ticklayout.py | 20 |
2 files changed, 29 insertions, 9 deletions
diff --git a/silx/gui/plot/_utils/test/test_ticklayout.py b/silx/gui/plot/_utils/test/test_ticklayout.py index 8c67620..927ffb6 100644 --- a/silx/gui/plot/_utils/test/test_ticklayout.py +++ b/silx/gui/plot/_utils/test/test_ticklayout.py @@ -27,12 +27,13 @@ from __future__ import absolute_import, division, unicode_literals __authors__ = ["T. Vincent"] __license__ = "MIT" -__date__ = "18/10/2016" +__date__ = "17/01/2018" import unittest +import numpy -from silx.test.utils import ParametricTestCase +from silx.utils.testutils import ParametricTestCase from silx.gui.plot._utils import ticklayout @@ -40,6 +41,19 @@ from silx.gui.plot._utils import ticklayout class TestTickLayout(ParametricTestCase): """Test ticks layout algorithms""" + def testTicks(self): + """Test of :func:`ticks`""" + tests = { # (vmin, vmax): ref_ticks + (1., 1.): (1.,), + (0.5, 10.5): (2.0, 4.0, 6.0, 8.0, 10.0), + (0.001, 0.005): (0.001, 0.002, 0.003, 0.004, 0.005) + } + + for (vmin, vmax), ref_ticks in tests.items(): + with self.subTest(vmin=vmin, vmax=vmax): + ticks, labels = ticklayout.ticks(vmin, vmax) + self.assertTrue(numpy.allclose(ticks, ref_ticks)) + def testNiceNumbers(self): """Minimalistic tests of :func:`niceNumbers`""" tests = { # (vmin, vmax): ref_ticks diff --git a/silx/gui/plot/_utils/ticklayout.py b/silx/gui/plot/_utils/ticklayout.py index 5f4b636..6e9f654 100644 --- a/silx/gui/plot/_utils/ticklayout.py +++ b/silx/gui/plot/_utils/ticklayout.py @@ -109,7 +109,7 @@ def ticks(vMin, vMax, nbTicks=5): """Returns tick positions and labels using nice numbers algorithm. This enforces ticks to be within [vMin, vMax] range. - It returns at least 2 ticks. + It returns at least 1 tick (when vMin == vMax). :param float vMin: The min value on the axis :param float vMax: The max value on the axis @@ -117,13 +117,19 @@ def ticks(vMin, vMax, nbTicks=5): :returns: tick positions and corresponding text labels :rtype: 2-tuple: list of float, list of string """ - start, end, step, nfrac = niceNumbers(vMin, vMax, nbTicks) - positions = [t for t in _frange(start, end, step) if vMin <= t <= vMax] + assert vMin <= vMax + if vMin == vMax: + positions = [vMin] + nfrac = 0 + + else: + start, end, step, nfrac = niceNumbers(vMin, vMax, nbTicks) + positions = [t for t in _frange(start, end, step) if vMin <= t <= vMax] - # Makes sure there is at least 2 ticks - if len(positions) < 2: - positions = [vMin, vMax] - nfrac = numberOfDigits(vMax - vMin) + # Makes sure there is at least 2 ticks + if len(positions) < 2: + positions = [vMin, vMax] + nfrac = numberOfDigits(vMax - vMin) # Generate labels format_ = '%g' if nfrac == 0 else '%.{}f'.format(nfrac) |