Skip to content

ENH: add long_axis property to colorbar #27167

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 4 commits into from
Oct 28, 2024
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
69 changes: 35 additions & 34 deletions lib/matplotlib/colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,44 +430,51 @@
self._extend_cid2 = self.ax.callbacks.connect(
"ylim_changed", self._do_extends)

@property
def long_axis(self):
"""Axis that has decorations (ticks, etc) on it."""
if self.orientation == 'vertical':
return self.ax.yaxis
return self.ax.xaxis

@property
def locator(self):
"""Major tick `.Locator` for the colorbar."""
return self._long_axis().get_major_locator()
return self.long_axis.get_major_locator()

@locator.setter
def locator(self, loc):
self._long_axis().set_major_locator(loc)
self.long_axis.set_major_locator(loc)
self._locator = loc

@property
def minorlocator(self):
"""Minor tick `.Locator` for the colorbar."""
return self._long_axis().get_minor_locator()
return self.long_axis.get_minor_locator()

@minorlocator.setter
def minorlocator(self, loc):
self._long_axis().set_minor_locator(loc)
self.long_axis.set_minor_locator(loc)
self._minorlocator = loc

@property
def formatter(self):
"""Major tick label `.Formatter` for the colorbar."""
return self._long_axis().get_major_formatter()
return self.long_axis.get_major_formatter()

@formatter.setter
def formatter(self, fmt):
self._long_axis().set_major_formatter(fmt)
self.long_axis.set_major_formatter(fmt)
self._formatter = fmt

@property
def minorformatter(self):
"""Minor tick `.Formatter` for the colorbar."""
return self._long_axis().get_minor_formatter()
return self.long_axis.get_minor_formatter()

@minorformatter.setter
def minorformatter(self, fmt):
self._long_axis().set_minor_formatter(fmt)
self.long_axis.set_minor_formatter(fmt)
self._minorformatter = fmt

