Skip to content

Commit e17b602

Browse files
authored
Merge pull request #23049 from oscargus/ticksminor
Add `minor` keyword argument to `plt.x/yticks`
2 parents 2a632f7 + 3fd93e9 commit e17b602

File tree

3 files changed

+46
-10
lines changed

3 files changed

+46
-10
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
``plt.xticks`` and ``plt.yticks`` support *minor* keyword argument
2+
------------------------------------------------------------------
3+
4+
It is now possible to set or get minor ticks using `.pyplot.xticks` and
5+
`.pyplot.yticks` by setting ``minor=True``.
6+
7+
.. plot::
8+
:include-source: true
9+
10+
import matplotlib.pyplot as plt
11+
plt.figure()
12+
plt.plot([1, 2, 3, 3.5], [2, 1, 0, -0.5])
13+
plt.xticks([1, 2, 3], ["One", "Zwei", "Trois"])
14+
plt.xticks([1.414, 2.5, 3.142],
15+
[r"$\sqrt{2}$", r"$\frac{5}{2}$", r"$\pi$"], minor=True)

lib/matplotlib/pyplot.py

+16-10
Original file line numberDiff line numberDiff line change
@@ -1751,7 +1751,7 @@ def ylim(*args, **kwargs):
17511751
return ret
17521752

17531753

1754-
def xticks(ticks=None, labels=None, **kwargs):
1754+
def xticks(ticks=None, labels=None, *, minor=False, **kwargs):
17551755
"""
17561756
Get or set the current tick locations and labels of the x-axis.
17571757
@@ -1764,6 +1764,9 @@ def xticks(ticks=None, labels=None, **kwargs):
17641764
labels : array-like, optional
17651765
The labels to place at the given *ticks* locations. This argument can
17661766
only be passed if *ticks* is passed as well.
1767+
minor : bool, default: False
1768+
If ``False``, get/set the major ticks/labels; if ``True``, the minor
1769+
ticks/labels.
17671770
**kwargs
17681771
`.Text` properties can be used to control the appearance of the labels.
17691772
@@ -1794,24 +1797,24 @@ def xticks(ticks=None, labels=None, **kwargs):
17941797
ax = gca()
17951798

17961799
if ticks is None:
1797-
locs = ax.get_xticks()
1800+
locs = ax.get_xticks(minor=minor)
17981801
if labels is not None:
17991802
raise TypeError("xticks(): Parameter 'labels' can't be set "
18001803
"without setting 'ticks'")
18011804
else:
1802-
locs = ax.set_xticks(ticks)
1805+
locs = ax.set_xticks(ticks, minor=minor)
18031806

18041807
if labels is None:
1805-
labels = ax.get_xticklabels()
1808+
labels = ax.get_xticklabels(minor=minor)
18061809
for l in labels:
18071810
l._internal_update(kwargs)
18081811
else:
1809-
labels = ax.set_xticklabels(labels, **kwargs)
1812+
labels = ax.set_xticklabels(labels, minor=minor, **kwargs)
18101813

18111814
return locs, labels
18121815

18131816

1814-
def yticks(ticks=None, labels=None, **kwargs):
1817+
def yticks(ticks=None, labels=None, *, minor=False, **kwargs):
18151818
"""
18161819
Get or set the current tick locations and labels of the y-axis.
18171820
@@ -1824,6 +1827,9 @@ def yticks(ticks=None, labels=None, **kwargs):
18241827
labels : array-like, optional
18251828
The labels to place at the given *ticks* locations. This argument can
18261829
only be passed if *ticks* is passed as well.
1830+
minor : bool, default: False
1831+
If ``False``, get/set the major ticks/labels; if ``True``, the minor
1832+
ticks/labels.
18271833
**kwargs
18281834
`.Text` properties can be used to control the appearance of the labels.
18291835
@@ -1854,19 +1860,19 @@ def yticks(ticks=None, labels=None, **kwargs):
18541860
ax = gca()
18551861

18561862
if ticks is None:
1857-
locs = ax.get_yticks()
1863+
locs = ax.get_yticks(minor=minor)
18581864
if labels is not None:
18591865
raise TypeError("yticks(): Parameter 'labels' can't be set "
18601866
"without setting 'ticks'")
18611867
else:
1862-
locs = ax.set_yticks(ticks)
1868+
locs = ax.set_yticks(ticks, minor=minor)
18631869

18641870
if labels is None:
1865-
labels = ax.get_yticklabels()
1871+
labels = ax.get_yticklabels(minor=minor)
18661872
for l in labels:
18671873
l._internal_update(kwargs)
18681874
else:
1869-
labels = ax.set_yticklabels(labels, **kwargs)
1875+
labels = ax.set_yticklabels(labels, minor=minor, **kwargs)
18701876

18711877
return locs, labels
18721878

lib/matplotlib/tests/test_pyplot.py

+15
Original file line numberDiff line numberDiff line change
@@ -383,3 +383,18 @@ def test_doc_pyplot_summary():
383383
f"The following functions are listed in the pyplot documentation, "
384384
f"but they do not exist in pyplot. "
385385
f"Please remove them from doc/api/pyplot_summary.rst: {extra!r}")
386+
387+
388+
def test_minor_ticks():
389+
plt.figure()
390+
plt.plot(np.arange(1, 10))
391+
tick_pos, tick_labels = plt.xticks(minor=True)
392+
assert np.all(tick_labels == np.array([], dtype=np.float64))
393+
assert tick_labels == []
394+
395+
plt.yticks(ticks=[3.5, 6.5], labels=["a", "b"], minor=True)
396+
ax = plt.gca()
397+
tick_pos = ax.get_yticks(minor=True)
398+
tick_labels = ax.get_yticklabels(minor=True)
399+
assert np.all(tick_pos == np.array([3.5, 6.5]))
400+
assert [l.get_text() for l in tick_labels] == ['a', 'b']

0 commit comments

Comments
 (0)