From ecb53b6bf7445e50915b73bb9bf0c9642ab9b4cd Mon Sep 17 00:00:00 2001 From: unknown <> Date: Wed, 19 Jan 2022 23:06:26 +0100 Subject: [PATCH 01/10] Expanded documentation of Axis.set_ticks() --- lib/matplotlib/axis.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index 07f15a87b1bc..278a3e937e68 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -1826,6 +1826,21 @@ def set_ticks(self, ticks, labels=None, *, minor=False, **kwargs): List of tick locations. labels : list of str, optional List of tick labels. If not set, the labels show the data value. + + .. warning:: + Setting *labels=None* does not guarantee that labels for all + set ticks will be automatically generated - this depends + on the :class:`~matplotlib.ticker.Formatter` of the axis. + Some tick label formatters, e.g. + :class:`~matplotlib.ticker.ScalarFormatter` (default for + plots made with :meth:`~.Axes.plot`) will make + labels for custom ticks, while + others like e.g. + :class:`~matplotlib.ticker.LogFormatterSciNotation` + (default for :meth:`~.Axes.semilogy` plots) may not. + Supply all tick labels in the *labels* parameter if you + want to be sure. + minor : bool, default: False If ``False``, set the major ticks; if ``True``, the minor ticks. **kwargs @@ -1838,6 +1853,22 @@ def set_ticks(self, ticks, labels=None, *, minor=False, **kwargs): choice to prevent the surprise of a non-visible tick. If you need other limits, you should set the limits explicitly after setting the ticks. + + This method implicitly calls :meth:`.set_major_locator` (or + :meth:`.set_minor_locator` if *minor=True*) with a newly-constructed + :class:`~matplotlib.ticker.FixedLocator` initialized to the provided + tick locations. + + Do not add ticks manually to an automatically ticked axis + by e.g. doing:: + + special_ticks = [1.1, 2.5, 3.7] + ticks = axes.get_yticks() + ticks_ext = np.append(ticklist, special_ticks) + axes.set_yticks(ticks_ext) + + as this may give unpredictable results. + See :ghissue:`22262` for discussion. """ result = self._set_tick_locations(ticks, minor=minor) if labels is not None: From a222b658f03a593df62c66b615483a8921ac4af8 Mon Sep 17 00:00:00 2001 From: unknown <> Date: Thu, 20 Jan 2022 12:02:09 +0100 Subject: [PATCH 02/10] Fix flake8 W293 (blank line contains whitespace) warnings --- lib/matplotlib/axis.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index 278a3e937e68..90b5c94cf162 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -1826,7 +1826,7 @@ def set_ticks(self, ticks, labels=None, *, minor=False, **kwargs): List of tick locations. labels : list of str, optional List of tick labels. If not set, the labels show the data value. - + .. warning:: Setting *labels=None* does not guarantee that labels for all set ticks will be automatically generated - this depends @@ -1853,20 +1853,20 @@ def set_ticks(self, ticks, labels=None, *, minor=False, **kwargs): choice to prevent the surprise of a non-visible tick. If you need other limits, you should set the limits explicitly after setting the ticks. - + This method implicitly calls :meth:`.set_major_locator` (or :meth:`.set_minor_locator` if *minor=True*) with a newly-constructed :class:`~matplotlib.ticker.FixedLocator` initialized to the provided tick locations. - + Do not add ticks manually to an automatically ticked axis by e.g. doing:: - + special_ticks = [1.1, 2.5, 3.7] ticks = axes.get_yticks() ticks_ext = np.append(ticklist, special_ticks) axes.set_yticks(ticks_ext) - + as this may give unpredictable results. See :ghissue:`22262` for discussion. """ From b68978299dac2cff12d58de3ef792b77c434b377 Mon Sep 17 00:00:00 2001 From: yaaun <6322902+yaaun@users.noreply.github.com> Date: Thu, 20 Jan 2022 12:43:53 +0100 Subject: [PATCH 03/10] Expanded the documentation even more based on discussion in issue #22262 --- lib/matplotlib/axis.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index 90b5c94cf162..1c03f6122528 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -1860,14 +1860,26 @@ def set_ticks(self, ticks, labels=None, *, minor=False, **kwargs): tick locations. Do not add ticks manually to an automatically ticked axis - by e.g. doing:: + by e.g. doing (for the *y* axis, in this example):: - special_ticks = [1.1, 2.5, 3.7] + special_ticks = [1.1, 2.5, 3.7] # arbitrary tick locations ticks = axes.get_yticks() ticks_ext = np.append(ticklist, special_ticks) axes.set_yticks(ticks_ext) - as this may give unpredictable results. + as this may give unpredictable results. Instead, create a secondary + axis with :meth:`.Axes.secondary_yaxis` and set its ticks and tick + labels manually:: + + special_ticks = [1.1, 2.5, 3.7] # arbitrary tick locations + special_labels = [f"{tick:g}" for tick in special_ticks] + secaxis = axes.secondary_yaxis("left") # or "right", according to\ +preference + # Manually setting the labels, as outlined in the warning. + secaxis.set_yticks(special_ticks, labels=special_labels) + + Note that there is no limit to the number of secondary axes and it + is possible to overlay a secondary axis on another axis. See :ghissue:`22262` for discussion. """ result = self._set_tick_locations(ticks, minor=minor) From 8afe316ab030aeddaf68d1830e8b7888d33e576f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Kuli=C5=84ski?= <6322902+yaaun@users.noreply.github.com> Date: Thu, 20 Jan 2022 19:16:51 +0100 Subject: [PATCH 04/10] Update lib/matplotlib/axis.py - @jklymak rewording Co-authored-by: Jody Klymak --- lib/matplotlib/axis.py | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index 1c03f6122528..b50cd746ed03 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -1823,24 +1823,10 @@ def set_ticks(self, ticks, labels=None, *, minor=False, **kwargs): Parameters ---------- ticks : list of floats - List of tick locations. + List of tick locations. The axis `.Locator` is replaced by a `.FixedLocator`. Some + default tick formatters will not label arbitrary ticks, so pass *label* if needed. labels : list of str, optional - List of tick labels. If not set, the labels show the data value. - - .. warning:: - Setting *labels=None* does not guarantee that labels for all - set ticks will be automatically generated - this depends - on the :class:`~matplotlib.ticker.Formatter` of the axis. - Some tick label formatters, e.g. - :class:`~matplotlib.ticker.ScalarFormatter` (default for - plots made with :meth:`~.Axes.plot`) will make - labels for custom ticks, while - others like e.g. - :class:`~matplotlib.ticker.LogFormatterSciNotation` - (default for :meth:`~.Axes.semilogy` plots) may not. - Supply all tick labels in the *labels* parameter if you - want to be sure. - + List of tick labels. If not set, the labels are generated with the axis tick `.Formatter`. minor : bool, default: False If ``False``, set the major ticks; if ``True``, the minor ticks. **kwargs From 0af1d145660ef4e18257ef1250e7a8f4a7495dce Mon Sep 17 00:00:00 2001 From: yaaun <6322902+yaaun@users.noreply.github.com> Date: Thu, 20 Jan 2022 19:32:21 +0100 Subject: [PATCH 05/10] Reduced verbosity of doc by @jklymak 's suggestion. --- lib/matplotlib/axis.py | 37 +++++++------------------------------ 1 file changed, 7 insertions(+), 30 deletions(-) diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index b50cd746ed03..3b5d46dc528c 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -1823,10 +1823,12 @@ def set_ticks(self, ticks, labels=None, *, minor=False, **kwargs): Parameters ---------- ticks : list of floats - List of tick locations. The axis `.Locator` is replaced by a `.FixedLocator`. Some - default tick formatters will not label arbitrary ticks, so pass *label* if needed. + List of tick locations. The axis `.Locator` is replaced by a + `.FixedLocator`. Some default tick formatters will not label + arbitrary ticks, so pass *label* to ensure labels are visible. labels : list of str, optional - List of tick labels. If not set, the labels are generated with the axis tick `.Formatter`. + List of tick labels. If not set, the labels are generated with + the axis tick `.Formatter`. minor : bool, default: False If ``False``, set the major ticks; if ``True``, the minor ticks. **kwargs @@ -1840,33 +1842,8 @@ def set_ticks(self, ticks, labels=None, *, minor=False, **kwargs): other limits, you should set the limits explicitly after setting the ticks. - This method implicitly calls :meth:`.set_major_locator` (or - :meth:`.set_minor_locator` if *minor=True*) with a newly-constructed - :class:`~matplotlib.ticker.FixedLocator` initialized to the provided - tick locations. - - Do not add ticks manually to an automatically ticked axis - by e.g. doing (for the *y* axis, in this example):: - - special_ticks = [1.1, 2.5, 3.7] # arbitrary tick locations - ticks = axes.get_yticks() - ticks_ext = np.append(ticklist, special_ticks) - axes.set_yticks(ticks_ext) - - as this may give unpredictable results. Instead, create a secondary - axis with :meth:`.Axes.secondary_yaxis` and set its ticks and tick - labels manually:: - - special_ticks = [1.1, 2.5, 3.7] # arbitrary tick locations - special_labels = [f"{tick:g}" for tick in special_ticks] - secaxis = axes.secondary_yaxis("left") # or "right", according to\ -preference - # Manually setting the labels, as outlined in the warning. - secaxis.set_yticks(special_ticks, labels=special_labels) - - Note that there is no limit to the number of secondary axes and it - is possible to overlay a secondary axis on another axis. - See :ghissue:`22262` for discussion. + Do not add ticks manually to an automatically ticked axis (as with + *labels=None*). See :ghissue:`22262` for discussion. """ result = self._set_tick_locations(ticks, minor=minor) if labels is not None: From a7446d75b666f4ee09fe202decfdafc6949c4096 Mon Sep 17 00:00:00 2001 From: yaaun <6322902+yaaun@users.noreply.github.com> Date: Thu, 20 Jan 2022 19:39:39 +0100 Subject: [PATCH 06/10] On second thought, the previous wording could be seen as very ambiguous. --- lib/matplotlib/axis.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index 3b5d46dc528c..6270d3349251 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -1842,8 +1842,8 @@ def set_ticks(self, ticks, labels=None, *, minor=False, **kwargs): other limits, you should set the limits explicitly after setting the ticks. - Do not add ticks manually to an automatically ticked axis (as with - *labels=None*). See :ghissue:`22262` for discussion. + Do not mix automatically and manually placed ticks with this method. + See :ghissue:`22262` for discussion. """ result = self._set_tick_locations(ticks, minor=minor) if labels is not None: From 8519c6e9f920732fc3d30d6fe99d48933ddaa813 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Kuli=C5=84ski?= <6322902+yaaun@users.noreply.github.com> Date: Fri, 21 Jan 2022 10:10:02 +0100 Subject: [PATCH 07/10] Update set_ticks docstring by @timhoffm compromise suggestion Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> --- lib/matplotlib/axis.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index 6270d3349251..7861831c859c 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -1824,8 +1824,13 @@ def set_ticks(self, ticks, labels=None, *, minor=False, **kwargs): ---------- ticks : list of floats List of tick locations. The axis `.Locator` is replaced by a - `.FixedLocator`. Some default tick formatters will not label - arbitrary ticks, so pass *label* to ensure labels are visible. + `.FixedLocator`. + + Some tick formatters will not label arbitrary tick positions; + e.g. log formatters only label decade ticks by default. In + such a case you can set a formatter explicitly on the axis + using `.Axis.set_major_formatter` or provide formatted + *labels* yourself. labels : list of str, optional List of tick labels. If not set, the labels are generated with the axis tick `.Formatter`. From c7c264df4489c174a49e4c6c13b132d5affea220 Mon Sep 17 00:00:00 2001 From: yaaun <6322902+yaaun@users.noreply.github.com> Date: Fri, 21 Jan 2022 11:38:49 +0100 Subject: [PATCH 08/10] Removed extra sentence as per @timhoffm review --- lib/matplotlib/axis.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index 6270d3349251..8216c4d91527 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -1841,9 +1841,6 @@ def set_ticks(self, ticks, labels=None, *, minor=False, **kwargs): choice to prevent the surprise of a non-visible tick. If you need other limits, you should set the limits explicitly after setting the ticks. - - Do not mix automatically and manually placed ticks with this method. - See :ghissue:`22262` for discussion. """ result = self._set_tick_locations(ticks, minor=minor) if labels is not None: From 6595b8075a77f7efd85ab2416013bfc8c9b12055 Mon Sep 17 00:00:00 2001 From: yaaun <6322902+yaaun@users.noreply.github.com> Date: Fri, 21 Jan 2022 12:05:42 +0100 Subject: [PATCH 09/10] Blank line whitespace issue crept up again --- lib/matplotlib/axis.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index 29c510711dbd..f2e19e609809 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -1825,7 +1825,7 @@ def set_ticks(self, ticks, labels=None, *, minor=False, **kwargs): ticks : list of floats List of tick locations. The axis `.Locator` is replaced by a `.FixedLocator`. - + Some tick formatters will not label arbitrary tick positions; e.g. log formatters only label decade ticks by default. In such a case you can set a formatter explicitly on the axis From 9c3ffb0a873c7cfe84170cb4616df793b5bba4fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Kuli=C5=84ski?= <6322902+yaaun@users.noreply.github.com> Date: Sat, 22 Jan 2022 19:18:20 +0100 Subject: [PATCH 10/10] Update lib/matplotlib/axis.py as per correction by @timhoffm Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> --- lib/matplotlib/axis.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index f2e19e609809..94e030556229 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -1824,7 +1824,7 @@ def set_ticks(self, ticks, labels=None, *, minor=False, **kwargs): ---------- ticks : list of floats List of tick locations. The axis `.Locator` is replaced by a - `.FixedLocator`. + `~.ticker.FixedLocator`. Some tick formatters will not label arbitrary tick positions; e.g. log formatters only label decade ticks by default. In