diff --git a/doc/users/next_whats_new/2019-01-25-AH-modify-minor-tick-spacing.rst b/doc/users/next_whats_new/2019-01-25-AH-modify-minor-tick-spacing.rst new file mode 100644 index 000000000000..dd2f1a97df74 --- /dev/null +++ b/doc/users/next_whats_new/2019-01-25-AH-modify-minor-tick-spacing.rst @@ -0,0 +1,5 @@ +Adjust default minor tick spacing +````````````````````````````````` + +Default minor tick spacing was changed from 0.625 to 0.5 for major ticks spaced +2.5 units apart. diff --git a/lib/matplotlib/tests/test_ticker.py b/lib/matplotlib/tests/test_ticker.py index 55f125608382..2ca5cbe58466 100644 --- a/lib/matplotlib/tests/test_ticker.py +++ b/lib/matplotlib/tests/test_ticker.py @@ -99,9 +99,7 @@ def test_basic(self): # NB: the following values are assuming that *xlim* is [0, 5] params = [ (0, 0), # no major tick => no minor tick either - (1, 0), # a single major tick => no minor tick - (2, 4), # 1 "nice" major step => 1*5 minor **divisions** - (3, 6) # 2 "not nice" major steps => 2*4 minor **divisions** + (1, 0) # a single major tick => no minor tick ] @pytest.mark.parametrize('nb_majorticks, expected_nb_minorticks', params) @@ -116,6 +114,32 @@ def test_low_number_of_majorticks( ax.xaxis.set_minor_locator(mticker.AutoMinorLocator()) assert len(ax.xaxis.get_minorticklocs()) == expected_nb_minorticks + majorstep_minordivisions = [(1, 5), + (2, 4), + (2.5, 5), + (5, 5), + (10, 5)] + + # This test is meant to verify the parameterization for + # test_number_of_minor_ticks + def test_using_all_default_major_steps(self): + with matplotlib.rc_context({'_internal.classic_mode': False}): + majorsteps = [x[0] for x in self.majorstep_minordivisions] + assert np.allclose(majorsteps, mticker.AutoLocator()._steps) + + @pytest.mark.parametrize('major_step, expected_nb_minordivisions', + majorstep_minordivisions) + def test_number_of_minor_ticks( + self, major_step, expected_nb_minordivisions): + fig, ax = plt.subplots() + xlims = (0, major_step) + ax.set_xlim(*xlims) + ax.set_xticks(xlims) + ax.minorticks_on() + ax.xaxis.set_minor_locator(mticker.AutoMinorLocator()) + nb_minor_divisions = len(ax.xaxis.get_minorticklocs()) + 1 + assert nb_minor_divisions == expected_nb_minordivisions + limits = [(0, 1.39), (0, 0.139), (0, 0.11e-19), (0, 0.112e-12), (-2.0e-07, -3.3e-08), (1.20e-06, 1.42e-06), diff --git a/lib/matplotlib/ticker.py b/lib/matplotlib/ticker.py index f952e604a12a..13ffd9f48b6a 100644 --- a/lib/matplotlib/ticker.py +++ b/lib/matplotlib/ticker.py @@ -2620,8 +2620,10 @@ def __call__(self): return [] if self.ndivs is None: - x = int(np.round(10 ** (np.log10(majorstep) % 1))) - if x in [1, 5, 10]: + + majorstep_no_exponent = 10 ** (np.log10(majorstep) % 1) + + if np.isclose(majorstep_no_exponent, [1.0, 2.5, 5.0, 10.0]).any(): ndivs = 5 else: ndivs = 4