Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c2a9793

Browse files
authored
Merge pull request #24661 from aalmasmari/Plot-first-and-last-minor-ticks-#22331
Plots first and last minor ticks #22331
2 parents 1d1d8ba + d1a9825 commit c2a9793

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Placing of maximum and minimum minor ticks
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
Calculation of minor tick locations has been corrected to make the maximum and
5+
minimum minor ticks more consistent. In some cases this results in an extra
6+
minor tick on an Axis.

lib/matplotlib/tests/test_colorbar.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,8 @@ def test_colorbar_minorticks_on_off():
396396
cbar.minorticks_on()
397397
np.testing.assert_almost_equal(
398398
cbar.ax.yaxis.get_minorticklocs(),
399-
[-1.1, -0.9, -0.8, -0.7, -0.6, -0.4, -0.3, -0.2, -0.1,
400-
0.1, 0.2, 0.3, 0.4, 0.6, 0.7, 0.8, 0.9, 1.1, 1.2, 1.3])
399+
[-1.2, -1.1, -0.9, -0.8, -0.7, -0.6, -0.4, -0.3, -0.2, -0.1,
400+
0.1, 0.2, 0.3, 0.4, 0.6, 0.7, 0.8, 0.9, 1.1, 1.2])
401401

402402
# tests for github issue #13257 and PR #13265
403403
data = np.random.uniform(low=1, high=10, size=(20, 20))

lib/matplotlib/tests/test_ticker.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,25 @@ def test_basic(self):
106106
(1, 0) # a single major tick => no minor tick
107107
]
108108

109+
def test_first_and_last_minorticks(self):
110+
"""
111+
Test that first and last minor tick appear as expected.
112+
"""
113+
# This test is related to issue #22331
114+
fig, ax = plt.subplots()
115+
ax.set_xlim(-1.9, 1.9)
116+
ax.xaxis.set_minor_locator(mticker.AutoMinorLocator())
117+
test_value = np.array([-1.9, -1.8, -1.7, -1.6, -1.4, -1.3, -1.2, -1.1,
118+
-0.9, -0.8, -0.7, -0.6, -0.4, -0.3, -0.2, -0.1,
119+
0.1, 0.2, 0.3, 0.4, 0.6, 0.7, 0.8, 0.9, 1.1,
120+
1.2, 1.3, 1.4, 1.6, 1.7, 1.8, 1.9])
121+
assert_almost_equal(ax.xaxis.get_ticklocs(minor=True), test_value)
122+
123+
ax.set_xlim(-5, 5)
124+
test_value = np.array([-5.0, -4.5, -3.5, -3.0, -2.5, -1.5, -1.0, -0.5,
125+
0.5, 1.0, 1.5, 2.5, 3.0, 3.5, 4.5, 5.0])
126+
assert_almost_equal(ax.xaxis.get_ticklocs(minor=True), test_value)
127+
109128
@pytest.mark.parametrize('nb_majorticks, expected_nb_minorticks', params)
110129
def test_low_number_of_majorticks(
111130
self, nb_majorticks, expected_nb_minorticks):

lib/matplotlib/ticker.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2908,9 +2908,9 @@ def __call__(self):
29082908
vmin, vmax = vmax, vmin
29092909

29102910
t0 = majorlocs[0]
2911-
tmin = ((vmin - t0) // minorstep + 1) * minorstep
2912-
tmax = ((vmax - t0) // minorstep + 1) * minorstep
2913-
locs = np.arange(tmin, tmax, minorstep) + t0
2911+
tmin = round((vmin - t0) / minorstep)
2912+
tmax = round((vmax - t0) / minorstep) + 1
2913+
locs = (np.arange(tmin, tmax) * minorstep) + t0
29142914

29152915
return self.raise_if_exceeds(locs)
29162916

0 commit comments

Comments
 (0)