Skip to content

Add minor keyword argument to plt.x/yticks #23049

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions doc/users/next_whats_new/plt_xyticks_support_minor.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
``plt.xticks`` and ``plt.yticks`` support *minor* keyword argument
------------------------------------------------------------------

It is now possible to set or get minor ticks using `.pyplot.xticks` and
`.pyplot.yticks` by setting ``minor=True``.

.. plot::
:include-source: true

import matplotlib.pyplot as plt
plt.figure()
plt.plot([1, 2, 3, 3.5], [2, 1, 0, -0.5])
plt.xticks([1, 2, 3], ["One", "Zwei", "Trois"])
plt.xticks([1.414, 2.5, 3.142],
[r"$\sqrt{2}$", r"$\frac{5}{2}$", r"$\pi$"], minor=True)
26 changes: 16 additions & 10 deletions lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1751,7 +1751,7 @@ def ylim(*args, **kwargs):
return ret


def xticks(ticks=None, labels=None, **kwargs):
def xticks(ticks=None, labels=None, *, minor=False, **kwargs):
"""
Get or set the current tick locations and labels of the x-axis.

Expand All @@ -1764,6 +1764,9 @@ def xticks(ticks=None, labels=None, **kwargs):
labels : array-like, optional
The labels to place at the given *ticks* locations. This argument can
only be passed if *ticks* is passed as well.
minor : bool, default: False
If ``False``, get/set the major ticks/labels; if ``True``, the minor
ticks/labels.
**kwargs
`.Text` properties can be used to control the appearance of the labels.

Expand Down Expand Up @@ -1794,24 +1797,24 @@ def xticks(ticks=None, labels=None, **kwargs):
ax = gca()

if ticks is None:
locs = ax.get_xticks()
locs = ax.get_xticks(minor=minor)
if labels is not None:
raise TypeError("xticks(): Parameter 'labels' can't be set "
"without setting 'ticks'")
else:
locs = ax.set_xticks(ticks)
locs = ax.set_xticks(ticks, minor=minor)

if labels is None:
labels = ax.get_xticklabels()
labels = ax.get_xticklabels(minor=minor)
for l in labels:
l._internal_update(kwargs)
else:
labels = ax.set_xticklabels(labels, **kwargs)
labels = ax.set_xticklabels(labels, minor=minor, **kwargs)

return locs, labels


def yticks(ticks=None, labels=None, **kwargs):
def yticks(ticks=None, labels=None, *, minor=False, **kwargs):
"""
Get or set the current tick locations and labels of the y-axis.

Expand All @@ -1824,6 +1827,9 @@ def yticks(ticks=None, labels=None, **kwargs):
labels : array-like, optional
The labels to place at the given *ticks* locations. This argument can
only be passed if *ticks* is passed as well.
minor : bool, default: False
If ``False``, get/set the major ticks/labels; if ``True``, the minor
ticks/labels.
**kwargs
`.Text` properties can be used to control the appearance of the labels.

Expand Down Expand Up @@ -1854,19 +1860,19 @@ def yticks(ticks=None, labels=None, **kwargs):
ax = gca()

if ticks is None:
locs = ax.get_yticks()
locs = ax.get_yticks(minor=minor)
if labels is not None:
raise TypeError("yticks(): Parameter 'labels' can't be set "
"without setting 'ticks'")
else:
locs = ax.set_yticks(ticks)
locs = ax.set_yticks(ticks, minor=minor)

if labels is None:
labels = ax.get_yticklabels()
labels = ax.get_yticklabels(minor=minor)
for l in labels:
l._internal_update(kwargs)
else:
labels = ax.set_yticklabels(labels, **kwargs)
labels = ax.set_yticklabels(labels, minor=minor, **kwargs)

return locs, labels

Expand Down
15 changes: 15 additions & 0 deletions lib/matplotlib/tests/test_pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,3 +383,18 @@ def test_doc_pyplot_summary():
f"The following functions are listed in the pyplot documentation, "
f"but they do not exist in pyplot. "
f"Please remove them from doc/api/pyplot_summary.rst: {extra!r}")


def test_minor_ticks():
plt.figure()
plt.plot(np.arange(1, 10))
tick_pos, tick_labels = plt.xticks(minor=True)
assert np.all(tick_labels == np.array([], dtype=np.float64))
assert tick_labels == []

plt.yticks(ticks=[3.5, 6.5], labels=["a", "b"], minor=True)
ax = plt.gca()
tick_pos = ax.get_yticks(minor=True)
tick_labels = ax.get_yticklabels(minor=True)
assert np.all(tick_pos == np.array([3.5, 6.5]))
assert [l.get_text() for l in tick_labels] == ['a', 'b']