test_ticker.py 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307
  1. try:
  2. from contextlib import nullcontext
  3. except ImportError:
  4. from contextlib import ExitStack as nullcontext # Py 3.6.
  5. import re
  6. import itertools
  7. import numpy as np
  8. from numpy.testing import assert_almost_equal, assert_array_equal
  9. import pytest
  10. import matplotlib
  11. import matplotlib.pyplot as plt
  12. import matplotlib.ticker as mticker
  13. class TestMaxNLocator:
  14. basic_data = [
  15. (20, 100, np.array([20., 40., 60., 80., 100.])),
  16. (0.001, 0.0001, np.array([0., 0.0002, 0.0004, 0.0006, 0.0008, 0.001])),
  17. (-1e15, 1e15, np.array([-1.0e+15, -5.0e+14, 0e+00, 5e+14, 1.0e+15])),
  18. (0, 0.85e-50, np.arange(6) * 2e-51),
  19. (-0.85e-50, 0, np.arange(-5, 1) * 2e-51),
  20. ]
  21. integer_data = [
  22. (-0.1, 1.1, None, np.array([-1, 0, 1, 2])),
  23. (-0.1, 0.95, None, np.array([-0.25, 0, 0.25, 0.5, 0.75, 1.0])),
  24. (1, 55, [1, 1.5, 5, 6, 10], np.array([0, 15, 30, 45, 60])),
  25. ]
  26. @pytest.mark.parametrize('vmin, vmax, expected', basic_data)
  27. def test_basic(self, vmin, vmax, expected):
  28. loc = mticker.MaxNLocator(nbins=5)
  29. assert_almost_equal(loc.tick_values(vmin, vmax), expected)
  30. @pytest.mark.parametrize('vmin, vmax, steps, expected', integer_data)
  31. def test_integer(self, vmin, vmax, steps, expected):
  32. loc = mticker.MaxNLocator(nbins=5, integer=True, steps=steps)
  33. assert_almost_equal(loc.tick_values(vmin, vmax), expected)
  34. class TestLinearLocator:
  35. def test_basic(self):
  36. loc = mticker.LinearLocator(numticks=3)
  37. test_value = np.array([-0.8, -0.3, 0.2])
  38. assert_almost_equal(loc.tick_values(-0.8, 0.2), test_value)
  39. def test_set_params(self):
  40. """
  41. Create linear locator with presets={}, numticks=2 and change it to
  42. something else. See if change was successful. Should not exception.
  43. """
  44. loc = mticker.LinearLocator(numticks=2)
  45. loc.set_params(numticks=8, presets={(0, 1): []})
  46. assert loc.numticks == 8
  47. assert loc.presets == {(0, 1): []}
  48. class TestMultipleLocator:
  49. def test_basic(self):
  50. loc = mticker.MultipleLocator(base=3.147)
  51. test_value = np.array([-9.441, -6.294, -3.147, 0., 3.147, 6.294,
  52. 9.441, 12.588])
  53. assert_almost_equal(loc.tick_values(-7, 10), test_value)
  54. def test_view_limits(self):
  55. """
  56. Test basic behavior of view limits.
  57. """
  58. with matplotlib.rc_context({'axes.autolimit_mode': 'data'}):
  59. loc = mticker.MultipleLocator(base=3.147)
  60. assert_almost_equal(loc.view_limits(-5, 5), (-5, 5))
  61. def test_view_limits_round_numbers(self):
  62. """
  63. Test that everything works properly with 'round_numbers' for auto
  64. limit.
  65. """
  66. with matplotlib.rc_context({'axes.autolimit_mode': 'round_numbers'}):
  67. loc = mticker.MultipleLocator(base=3.147)
  68. assert_almost_equal(loc.view_limits(-4, 4), (-6.294, 6.294))
  69. def test_set_params(self):
  70. """
  71. Create multiple locator with 0.7 base, and change it to something else.
  72. See if change was successful.
  73. """
  74. mult = mticker.MultipleLocator(base=0.7)
  75. mult.set_params(base=1.7)
  76. assert mult._edge.step == 1.7
  77. class TestAutoMinorLocator:
  78. def test_basic(self):
  79. fig, ax = plt.subplots()
  80. ax.set_xlim(0, 1.39)
  81. ax.minorticks_on()
  82. test_value = np.array([0.05, 0.1, 0.15, 0.25, 0.3, 0.35, 0.45,
  83. 0.5, 0.55, 0.65, 0.7, 0.75, 0.85, 0.9,
  84. 0.95, 1.05, 1.1, 1.15, 1.25, 1.3, 1.35])
  85. assert_almost_equal(ax.xaxis.get_ticklocs(minor=True), test_value)
  86. # NB: the following values are assuming that *xlim* is [0, 5]
  87. params = [
  88. (0, 0), # no major tick => no minor tick either
  89. (1, 0) # a single major tick => no minor tick
  90. ]
  91. @pytest.mark.parametrize('nb_majorticks, expected_nb_minorticks', params)
  92. def test_low_number_of_majorticks(
  93. self, nb_majorticks, expected_nb_minorticks):
  94. # This test is related to issue #8804
  95. fig, ax = plt.subplots()
  96. xlims = (0, 5) # easier to test the different code paths
  97. ax.set_xlim(*xlims)
  98. ax.set_xticks(np.linspace(xlims[0], xlims[1], nb_majorticks))
  99. ax.minorticks_on()
  100. ax.xaxis.set_minor_locator(mticker.AutoMinorLocator())
  101. assert len(ax.xaxis.get_minorticklocs()) == expected_nb_minorticks
  102. majorstep_minordivisions = [(1, 5),
  103. (2, 4),
  104. (2.5, 5),
  105. (5, 5),
  106. (10, 5)]
  107. # This test is meant to verify the parameterization for
  108. # test_number_of_minor_ticks
  109. def test_using_all_default_major_steps(self):
  110. with matplotlib.rc_context({'_internal.classic_mode': False}):
  111. majorsteps = [x[0] for x in self.majorstep_minordivisions]
  112. np.testing.assert_allclose(majorsteps,
  113. mticker.AutoLocator()._steps)
  114. @pytest.mark.parametrize('major_step, expected_nb_minordivisions',
  115. majorstep_minordivisions)
  116. def test_number_of_minor_ticks(
  117. self, major_step, expected_nb_minordivisions):
  118. fig, ax = plt.subplots()
  119. xlims = (0, major_step)
  120. ax.set_xlim(*xlims)
  121. ax.set_xticks(xlims)
  122. ax.minorticks_on()
  123. ax.xaxis.set_minor_locator(mticker.AutoMinorLocator())
  124. nb_minor_divisions = len(ax.xaxis.get_minorticklocs()) + 1
  125. assert nb_minor_divisions == expected_nb_minordivisions
  126. limits = [(0, 1.39), (0, 0.139),
  127. (0, 0.11e-19), (0, 0.112e-12),
  128. (-2.0e-07, -3.3e-08), (1.20e-06, 1.42e-06),
  129. (-1.34e-06, -1.44e-06), (-8.76e-07, -1.51e-06)]
  130. reference = [
  131. [0.05, 0.1, 0.15, 0.25, 0.3, 0.35, 0.45, 0.5, 0.55, 0.65, 0.7,
  132. 0.75, 0.85, 0.9, 0.95, 1.05, 1.1, 1.15, 1.25, 1.3, 1.35],
  133. [0.005, 0.01, 0.015, 0.025, 0.03, 0.035, 0.045, 0.05, 0.055, 0.065,
  134. 0.07, 0.075, 0.085, 0.09, 0.095, 0.105, 0.11, 0.115, 0.125, 0.13,
  135. 0.135],
  136. [5.00e-22, 1.00e-21, 1.50e-21, 2.50e-21, 3.00e-21, 3.50e-21, 4.50e-21,
  137. 5.00e-21, 5.50e-21, 6.50e-21, 7.00e-21, 7.50e-21, 8.50e-21, 9.00e-21,
  138. 9.50e-21, 1.05e-20, 1.10e-20],
  139. [5.00e-15, 1.00e-14, 1.50e-14, 2.50e-14, 3.00e-14, 3.50e-14, 4.50e-14,
  140. 5.00e-14, 5.50e-14, 6.50e-14, 7.00e-14, 7.50e-14, 8.50e-14, 9.00e-14,
  141. 9.50e-14, 1.05e-13, 1.10e-13],
  142. [-1.95e-07, -1.90e-07, -1.85e-07, -1.75e-07, -1.70e-07, -1.65e-07,
  143. -1.55e-07, -1.50e-07, -1.45e-07, -1.35e-07, -1.30e-07, -1.25e-07,
  144. -1.15e-07, -1.10e-07, -1.05e-07, -9.50e-08, -9.00e-08, -8.50e-08,
  145. -7.50e-08, -7.00e-08, -6.50e-08, -5.50e-08, -5.00e-08, -4.50e-08,
  146. -3.50e-08],
  147. [1.21e-06, 1.22e-06, 1.23e-06, 1.24e-06, 1.26e-06, 1.27e-06, 1.28e-06,
  148. 1.29e-06, 1.31e-06, 1.32e-06, 1.33e-06, 1.34e-06, 1.36e-06, 1.37e-06,
  149. 1.38e-06, 1.39e-06, 1.41e-06, 1.42e-06],
  150. [-1.435e-06, -1.430e-06, -1.425e-06, -1.415e-06, -1.410e-06,
  151. -1.405e-06, -1.395e-06, -1.390e-06, -1.385e-06, -1.375e-06,
  152. -1.370e-06, -1.365e-06, -1.355e-06, -1.350e-06, -1.345e-06],
  153. [-1.48e-06, -1.46e-06, -1.44e-06, -1.42e-06, -1.38e-06, -1.36e-06,
  154. -1.34e-06, -1.32e-06, -1.28e-06, -1.26e-06, -1.24e-06, -1.22e-06,
  155. -1.18e-06, -1.16e-06, -1.14e-06, -1.12e-06, -1.08e-06, -1.06e-06,
  156. -1.04e-06, -1.02e-06, -9.80e-07, -9.60e-07, -9.40e-07, -9.20e-07,
  157. -8.80e-07]]
  158. additional_data = list(zip(limits, reference))
  159. @pytest.mark.parametrize('lim, ref', additional_data)
  160. def test_additional(self, lim, ref):
  161. fig, ax = plt.subplots()
  162. ax.minorticks_on()
  163. ax.grid(True, 'minor', 'y', linewidth=1)
  164. ax.grid(True, 'major', color='k', linewidth=1)
  165. ax.set_ylim(lim)
  166. assert_almost_equal(ax.yaxis.get_ticklocs(minor=True), ref)
  167. class TestLogLocator:
  168. def test_basic(self):
  169. loc = mticker.LogLocator(numticks=5)
  170. with pytest.raises(ValueError):
  171. loc.tick_values(0, 1000)
  172. test_value = np.array([1.00000000e-05, 1.00000000e-03, 1.00000000e-01,
  173. 1.00000000e+01, 1.00000000e+03, 1.00000000e+05,
  174. 1.00000000e+07, 1.000000000e+09])
  175. assert_almost_equal(loc.tick_values(0.001, 1.1e5), test_value)
  176. loc = mticker.LogLocator(base=2)
  177. test_value = np.array([0.5, 1., 2., 4., 8., 16., 32., 64., 128., 256.])
  178. assert_almost_equal(loc.tick_values(1, 100), test_value)
  179. def test_switch_to_autolocator(self):
  180. loc = mticker.LogLocator(subs="all")
  181. assert_array_equal(loc.tick_values(0.45, 0.55),
  182. [0.44, 0.46, 0.48, 0.5, 0.52, 0.54, 0.56])
  183. # check that we *skip* 1.0, and 10, because this is a minor locator
  184. loc = mticker.LogLocator(subs=np.arange(2, 10))
  185. assert 1.0 not in loc.tick_values(0.9, 20.)
  186. assert 10.0 not in loc.tick_values(0.9, 20.)
  187. def test_set_params(self):
  188. """
  189. Create log locator with default value, base=10.0, subs=[1.0],
  190. numdecs=4, numticks=15 and change it to something else.
  191. See if change was successful. Should not raise exception.
  192. """
  193. loc = mticker.LogLocator()
  194. loc.set_params(numticks=7, numdecs=8, subs=[2.0], base=4)
  195. assert loc.numticks == 7
  196. assert loc.numdecs == 8
  197. assert loc._base == 4
  198. assert list(loc._subs) == [2.0]
  199. class TestNullLocator:
  200. def test_set_params(self):
  201. """
  202. Create null locator, and attempt to call set_params() on it.
  203. Should not exception, and should raise a warning.
  204. """
  205. loc = mticker.NullLocator()
  206. with pytest.warns(UserWarning):
  207. loc.set_params()
  208. class _LogitHelper:
  209. @staticmethod
  210. def isclose(x, y):
  211. return (np.isclose(-np.log(1/x-1), -np.log(1/y-1))
  212. if 0 < x < 1 and 0 < y < 1 else False)
  213. @staticmethod
  214. def assert_almost_equal(x, y):
  215. ax = np.array(x)
  216. ay = np.array(y)
  217. assert np.all(ax > 0) and np.all(ax < 1)
  218. assert np.all(ay > 0) and np.all(ay < 1)
  219. lx = -np.log(1/ax-1)
  220. ly = -np.log(1/ay-1)
  221. assert_almost_equal(lx, ly)
  222. class TestLogitLocator:
  223. ref_basic_limits = [
  224. (5e-2, 1 - 5e-2),
  225. (5e-3, 1 - 5e-3),
  226. (5e-4, 1 - 5e-4),
  227. (5e-5, 1 - 5e-5),
  228. (5e-6, 1 - 5e-6),
  229. (5e-7, 1 - 5e-7),
  230. (5e-8, 1 - 5e-8),
  231. (5e-9, 1 - 5e-9),
  232. ]
  233. ref_basic_major_ticks = [
  234. 1 / (10 ** np.arange(1, 3)),
  235. 1 / (10 ** np.arange(1, 4)),
  236. 1 / (10 ** np.arange(1, 5)),
  237. 1 / (10 ** np.arange(1, 6)),
  238. 1 / (10 ** np.arange(1, 7)),
  239. 1 / (10 ** np.arange(1, 8)),
  240. 1 / (10 ** np.arange(1, 9)),
  241. 1 / (10 ** np.arange(1, 10)),
  242. ]
  243. ref_maxn_limits = [(0.4, 0.6), (5e-2, 2e-1), (1 - 2e-1, 1 - 5e-2)]
  244. @pytest.mark.parametrize(
  245. "lims, expected_low_ticks",
  246. zip(ref_basic_limits, ref_basic_major_ticks),
  247. )
  248. def test_basic_major(self, lims, expected_low_ticks):
  249. """
  250. Create logit locator with huge number of major, and tests ticks.
  251. """
  252. expected_ticks = sorted(
  253. [*expected_low_ticks, 0.5, *(1 - expected_low_ticks)]
  254. )
  255. loc = mticker.LogitLocator(nbins=100)
  256. _LogitHelper.assert_almost_equal(
  257. loc.tick_values(*lims),
  258. expected_ticks
  259. )
  260. @pytest.mark.parametrize("lims", ref_maxn_limits)
  261. def test_maxn_major(self, lims):
  262. """
  263. When the axis is zoomed, the locator must have the same behavior as
  264. MaxNLocator.
  265. """
  266. loc = mticker.LogitLocator(nbins=100)
  267. maxn_loc = mticker.MaxNLocator(nbins=100, steps=[1, 2, 5, 10])
  268. for nbins in (4, 8, 16):
  269. loc.set_params(nbins=nbins)
  270. maxn_loc.set_params(nbins=nbins)
  271. ticks = loc.tick_values(*lims)
  272. maxn_ticks = maxn_loc.tick_values(*lims)
  273. assert ticks.shape == maxn_ticks.shape
  274. assert (ticks == maxn_ticks).all()
  275. @pytest.mark.parametrize("lims", ref_basic_limits + ref_maxn_limits)
  276. def test_nbins_major(self, lims):
  277. """
  278. Assert logit locator for respecting nbins param.
  279. """
  280. basic_needed = int(-np.floor(np.log10(lims[0]))) * 2 + 1
  281. loc = mticker.LogitLocator(nbins=100)
  282. for nbins in range(basic_needed, 2, -1):
  283. loc.set_params(nbins=nbins)
  284. assert len(loc.tick_values(*lims)) <= nbins + 2
  285. @pytest.mark.parametrize(
  286. "lims, expected_low_ticks",
  287. zip(ref_basic_limits, ref_basic_major_ticks),
  288. )
  289. def test_minor(self, lims, expected_low_ticks):
  290. """
  291. In large scale, test the presence of minor,
  292. and assert no minor when major are subsampled.
  293. """
  294. expected_ticks = sorted(
  295. [*expected_low_ticks, 0.5, *(1 - expected_low_ticks)]
  296. )
  297. basic_needed = len(expected_ticks)
  298. loc = mticker.LogitLocator(nbins=100)
  299. minor_loc = mticker.LogitLocator(nbins=100, minor=True)
  300. for nbins in range(basic_needed, 2, -1):
  301. loc.set_params(nbins=nbins)
  302. minor_loc.set_params(nbins=nbins)
  303. major_ticks = loc.tick_values(*lims)
  304. minor_ticks = minor_loc.tick_values(*lims)
  305. if len(major_ticks) >= len(expected_ticks):
  306. # no subsample, we must have a lot of minors ticks
  307. assert (len(major_ticks) - 1) * 5 < len(minor_ticks)
  308. else:
  309. # subsample
  310. _LogitHelper.assert_almost_equal(
  311. np.sort(np.concatenate((major_ticks, minor_ticks))),
  312. expected_ticks,
  313. )
  314. def test_minor_attr(self):
  315. loc = mticker.LogitLocator(nbins=100)
  316. assert not loc.minor
  317. loc.minor = True
  318. assert loc.minor
  319. loc.set_params(minor=False)
  320. assert not loc.minor
  321. acceptable_vmin_vmax = [
  322. *(2.5 ** np.arange(-3, 0)),
  323. *(1 - 2.5 ** np.arange(-3, 0)),
  324. ]
  325. @pytest.mark.parametrize(
  326. "lims",
  327. [
  328. (a, b)
  329. for (a, b) in itertools.product(acceptable_vmin_vmax, repeat=2)
  330. if a != b
  331. ],
  332. )
  333. def test_nonsingular_ok(self, lims):
  334. """
  335. Create logit locator, and test the nonsingular method for acceptable
  336. value
  337. """
  338. loc = mticker.LogitLocator()
  339. lims2 = loc.nonsingular(*lims)
  340. assert sorted(lims) == sorted(lims2)
  341. @pytest.mark.parametrize("okval", acceptable_vmin_vmax)
  342. def test_nonsingular_nok(self, okval):
  343. """
  344. Create logit locator, and test the nonsingular method for non
  345. acceptable value
  346. """
  347. loc = mticker.LogitLocator()
  348. vmin, vmax = (-1, okval)
  349. vmin2, vmax2 = loc.nonsingular(vmin, vmax)
  350. assert vmax2 == vmax
  351. assert 0 < vmin2 < vmax2
  352. vmin, vmax = (okval, 2)
  353. vmin2, vmax2 = loc.nonsingular(vmin, vmax)
  354. assert vmin2 == vmin
  355. assert vmin2 < vmax2 < 1
  356. class TestFixedLocator:
  357. def test_set_params(self):
  358. """
  359. Create fixed locator with 5 nbins, and change it to something else.
  360. See if change was successful.
  361. Should not exception.
  362. """
  363. fixed = mticker.FixedLocator(range(0, 24), nbins=5)
  364. fixed.set_params(nbins=7)
  365. assert fixed.nbins == 7
  366. class TestIndexLocator:
  367. def test_set_params(self):
  368. """
  369. Create index locator with 3 base, 4 offset. and change it to something
  370. else. See if change was successful.
  371. Should not exception.
  372. """
  373. index = mticker.IndexLocator(base=3, offset=4)
  374. index.set_params(base=7, offset=7)
  375. assert index._base == 7
  376. assert index.offset == 7
  377. class TestSymmetricalLogLocator:
  378. def test_set_params(self):
  379. """
  380. Create symmetrical log locator with default subs =[1.0] numticks = 15,
  381. and change it to something else.
  382. See if change was successful.
  383. Should not exception.
  384. """
  385. sym = mticker.SymmetricalLogLocator(base=10, linthresh=1)
  386. sym.set_params(subs=[2.0], numticks=8)
  387. assert sym._subs == [2.0]
  388. assert sym.numticks == 8
  389. class TestScalarFormatter:
  390. offset_data = [
  391. (123, 189, 0),
  392. (-189, -123, 0),
  393. (12341, 12349, 12340),
  394. (-12349, -12341, -12340),
  395. (99999.5, 100010.5, 100000),
  396. (-100010.5, -99999.5, -100000),
  397. (99990.5, 100000.5, 100000),
  398. (-100000.5, -99990.5, -100000),
  399. (1233999, 1234001, 1234000),
  400. (-1234001, -1233999, -1234000),
  401. (1, 1, 1),
  402. (123, 123, 0),
  403. # Test cases courtesy of @WeatherGod
  404. (.4538, .4578, .45),
  405. (3789.12, 3783.1, 3780),
  406. (45124.3, 45831.75, 45000),
  407. (0.000721, 0.0007243, 0.00072),
  408. (12592.82, 12591.43, 12590),
  409. (9., 12., 0),
  410. (900., 1200., 0),
  411. (1900., 1200., 0),
  412. (0.99, 1.01, 1),
  413. (9.99, 10.01, 10),
  414. (99.99, 100.01, 100),
  415. (5.99, 6.01, 6),
  416. (15.99, 16.01, 16),
  417. (-0.452, 0.492, 0),
  418. (-0.492, 0.492, 0),
  419. (12331.4, 12350.5, 12300),
  420. (-12335.3, 12335.3, 0),
  421. ]
  422. use_offset_data = [True, False]
  423. # (sci_type, scilimits, lim, orderOfMag, fewticks)
  424. scilimits_data = [
  425. (False, (0, 0), (10.0, 20.0), 0, False),
  426. (True, (-2, 2), (-10, 20), 0, False),
  427. (True, (-2, 2), (-20, 10), 0, False),
  428. (True, (-2, 2), (-110, 120), 2, False),
  429. (True, (-2, 2), (-120, 110), 2, False),
  430. (True, (-2, 2), (-.001, 0.002), -3, False),
  431. (True, (-7, 7), (0.18e10, 0.83e10), 9, True),
  432. (True, (0, 0), (-1e5, 1e5), 5, False),
  433. (True, (6, 6), (-1e5, 1e5), 6, False),
  434. ]
  435. @pytest.mark.parametrize('left, right, offset', offset_data)
  436. def test_offset_value(self, left, right, offset):
  437. fig, ax = plt.subplots()
  438. formatter = ax.get_xaxis().get_major_formatter()
  439. with (pytest.warns(UserWarning, match='Attempting to set identical')
  440. if left == right else nullcontext()):
  441. ax.set_xlim(left, right)
  442. ax.get_xaxis()._update_ticks()
  443. assert formatter.offset == offset
  444. with (pytest.warns(UserWarning, match='Attempting to set identical')
  445. if left == right else nullcontext()):
  446. ax.set_xlim(right, left)
  447. ax.get_xaxis()._update_ticks()
  448. assert formatter.offset == offset
  449. @pytest.mark.parametrize('use_offset', use_offset_data)
  450. def test_use_offset(self, use_offset):
  451. with matplotlib.rc_context({'axes.formatter.useoffset': use_offset}):
  452. tmp_form = mticker.ScalarFormatter()
  453. assert use_offset == tmp_form.get_useOffset()
  454. @pytest.mark.parametrize(
  455. 'sci_type, scilimits, lim, orderOfMag, fewticks', scilimits_data)
  456. def test_scilimits(self, sci_type, scilimits, lim, orderOfMag, fewticks):
  457. tmp_form = mticker.ScalarFormatter()
  458. tmp_form.set_scientific(sci_type)
  459. tmp_form.set_powerlimits(scilimits)
  460. fig, ax = plt.subplots()
  461. ax.yaxis.set_major_formatter(tmp_form)
  462. ax.set_ylim(*lim)
  463. if fewticks:
  464. ax.yaxis.set_major_locator(mticker.MaxNLocator(4))
  465. tmp_form.set_locs(ax.yaxis.get_majorticklocs())
  466. assert orderOfMag == tmp_form.orderOfMagnitude
  467. class FakeAxis:
  468. """Allow Formatter to be called without having a "full" plot set up."""
  469. def __init__(self, vmin=1, vmax=10):
  470. self.vmin = vmin
  471. self.vmax = vmax
  472. def get_view_interval(self):
  473. return self.vmin, self.vmax
  474. class TestLogFormatterExponent:
  475. param_data = [
  476. (True, 4, np.arange(-3, 4.0), np.arange(-3, 4.0),
  477. ['-3', '-2', '-1', '0', '1', '2', '3']),
  478. # With labelOnlyBase=False, non-integer powers should be nicely
  479. # formatted.
  480. (False, 10, np.array([0.1, 0.00001, np.pi, 0.2, -0.2, -0.00001]),
  481. range(6), ['0.1', '1e-05', '3.14', '0.2', '-0.2', '-1e-05']),
  482. (False, 50, np.array([3, 5, 12, 42], dtype='float'), range(6),
  483. ['3', '5', '12', '42']),
  484. ]
  485. base_data = [2.0, 5.0, 10.0, np.pi, np.e]
  486. @pytest.mark.parametrize(
  487. 'labelOnlyBase, exponent, locs, positions, expected', param_data)
  488. @pytest.mark.parametrize('base', base_data)
  489. def test_basic(self, labelOnlyBase, base, exponent, locs, positions,
  490. expected):
  491. formatter = mticker.LogFormatterExponent(base=base,
  492. labelOnlyBase=labelOnlyBase)
  493. formatter.axis = FakeAxis(1, base**exponent)
  494. vals = base**locs
  495. labels = [formatter(x, pos) for (x, pos) in zip(vals, positions)]
  496. assert labels == expected
  497. def test_blank(self):
  498. # Should be a blank string for non-integer powers if labelOnlyBase=True
  499. formatter = mticker.LogFormatterExponent(base=10, labelOnlyBase=True)
  500. formatter.axis = FakeAxis()
  501. assert formatter(10**0.1) == ''
  502. class TestLogFormatterMathtext:
  503. fmt = mticker.LogFormatterMathtext()
  504. test_data = [
  505. (0, 1, '$\\mathdefault{10^{0}}$'),
  506. (0, 1e-2, '$\\mathdefault{10^{-2}}$'),
  507. (0, 1e2, '$\\mathdefault{10^{2}}$'),
  508. (3, 1, '$\\mathdefault{1}$'),
  509. (3, 1e-2, '$\\mathdefault{0.01}$'),
  510. (3, 1e2, '$\\mathdefault{100}$'),
  511. (3, 1e-3, '$\\mathdefault{10^{-3}}$'),
  512. (3, 1e3, '$\\mathdefault{10^{3}}$'),
  513. ]
  514. @pytest.mark.parametrize('min_exponent, value, expected', test_data)
  515. def test_min_exponent(self, min_exponent, value, expected):
  516. with matplotlib.rc_context({'axes.formatter.min_exponent':
  517. min_exponent}):
  518. assert self.fmt(value) == expected
  519. class TestLogFormatterSciNotation:
  520. test_data = [
  521. (2, 0.03125, '$\\mathdefault{2^{-5}}$'),
  522. (2, 1, '$\\mathdefault{2^{0}}$'),
  523. (2, 32, '$\\mathdefault{2^{5}}$'),
  524. (2, 0.0375, '$\\mathdefault{1.2\\times2^{-5}}$'),
  525. (2, 1.2, '$\\mathdefault{1.2\\times2^{0}}$'),
  526. (2, 38.4, '$\\mathdefault{1.2\\times2^{5}}$'),
  527. (10, -1, '$\\mathdefault{-10^{0}}$'),
  528. (10, 1e-05, '$\\mathdefault{10^{-5}}$'),
  529. (10, 1, '$\\mathdefault{10^{0}}$'),
  530. (10, 100000, '$\\mathdefault{10^{5}}$'),
  531. (10, 2e-05, '$\\mathdefault{2\\times10^{-5}}$'),
  532. (10, 2, '$\\mathdefault{2\\times10^{0}}$'),
  533. (10, 200000, '$\\mathdefault{2\\times10^{5}}$'),
  534. (10, 5e-05, '$\\mathdefault{5\\times10^{-5}}$'),
  535. (10, 5, '$\\mathdefault{5\\times10^{0}}$'),
  536. (10, 500000, '$\\mathdefault{5\\times10^{5}}$'),
  537. ]
  538. @pytest.mark.style('default')
  539. @pytest.mark.parametrize('base, value, expected', test_data)
  540. def test_basic(self, base, value, expected):
  541. formatter = mticker.LogFormatterSciNotation(base=base)
  542. formatter.sublabel = {1, 2, 5, 1.2}
  543. with matplotlib.rc_context({'text.usetex': False}):
  544. assert formatter(value) == expected
  545. class TestLogFormatter:
  546. pprint_data = [
  547. (3.141592654e-05, 0.001, '3.142e-5'),
  548. (0.0003141592654, 0.001, '3.142e-4'),
  549. (0.003141592654, 0.001, '3.142e-3'),
  550. (0.03141592654, 0.001, '3.142e-2'),
  551. (0.3141592654, 0.001, '3.142e-1'),
  552. (3.141592654, 0.001, '3.142'),
  553. (31.41592654, 0.001, '3.142e1'),
  554. (314.1592654, 0.001, '3.142e2'),
  555. (3141.592654, 0.001, '3.142e3'),
  556. (31415.92654, 0.001, '3.142e4'),
  557. (314159.2654, 0.001, '3.142e5'),
  558. (1e-05, 0.001, '1e-5'),
  559. (0.0001, 0.001, '1e-4'),
  560. (0.001, 0.001, '1e-3'),
  561. (0.01, 0.001, '1e-2'),
  562. (0.1, 0.001, '1e-1'),
  563. (1, 0.001, '1'),
  564. (10, 0.001, '10'),
  565. (100, 0.001, '100'),
  566. (1000, 0.001, '1000'),
  567. (10000, 0.001, '1e4'),
  568. (100000, 0.001, '1e5'),
  569. (3.141592654e-05, 0.015, '0'),
  570. (0.0003141592654, 0.015, '0'),
  571. (0.003141592654, 0.015, '0.003'),
  572. (0.03141592654, 0.015, '0.031'),
  573. (0.3141592654, 0.015, '0.314'),
  574. (3.141592654, 0.015, '3.142'),
  575. (31.41592654, 0.015, '31.416'),
  576. (314.1592654, 0.015, '314.159'),
  577. (3141.592654, 0.015, '3141.593'),
  578. (31415.92654, 0.015, '31415.927'),
  579. (314159.2654, 0.015, '314159.265'),
  580. (1e-05, 0.015, '0'),
  581. (0.0001, 0.015, '0'),
  582. (0.001, 0.015, '0.001'),
  583. (0.01, 0.015, '0.01'),
  584. (0.1, 0.015, '0.1'),
  585. (1, 0.015, '1'),
  586. (10, 0.015, '10'),
  587. (100, 0.015, '100'),
  588. (1000, 0.015, '1000'),
  589. (10000, 0.015, '10000'),
  590. (100000, 0.015, '100000'),
  591. (3.141592654e-05, 0.5, '0'),
  592. (0.0003141592654, 0.5, '0'),
  593. (0.003141592654, 0.5, '0.003'),
  594. (0.03141592654, 0.5, '0.031'),
  595. (0.3141592654, 0.5, '0.314'),
  596. (3.141592654, 0.5, '3.142'),
  597. (31.41592654, 0.5, '31.416'),
  598. (314.1592654, 0.5, '314.159'),
  599. (3141.592654, 0.5, '3141.593'),
  600. (31415.92654, 0.5, '31415.927'),
  601. (314159.2654, 0.5, '314159.265'),
  602. (1e-05, 0.5, '0'),
  603. (0.0001, 0.5, '0'),
  604. (0.001, 0.5, '0.001'),
  605. (0.01, 0.5, '0.01'),
  606. (0.1, 0.5, '0.1'),
  607. (1, 0.5, '1'),
  608. (10, 0.5, '10'),
  609. (100, 0.5, '100'),
  610. (1000, 0.5, '1000'),
  611. (10000, 0.5, '10000'),
  612. (100000, 0.5, '100000'),
  613. (3.141592654e-05, 5, '0'),
  614. (0.0003141592654, 5, '0'),
  615. (0.003141592654, 5, '0'),
  616. (0.03141592654, 5, '0.03'),
  617. (0.3141592654, 5, '0.31'),
  618. (3.141592654, 5, '3.14'),
  619. (31.41592654, 5, '31.42'),
  620. (314.1592654, 5, '314.16'),
  621. (3141.592654, 5, '3141.59'),
  622. (31415.92654, 5, '31415.93'),
  623. (314159.2654, 5, '314159.27'),
  624. (1e-05, 5, '0'),
  625. (0.0001, 5, '0'),
  626. (0.001, 5, '0'),
  627. (0.01, 5, '0.01'),
  628. (0.1, 5, '0.1'),
  629. (1, 5, '1'),
  630. (10, 5, '10'),
  631. (100, 5, '100'),
  632. (1000, 5, '1000'),
  633. (10000, 5, '10000'),
  634. (100000, 5, '100000'),
  635. (3.141592654e-05, 100, '0'),
  636. (0.0003141592654, 100, '0'),
  637. (0.003141592654, 100, '0'),
  638. (0.03141592654, 100, '0'),
  639. (0.3141592654, 100, '0.3'),
  640. (3.141592654, 100, '3.1'),
  641. (31.41592654, 100, '31.4'),
  642. (314.1592654, 100, '314.2'),
  643. (3141.592654, 100, '3141.6'),
  644. (31415.92654, 100, '31415.9'),
  645. (314159.2654, 100, '314159.3'),
  646. (1e-05, 100, '0'),
  647. (0.0001, 100, '0'),
  648. (0.001, 100, '0'),
  649. (0.01, 100, '0'),
  650. (0.1, 100, '0.1'),
  651. (1, 100, '1'),
  652. (10, 100, '10'),
  653. (100, 100, '100'),
  654. (1000, 100, '1000'),
  655. (10000, 100, '10000'),
  656. (100000, 100, '100000'),
  657. (3.141592654e-05, 1000000.0, '3.1e-5'),
  658. (0.0003141592654, 1000000.0, '3.1e-4'),
  659. (0.003141592654, 1000000.0, '3.1e-3'),
  660. (0.03141592654, 1000000.0, '3.1e-2'),
  661. (0.3141592654, 1000000.0, '3.1e-1'),
  662. (3.141592654, 1000000.0, '3.1'),
  663. (31.41592654, 1000000.0, '3.1e1'),
  664. (314.1592654, 1000000.0, '3.1e2'),
  665. (3141.592654, 1000000.0, '3.1e3'),
  666. (31415.92654, 1000000.0, '3.1e4'),
  667. (314159.2654, 1000000.0, '3.1e5'),
  668. (1e-05, 1000000.0, '1e-5'),
  669. (0.0001, 1000000.0, '1e-4'),
  670. (0.001, 1000000.0, '1e-3'),
  671. (0.01, 1000000.0, '1e-2'),
  672. (0.1, 1000000.0, '1e-1'),
  673. (1, 1000000.0, '1'),
  674. (10, 1000000.0, '10'),
  675. (100, 1000000.0, '100'),
  676. (1000, 1000000.0, '1000'),
  677. (10000, 1000000.0, '1e4'),
  678. (100000, 1000000.0, '1e5'),
  679. ]
  680. @pytest.mark.parametrize('value, domain, expected', pprint_data)
  681. def test_pprint(self, value, domain, expected):
  682. fmt = mticker.LogFormatter()
  683. label = fmt._pprint_val(value, domain)
  684. assert label == expected
  685. def _sub_labels(self, axis, subs=()):
  686. "Test whether locator marks subs to be labeled"
  687. fmt = axis.get_minor_formatter()
  688. minor_tlocs = axis.get_minorticklocs()
  689. fmt.set_locs(minor_tlocs)
  690. coefs = minor_tlocs / 10**(np.floor(np.log10(minor_tlocs)))
  691. label_expected = [round(c) in subs for c in coefs]
  692. label_test = [fmt(x) != '' for x in minor_tlocs]
  693. assert label_test == label_expected
  694. @pytest.mark.style('default')
  695. def test_sublabel(self):
  696. # test label locator
  697. fig, ax = plt.subplots()
  698. ax.set_xscale('log')
  699. ax.xaxis.set_major_locator(mticker.LogLocator(base=10, subs=[]))
  700. ax.xaxis.set_minor_locator(mticker.LogLocator(base=10,
  701. subs=np.arange(2, 10)))
  702. ax.xaxis.set_major_formatter(mticker.LogFormatter(labelOnlyBase=True))
  703. ax.xaxis.set_minor_formatter(mticker.LogFormatter(labelOnlyBase=False))
  704. # axis range above 3 decades, only bases are labeled
  705. ax.set_xlim(1, 1e4)
  706. fmt = ax.xaxis.get_major_formatter()
  707. fmt.set_locs(ax.xaxis.get_majorticklocs())
  708. show_major_labels = [fmt(x) != ''
  709. for x in ax.xaxis.get_majorticklocs()]
  710. assert np.all(show_major_labels)
  711. self._sub_labels(ax.xaxis, subs=[])
  712. # For the next two, if the numdec threshold in LogFormatter.set_locs
  713. # were 3, then the label sub would be 3 for 2-3 decades and (2, 5)
  714. # for 1-2 decades. With a threshold of 1, subs are not labeled.
  715. # axis range at 2 to 3 decades
  716. ax.set_xlim(1, 800)
  717. self._sub_labels(ax.xaxis, subs=[])
  718. # axis range at 1 to 2 decades
  719. ax.set_xlim(1, 80)
  720. self._sub_labels(ax.xaxis, subs=[])
  721. # axis range at 0.4 to 1 decades, label subs 2, 3, 4, 6
  722. ax.set_xlim(1, 8)
  723. self._sub_labels(ax.xaxis, subs=[2, 3, 4, 6])
  724. # axis range at 0 to 0.4 decades, label all
  725. ax.set_xlim(0.5, 0.9)
  726. self._sub_labels(ax.xaxis, subs=np.arange(2, 10, dtype=int))
  727. @pytest.mark.parametrize('val', [1, 10, 100, 1000])
  728. def test_LogFormatter_call(self, val):
  729. # test _num_to_string method used in __call__
  730. temp_lf = mticker.LogFormatter()
  731. temp_lf.axis = FakeAxis()
  732. assert temp_lf(val) == str(val)
  733. class TestLogitFormatter:
  734. @staticmethod
  735. def logit_deformatter(string):
  736. r"""
  737. Parser to convert string as r'$\mathdefault{1.41\cdot10^{-4}}$' in
  738. float 1.41e-4, as '0.5' or as r'$\mathdefault{\frac{1}{2}}$' in float
  739. 0.5,
  740. """
  741. match = re.match(
  742. r"[^\d]*"
  743. r"(?P<comp>1-)?"
  744. r"(?P<mant>\d*\.?\d*)?"
  745. r"(?:\\cdot)?"
  746. r"(?:10\^\{(?P<expo>-?\d*)})?"
  747. r"[^\d]*$",
  748. string,
  749. )
  750. if match:
  751. comp = match["comp"] is not None
  752. mantissa = float(match["mant"]) if match["mant"] else 1
  753. expo = int(match["expo"]) if match["expo"] is not None else 0
  754. value = mantissa * 10 ** expo
  755. if match["mant"] or match["expo"] is not None:
  756. if comp:
  757. return 1 - value
  758. return value
  759. match = re.match(
  760. r"[^\d]*\\frac\{(?P<num>\d+)\}\{(?P<deno>\d+)\}[^\d]*$", string
  761. )
  762. if match:
  763. num, deno = float(match["num"]), float(match["deno"])
  764. return num / deno
  765. raise ValueError("not formatted by LogitFormatter")
  766. @pytest.mark.parametrize(
  767. "fx, x",
  768. [
  769. (r"STUFF0.41OTHERSTUFF", 0.41),
  770. (r"STUFF1.41\cdot10^{-2}OTHERSTUFF", 1.41e-2),
  771. (r"STUFF1-0.41OTHERSTUFF", 1 - 0.41),
  772. (r"STUFF1-1.41\cdot10^{-2}OTHERSTUFF", 1 - 1.41e-2),
  773. (r"STUFF", None),
  774. (r"STUFF12.4e-3OTHERSTUFF", None),
  775. ],
  776. )
  777. def test_logit_deformater(self, fx, x):
  778. if x is None:
  779. with pytest.raises(ValueError):
  780. TestLogitFormatter.logit_deformatter(fx)
  781. else:
  782. y = TestLogitFormatter.logit_deformatter(fx)
  783. assert _LogitHelper.isclose(x, y)
  784. decade_test = sorted(
  785. [10 ** (-i) for i in range(1, 10)]
  786. + [1 - 10 ** (-i) for i in range(1, 10)]
  787. + [1 / 2]
  788. )
  789. @pytest.mark.parametrize("x", decade_test)
  790. def test_basic(self, x):
  791. """
  792. Test the formatted value correspond to the value for ideal ticks in
  793. logit space.
  794. """
  795. formatter = mticker.LogitFormatter(use_overline=False)
  796. formatter.set_locs(self.decade_test)
  797. s = formatter(x)
  798. x2 = TestLogitFormatter.logit_deformatter(s)
  799. assert _LogitHelper.isclose(x, x2)
  800. @pytest.mark.parametrize("x", (-1, -0.5, -0.1, 1.1, 1.5, 2))
  801. def test_invalid(self, x):
  802. """
  803. Test that invalid value are formatted with empty string without
  804. raising exception.
  805. """
  806. formatter = mticker.LogitFormatter(use_overline=False)
  807. formatter.set_locs(self.decade_test)
  808. s = formatter(x)
  809. assert s == ""
  810. @pytest.mark.parametrize("x", 1 / (1 + np.exp(-np.linspace(-7, 7, 10))))
  811. def test_variablelength(self, x):
  812. """
  813. The format length should change depending on the neighbor labels.
  814. """
  815. formatter = mticker.LogitFormatter(use_overline=False)
  816. for N in (10, 20, 50, 100, 200, 1000, 2000, 5000, 10000):
  817. if x + 1 / N < 1:
  818. formatter.set_locs([x - 1 / N, x, x + 1 / N])
  819. sx = formatter(x)
  820. sx1 = formatter(x + 1 / N)
  821. d = (
  822. TestLogitFormatter.logit_deformatter(sx1)
  823. - TestLogitFormatter.logit_deformatter(sx)
  824. )
  825. assert 0 < d < 2 / N
  826. lims_minor_major = [
  827. (True, (5e-8, 1 - 5e-8), ((25, False), (75, False))),
  828. (True, (5e-5, 1 - 5e-5), ((25, False), (75, True))),
  829. (True, (5e-2, 1 - 5e-2), ((25, True), (75, True))),
  830. (False, (0.75, 0.76, 0.77), ((7, True), (25, True), (75, True))),
  831. ]
  832. @pytest.mark.parametrize("method, lims, cases", lims_minor_major)
  833. def test_minor_vs_major(self, method, lims, cases):
  834. """
  835. Test minor/major displays.
  836. """
  837. if method:
  838. min_loc = mticker.LogitLocator(minor=True)
  839. ticks = min_loc.tick_values(*lims)
  840. else:
  841. ticks = np.array(lims)
  842. min_form = mticker.LogitFormatter(minor=True)
  843. for threshold, has_minor in cases:
  844. min_form.set_minor_threshold(threshold)
  845. formatted = min_form.format_ticks(ticks)
  846. labelled = [f for f in formatted if len(f) > 0]
  847. if has_minor:
  848. assert len(labelled) > 0, (threshold, has_minor)
  849. else:
  850. assert len(labelled) == 0, (threshold, has_minor)
  851. def test_minor_number(self):
  852. """
  853. Test the parameter minor_number
  854. """
  855. min_loc = mticker.LogitLocator(minor=True)
  856. min_form = mticker.LogitFormatter(minor=True)
  857. ticks = min_loc.tick_values(5e-2, 1 - 5e-2)
  858. for minor_number in (2, 4, 8, 16):
  859. min_form.set_minor_number(minor_number)
  860. formatted = min_form.format_ticks(ticks)
  861. labelled = [f for f in formatted if len(f) > 0]
  862. assert len(labelled) == minor_number
  863. def test_use_overline(self):
  864. """
  865. Test the parameter use_overline
  866. """
  867. x = 1 - 1e-2
  868. fx1 = r"$\mathdefault{1-10^{-2}}$"
  869. fx2 = r"$\mathdefault{\overline{10^{-2}}}$"
  870. form = mticker.LogitFormatter(use_overline=False)
  871. assert form(x) == fx1
  872. form.use_overline(True)
  873. assert form(x) == fx2
  874. form.use_overline(False)
  875. assert form(x) == fx1
  876. def test_one_half(self):
  877. """
  878. Test the parameter one_half
  879. """
  880. form = mticker.LogitFormatter()
  881. assert r"\frac{1}{2}" in form(1/2)
  882. form.set_one_half("1/2")
  883. assert "1/2" in form(1/2)
  884. form.set_one_half("one half")
  885. assert "one half" in form(1/2)
  886. @pytest.mark.parametrize("N", (100, 253, 754))
  887. def test_format_data_short(self, N):
  888. locs = np.linspace(0, 1, N)[1:-1]
  889. form = mticker.LogitFormatter()
  890. for x in locs:
  891. fx = form.format_data_short(x)
  892. if fx.startswith("1-"):
  893. x2 = 1 - float(fx[2:])
  894. else:
  895. x2 = float(fx)
  896. assert np.abs(x - x2) < 1 / N
  897. class TestFormatStrFormatter:
  898. def test_basic(self):
  899. # test % style formatter
  900. tmp_form = mticker.FormatStrFormatter('%05d')
  901. assert '00002' == tmp_form(2)
  902. class TestStrMethodFormatter:
  903. test_data = [
  904. ('{x:05d}', (2,), '00002'),
  905. ('{x:03d}-{pos:02d}', (2, 1), '002-01'),
  906. ]
  907. @pytest.mark.parametrize('format, input, expected', test_data)
  908. def test_basic(self, format, input, expected):
  909. fmt = mticker.StrMethodFormatter(format)
  910. assert fmt(*input) == expected
  911. class TestEngFormatter:
  912. # (unicode_minus, input, expected) where ''expected'' corresponds to the
  913. # outputs respectively returned when (places=None, places=0, places=2)
  914. # unicode_minus is a boolean value for the rcParam['axes.unicode_minus']
  915. raw_format_data = [
  916. (False, -1234.56789, ('-1.23457 k', '-1 k', '-1.23 k')),
  917. (True, -1234.56789, ('\N{MINUS SIGN}1.23457 k', '\N{MINUS SIGN}1 k',
  918. '\N{MINUS SIGN}1.23 k')),
  919. (False, -1.23456789, ('-1.23457', '-1', '-1.23')),
  920. (True, -1.23456789, ('\N{MINUS SIGN}1.23457', '\N{MINUS SIGN}1',
  921. '\N{MINUS SIGN}1.23')),
  922. (False, -0.123456789, ('-123.457 m', '-123 m', '-123.46 m')),
  923. (True, -0.123456789, ('\N{MINUS SIGN}123.457 m', '\N{MINUS SIGN}123 m',
  924. '\N{MINUS SIGN}123.46 m')),
  925. (False, -0.00123456789, ('-1.23457 m', '-1 m', '-1.23 m')),
  926. (True, -0.00123456789, ('\N{MINUS SIGN}1.23457 m', '\N{MINUS SIGN}1 m',
  927. '\N{MINUS SIGN}1.23 m')),
  928. (True, -0.0, ('0', '0', '0.00')),
  929. (True, -0, ('0', '0', '0.00')),
  930. (True, 0, ('0', '0', '0.00')),
  931. (True, 1.23456789e-6, ('1.23457 µ', '1 µ', '1.23 µ')),
  932. (True, 0.123456789, ('123.457 m', '123 m', '123.46 m')),
  933. (True, 0.1, ('100 m', '100 m', '100.00 m')),
  934. (True, 1, ('1', '1', '1.00')),
  935. (True, 1.23456789, ('1.23457', '1', '1.23')),
  936. # places=0: corner-case rounding
  937. (True, 999.9, ('999.9', '1 k', '999.90')),
  938. # corner-case rounding for all
  939. (True, 999.9999, ('1 k', '1 k', '1.00 k')),
  940. # negative corner-case
  941. (False, -999.9999, ('-1 k', '-1 k', '-1.00 k')),
  942. (True, -999.9999, ('\N{MINUS SIGN}1 k', '\N{MINUS SIGN}1 k',
  943. '\N{MINUS SIGN}1.00 k')),
  944. (True, 1000, ('1 k', '1 k', '1.00 k')),
  945. (True, 1001, ('1.001 k', '1 k', '1.00 k')),
  946. (True, 100001, ('100.001 k', '100 k', '100.00 k')),
  947. (True, 987654.321, ('987.654 k', '988 k', '987.65 k')),
  948. # OoR value (> 1000 Y)
  949. (True, 1.23e27, ('1230 Y', '1230 Y', '1230.00 Y'))
  950. ]
  951. @pytest.mark.parametrize('unicode_minus, input, expected', raw_format_data)
  952. def test_params(self, unicode_minus, input, expected):
  953. """
  954. Test the formatting of EngFormatter for various values of the 'places'
  955. argument, in several cases:
  956. 0. without a unit symbol but with a (default) space separator;
  957. 1. with both a unit symbol and a (default) space separator;
  958. 2. with both a unit symbol and some non default separators;
  959. 3. without a unit symbol but with some non default separators.
  960. Note that cases 2. and 3. are looped over several separator strings.
  961. """
  962. plt.rcParams['axes.unicode_minus'] = unicode_minus
  963. UNIT = 's' # seconds
  964. DIGITS = '0123456789' # %timeit showed 10-20% faster search than set
  965. # Case 0: unit='' (default) and sep=' ' (default).
  966. # 'expected' already corresponds to this reference case.
  967. exp_outputs = expected
  968. formatters = (
  969. mticker.EngFormatter(), # places=None (default)
  970. mticker.EngFormatter(places=0),
  971. mticker.EngFormatter(places=2)
  972. )
  973. for _formatter, _exp_output in zip(formatters, exp_outputs):
  974. assert _formatter(input) == _exp_output
  975. # Case 1: unit=UNIT and sep=' ' (default).
  976. # Append a unit symbol to the reference case.
  977. # Beware of the values in [1, 1000), where there is no prefix!
  978. exp_outputs = (_s + " " + UNIT if _s[-1] in DIGITS # case w/o prefix
  979. else _s + UNIT for _s in expected)
  980. formatters = (
  981. mticker.EngFormatter(unit=UNIT), # places=None (default)
  982. mticker.EngFormatter(unit=UNIT, places=0),
  983. mticker.EngFormatter(unit=UNIT, places=2)
  984. )
  985. for _formatter, _exp_output in zip(formatters, exp_outputs):
  986. assert _formatter(input) == _exp_output
  987. # Test several non default separators: no separator, a narrow
  988. # no-break space (unicode character) and an extravagant string.
  989. for _sep in ("", "\N{NARROW NO-BREAK SPACE}", "@_@"):
  990. # Case 2: unit=UNIT and sep=_sep.
  991. # Replace the default space separator from the reference case
  992. # with the tested one `_sep` and append a unit symbol to it.
  993. exp_outputs = (_s + _sep + UNIT if _s[-1] in DIGITS # no prefix
  994. else _s.replace(" ", _sep) + UNIT
  995. for _s in expected)
  996. formatters = (
  997. mticker.EngFormatter(unit=UNIT, sep=_sep), # places=None
  998. mticker.EngFormatter(unit=UNIT, places=0, sep=_sep),
  999. mticker.EngFormatter(unit=UNIT, places=2, sep=_sep)
  1000. )
  1001. for _formatter, _exp_output in zip(formatters, exp_outputs):
  1002. assert _formatter(input) == _exp_output
  1003. # Case 3: unit='' (default) and sep=_sep.
  1004. # Replace the default space separator from the reference case
  1005. # with the tested one `_sep`. Reference case is already unitless.
  1006. exp_outputs = (_s.replace(" ", _sep) for _s in expected)
  1007. formatters = (
  1008. mticker.EngFormatter(sep=_sep), # places=None (default)
  1009. mticker.EngFormatter(places=0, sep=_sep),
  1010. mticker.EngFormatter(places=2, sep=_sep)
  1011. )
  1012. for _formatter, _exp_output in zip(formatters, exp_outputs):
  1013. assert _formatter(input) == _exp_output
  1014. def test_engformatter_usetex_useMathText():
  1015. fig, ax = plt.subplots()
  1016. ax.plot([0, 500, 1000], [0, 500, 1000])
  1017. ax.set_xticks([0, 500, 1000])
  1018. for formatter in (mticker.EngFormatter(usetex=True),
  1019. mticker.EngFormatter(useMathText=True)):
  1020. ax.xaxis.set_major_formatter(formatter)
  1021. fig.canvas.draw()
  1022. x_tick_label_text = [labl.get_text() for labl in ax.get_xticklabels()]
  1023. # Checking if the dollar `$` signs have been inserted around numbers
  1024. # in tick labels.
  1025. assert x_tick_label_text == ['$0$', '$500$', '$1$ k']
  1026. class TestPercentFormatter:
  1027. percent_data = [
  1028. # Check explicitly set decimals over different intervals and values
  1029. (100, 0, '%', 120, 100, '120%'),
  1030. (100, 0, '%', 100, 90, '100%'),
  1031. (100, 0, '%', 90, 50, '90%'),
  1032. (100, 0, '%', -1.7, 40, '-2%'),
  1033. (100, 1, '%', 90.0, 100, '90.0%'),
  1034. (100, 1, '%', 80.1, 90, '80.1%'),
  1035. (100, 1, '%', 70.23, 50, '70.2%'),
  1036. # 60.554 instead of 60.55: see https://bugs.python.org/issue5118
  1037. (100, 1, '%', -60.554, 40, '-60.6%'),
  1038. # Check auto decimals over different intervals and values
  1039. (100, None, '%', 95, 1, '95.00%'),
  1040. (1.0, None, '%', 3, 6, '300%'),
  1041. (17.0, None, '%', 1, 8.5, '6%'),
  1042. (17.0, None, '%', 1, 8.4, '5.9%'),
  1043. (5, None, '%', -100, 0.000001, '-2000.00000%'),
  1044. # Check percent symbol
  1045. (1.0, 2, None, 1.2, 100, '120.00'),
  1046. (75, 3, '', 50, 100, '66.667'),
  1047. (42, None, '^^Foobar$$', 21, 12, '50.0^^Foobar$$'),
  1048. ]
  1049. percent_ids = [
  1050. # Check explicitly set decimals over different intervals and values
  1051. 'decimals=0, x>100%',
  1052. 'decimals=0, x=100%',
  1053. 'decimals=0, x<100%',
  1054. 'decimals=0, x<0%',
  1055. 'decimals=1, x>100%',
  1056. 'decimals=1, x=100%',
  1057. 'decimals=1, x<100%',
  1058. 'decimals=1, x<0%',
  1059. # Check auto decimals over different intervals and values
  1060. 'autodecimal, x<100%, display_range=1',
  1061. 'autodecimal, x>100%, display_range=6 (custom xmax test)',
  1062. 'autodecimal, x<100%, display_range=8.5 (autodecimal test 1)',
  1063. 'autodecimal, x<100%, display_range=8.4 (autodecimal test 2)',
  1064. 'autodecimal, x<-100%, display_range=1e-6 (tiny display range)',
  1065. # Check percent symbol
  1066. 'None as percent symbol',
  1067. 'Empty percent symbol',
  1068. 'Custom percent symbol',
  1069. ]
  1070. latex_data = [
  1071. (False, False, r'50\{t}%'),
  1072. (False, True, r'50\\\{t\}\%'),
  1073. (True, False, r'50\{t}%'),
  1074. (True, True, r'50\{t}%'),
  1075. ]
  1076. @pytest.mark.parametrize(
  1077. 'xmax, decimals, symbol, x, display_range, expected',
  1078. percent_data, ids=percent_ids)
  1079. def test_basic(self, xmax, decimals, symbol,
  1080. x, display_range, expected):
  1081. formatter = mticker.PercentFormatter(xmax, decimals, symbol)
  1082. with matplotlib.rc_context(rc={'text.usetex': False}):
  1083. assert formatter.format_pct(x, display_range) == expected
  1084. @pytest.mark.parametrize('is_latex, usetex, expected', latex_data)
  1085. def test_latex(self, is_latex, usetex, expected):
  1086. fmt = mticker.PercentFormatter(symbol='\\{t}%', is_latex=is_latex)
  1087. with matplotlib.rc_context(rc={'text.usetex': usetex}):
  1088. assert fmt.format_pct(50, 100) == expected
  1089. def test_majformatter_type():
  1090. fig, ax = plt.subplots()
  1091. with pytest.raises(TypeError):
  1092. ax.xaxis.set_major_formatter(matplotlib.ticker.LogLocator())
  1093. def test_minformatter_type():
  1094. fig, ax = plt.subplots()
  1095. with pytest.raises(TypeError):
  1096. ax.xaxis.set_minor_formatter(matplotlib.ticker.LogLocator())
  1097. def test_majlocator_type():
  1098. fig, ax = plt.subplots()
  1099. with pytest.raises(TypeError):
  1100. ax.xaxis.set_major_locator(matplotlib.ticker.LogFormatter())
  1101. def test_minlocator_type():
  1102. fig, ax = plt.subplots()
  1103. with pytest.raises(TypeError):
  1104. ax.xaxis.set_minor_locator(matplotlib.ticker.LogFormatter())
  1105. def test_minorticks_rc():
  1106. fig = plt.figure()
  1107. def minorticksubplot(xminor, yminor, i):
  1108. rc = {'xtick.minor.visible': xminor,
  1109. 'ytick.minor.visible': yminor}
  1110. with plt.rc_context(rc=rc):
  1111. ax = fig.add_subplot(2, 2, i)
  1112. assert (len(ax.xaxis.get_minor_ticks()) > 0) == xminor
  1113. assert (len(ax.yaxis.get_minor_ticks()) > 0) == yminor
  1114. minorticksubplot(False, False, 1)
  1115. minorticksubplot(True, False, 2)
  1116. minorticksubplot(False, True, 3)
  1117. minorticksubplot(True, True, 4)
  1118. @pytest.mark.parametrize('remove_overlapping_locs, expected_num',
  1119. ((True, 6),
  1120. (None, 6), # this tests the default
  1121. (False, 9)))
  1122. def test_remove_overlap(remove_overlapping_locs, expected_num):
  1123. import numpy as np
  1124. import matplotlib.dates as mdates
  1125. t = np.arange("2018-11-03", "2018-11-06", dtype="datetime64")
  1126. x = np.ones(len(t))
  1127. fig, ax = plt.subplots()
  1128. ax.plot(t, x)
  1129. ax.xaxis.set_major_locator(mdates.DayLocator())
  1130. ax.xaxis.set_major_formatter(mdates.DateFormatter('\n%a'))
  1131. ax.xaxis.set_minor_locator(mdates.HourLocator((0, 6, 12, 18)))
  1132. ax.xaxis.set_minor_formatter(mdates.DateFormatter('%H:%M'))
  1133. # force there to be extra ticks
  1134. ax.xaxis.get_minor_ticks(15)
  1135. if remove_overlapping_locs is not None:
  1136. ax.xaxis.remove_overlapping_locs = remove_overlapping_locs
  1137. # check that getter/setter exists
  1138. current = ax.xaxis.remove_overlapping_locs
  1139. assert (current == ax.xaxis.get_remove_overlapping_locs())
  1140. plt.setp(ax.xaxis, remove_overlapping_locs=current)
  1141. new = ax.xaxis.remove_overlapping_locs
  1142. assert (new == ax.xaxis.remove_overlapping_locs)
  1143. # check that the accessors filter correctly
  1144. # this is the method that does the actual filtering
  1145. assert len(ax.xaxis.get_minorticklocs()) == expected_num
  1146. # these three are derivative
  1147. assert len(ax.xaxis.get_minor_ticks()) == expected_num
  1148. assert len(ax.xaxis.get_minorticklabels()) == expected_num
  1149. assert len(ax.xaxis.get_minorticklines()) == expected_num*2
  1150. @pytest.mark.parametrize('sub', [
  1151. ['hi', 'aardvark'],
  1152. np.zeros((2, 2))])
  1153. def test_bad_locator_subs(sub):
  1154. ll = mticker.LogLocator()
  1155. with pytest.raises(ValueError):
  1156. ll.subs(sub)