diff --git a/doc/users/next_whats_new/plt_xyticks_support_minor.rst b/doc/users/next_whats_new/plt_xyticks_support_minor.rst new file mode 100644 index 000000000000..fef2ce764113 --- /dev/null +++ b/doc/users/next_whats_new/plt_xyticks_support_minor.rst @@ -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) diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py index 951e6e09b2fe..dac36d55089d 100644 --- a/lib/matplotlib/pyplot.py +++ b/lib/matplotlib/pyplot.py @@ -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. @@ -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. @@ -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. @@ -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. @@ -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 diff --git a/lib/matplotlib/tests/test_pyplot.py b/lib/matplotlib/tests/test_pyplot.py index f824df490c5f..2e51af54ea88 100644 --- a/lib/matplotlib/tests/test_pyplot.py +++ b/lib/matplotlib/tests/test_pyplot.py @@ -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']