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

Skip to content

FIX: pass colorbar.set_ticklabels down to long_axis #20758

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
Jul 29, 2021
Merged
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
58 changes: 38 additions & 20 deletions lib/matplotlib/colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,6 @@ def update_ticks(self):
"""
Setup the ticks and ticklabels. This should not be needed by users.
"""
ax = self.ax
# Get the locator and formatter; defaults to self.locator if not None.
self._get_ticker_locator_formatter()
self._long_axis().set_major_locator(self.locator)
Expand Down Expand Up @@ -817,26 +816,30 @@ def _get_ticker_locator_formatter(self):
_log.debug('locator: %r', locator)

@_api.delete_parameter("3.5", "update_ticks")
def set_ticks(self, ticks, update_ticks=True):
def set_ticks(self, ticks, update_ticks=True, labels=None, *,
minor=False, **kwargs):
"""
Set tick locations.

Parameters
----------
ticks : array-like or `~matplotlib.ticker.Locator` or None
The tick positions can be hard-coded by an array of values; or
they can be defined by a `.Locator`. Setting to *None* reverts
Comment on lines -827 to -828
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accepting a Locator is (intentionally?) no longer documented.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I didn't want to break API, however, axis.set_ticks does not take a locator so far as I can tell.

to using a default locator.

update_ticks : bool, default: True
As of 3.5 this has no effect.

ticks : list of floats
List of tick locations.
labels : list of str, optional
List of tick labels. If not set, the labels show the data value.
minor : bool, default: False
If ``False``, set the major ticks; if ``True``, the minor ticks.
**kwargs
`.Text` properties for the labels. These take effect only if you
pass *labels*. In other cases, please use `~.Axes.tick_params`.
"""
if np.iterable(ticks):
self.locator = ticker.FixedLocator(ticks, nbins=len(ticks))
self._long_axis().set_ticks(ticks, labels=labels, minor=minor,
**kwargs)
self.locator = self._long_axis().get_major_locator()
else:
self.locator = ticks
self._long_axis().set_major_locator(self.locator)
self._long_axis().set_major_locator(self.locator)
self.stale = True

def get_ticks(self, minor=False):
Expand All @@ -854,26 +857,41 @@ def get_ticks(self, minor=False):
return self._long_axis().get_majorticklocs()

@_api.delete_parameter("3.5", "update_ticks")
def set_ticklabels(self, ticklabels, update_ticks=True):
def set_ticklabels(self, ticklabels, update_ticks=True, *, minor=False,
**kwargs):
"""
Set tick labels.

.. admonition:: Discouraged

The use of this method is discouraged, because of the dependency
on tick positions. In most cases, you'll want to use
``set_ticks(positions, labels=labels)`` instead.

If you are using this method, you should always fix the tick
positions before, e.g. by using `.Colorbar.set_ticks` or by
explicitly setting a `~.ticker.FixedLocator` on the long axis
of the colorbar. Otherwise, ticks are free to move and the
labels may end up in unexpected positions.

Parameters
----------
ticklabels : sequence of str or of `.Text`
Texts for labeling each tick location in the sequence set by
`.Axis.set_ticks`; the number of labels must match the number of
locations.
`.Colorbar.set_ticks`; the number of labels must match the number
of locations.

update_ticks : bool, default: True
This keyword argument is ignored and will be be removed.
Deprecated

minor : bool
If True, set minor ticks instead of major ticks.

**kwargs
`.Text` properties for the labels.
"""
if isinstance(self.locator, ticker.FixedLocator):
self.formatter = ticker.FixedFormatter(ticklabels)
else:
_api._warn_external("set_ticks() must have been called.")
self.stale = True
self._long_axis().set_ticklabels(ticklabels, minor=minor, **kwargs)

def minorticks_on(self):
"""
Expand Down