def _cbar_cla(self):
Expand Down Expand Up @@ -526,7 +533,7 @@
else:
if mpl.rcParams['xtick.minor.visible']:
self.minorticks_on()
self._long_axis().set(label_position=self.ticklocation,
self.long_axis.set(label_position=self.ticklocation,
ticks_position=self.ticklocation)
self._short_axis().set_ticks([])
self._short_axis().set_ticks([], minor=True)
Expand All @@ -545,7 +552,7 @@
# also adds the outline path to self.outline spine:
self._do_extends()
lower, upper = self.vmin, self.vmax
if self._long_axis().get_inverted():
if self.long_axis.get_inverted():
# If the axis is inverted, we need to swap the vmin/vmax
lower, upper = upper, lower
if self.orientation == 'vertical':
Expand Down Expand Up @@ -686,7 +693,7 @@
if self.orientation == 'horizontal':
xy = xy[:, ::-1]
# add the patch
val = -1 if self._long_axis().get_inverted() else 0
val = -1 if self.long_axis.get_inverted() else 0
color = self.cmap(self.norm(self._values[val]))
patch = mpatches.PathPatch(
mpath.Path(xy), facecolor=color, alpha=self.alpha,
Expand All @@ -710,7 +717,7 @@
if self.orientation == 'horizontal':
xy = xy[:, ::-1]
# add the patch
val = 0 if self._long_axis().get_inverted() else -1
val = 0 if self.long_axis.get_inverted() else -1
color = self.cmap(self.norm(self._values[val]))
hatch_idx = len(self._y) - 1
patch = mpatches.PathPatch(
Expand Down Expand Up @@ -812,9 +819,9 @@
"""
# 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)
self._long_axis().set_minor_locator(self._minorlocator)
self._long_axis().set_major_formatter(self._formatter)
self.long_axis.set_major_locator(self._locator)
self.long_axis.set_minor_locator(self._minorlocator)
self.long_axis.set_major_formatter(self._formatter)

def _get_ticker_locator_formatter(self):
"""
Expand Down Expand Up @@ -849,15 +856,15 @@
if locator is None:
# we haven't set the locator explicitly, so use the default
# for this axis:
locator = self._long_axis().get_major_locator()
locator = self.long_axis.get_major_locator()
if minorlocator is None:
minorlocator = self._long_axis().get_minor_locator()
minorlocator = self.long_axis.get_minor_locator()

if minorlocator is None:
minorlocator = ticker.NullLocator()

if formatter is None:
formatter = self._long_axis().get_major_formatter()
formatter = self.long_axis.get_major_formatter()

self._locator = locator
self._formatter = formatter
Expand All @@ -881,12 +888,12 @@
pass *labels*. In other cases, please use `~.Axes.tick_params`.
"""
if np.iterable(ticks):
self._long_axis().set_ticks(ticks, labels=labels, minor=minor,
self.long_axis.set_ticks(ticks, labels=labels, minor=minor,
**kwargs)
self._locator = self._long_axis().get_major_locator()
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)

Check warning on line 896 in lib/matplotlib/colorbar.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/colorbar.py#L896

Added line #L896 was not covered by tests
self.stale = True

def get_ticks(self, minor=False):
Expand All @@ -899,9 +906,9 @@
if True return the minor ticks.
"""
if minor:
return self._long_axis().get_minorticklocs()
return self.long_axis.get_minorticklocs()
else:
return self._long_axis().get_majorticklocs()
return self.long_axis.get_majorticklocs()

def set_ticklabels(self, ticklabels, *, minor=False, **kwargs):
"""
Expand Down Expand Up @@ -936,7 +943,7 @@
**kwargs
`.Text` properties for the labels.
"""
self._long_axis().set_ticklabels(ticklabels, minor=minor, **kwargs)
self.long_axis.set_ticklabels(ticklabels, minor=minor, **kwargs)

Check warning on line 946 in lib/matplotlib/colorbar.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/colorbar.py#L946

Added line #L946 was not covered by tests

def minorticks_on(self):
"""
Expand All @@ -948,7 +955,7 @@
def minorticks_off(self):
"""Turn the minor ticks of the colorbar off."""
self._minorlocator = ticker.NullLocator()
self._long_axis().set_minor_locator(self._minorlocator)
self.long_axis.set_minor_locator(self._minorlocator)

def set_label(self, label, *, loc=None, **kwargs):
"""
Expand Down Expand Up @@ -1013,7 +1020,7 @@
`matplotlib.scale.register_scale`. These scales can then also
be used here.
"""
self._long_axis()._set_axes_scale(scale, **kwargs)
self.long_axis._set_axes_scale(scale, **kwargs)

def remove(self):
"""
Expand Down Expand Up @@ -1285,20 +1292,14 @@

def _extend_lower(self):
"""Return whether the lower limit is open ended."""
minmax = "max" if self._long_axis().get_inverted() else "min"
minmax = "max" if self.long_axis.get_inverted() else "min"
return self.extend in ('both', minmax)

def _extend_upper(self):
"""Return whether the upper limit is open ended."""
minmax = "min" if self._long_axis().get_inverted() else "max"
minmax = "min" if self.long_axis.get_inverted() else "max"
return self.extend in ('both', minmax)

def _long_axis(self):
"""Return the long axis"""
if self.orientation == 'vertical':
return self.ax.yaxis
return self.ax.xaxis

def _short_axis(self):
"""Return the short axis"""
if self.orientation == 'vertical':
Expand Down
3 changes: 3 additions & 0 deletions lib/matplotlib/colorbar.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import matplotlib.spines as mspines
from matplotlib import cm, collections, colors, contour, colorizer
from matplotlib.axes import Axes
from matplotlib.axis import Axis
from matplotlib.backend_bases import RendererBase
from matplotlib.patches import Patch
from matplotlib.ticker import Locator, Formatter
Expand Down Expand Up @@ -63,6 +64,8 @@ class Colorbar:
location: Literal["left", "right", "top", "bottom"] | None = ...
) -> None: ...
@property
def long_axis(self) -> Axis: ...
@property
def locator(self) -> Locator: ...
@locator.setter
def locator(self, loc: Locator) -> None: ...
Expand Down
1 change: 1 addition & 0 deletions lib/matplotlib/tests/test_colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -1139,6 +1139,7 @@ def test_colorbar_set_formatter_locator():
fmt = LogFormatter()
cb.minorformatter = fmt
assert cb.ax.yaxis.get_minor_formatter() is fmt
assert cb.long_axis is cb.ax.yaxis


@image_comparison(['colorbar_extend_alpha.png'], remove_text=True,
Expand Down
Loading