Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 36daea4

Browse filesBrowse files
authored
Merge pull request #27167 from jklymak/enh-expose-long-axis
ENH: add long_axis property to colorbar
2 parents 61995ee + fecdb54 commit 36daea4
Copy full SHA for 36daea4

File tree

3 files changed

+39
-34
lines changed
Filter options

3 files changed

+39
-34
lines changed

‎lib/matplotlib/colorbar.py

Copy file name to clipboardExpand all lines: lib/matplotlib/colorbar.py
+35-34Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -430,44 +430,51 @@ def __init__(self, ax, mappable=None, *, cmap=None,
430430
self._extend_cid2 = self.ax.callbacks.connect(
431431
"ylim_changed", self._do_extends)
432432

433+
@property
434+
def long_axis(self):
435+
"""Axis that has decorations (ticks, etc) on it."""
436+
if self.orientation == 'vertical':
437+
return self.ax.yaxis
438+
return self.ax.xaxis
439+
433440
@property
434441
def locator(self):
435442
"""Major tick `.Locator` for the colorbar."""
436-
return self._long_axis().get_major_locator()
443+
return self.long_axis.get_major_locator()
437444

438445
@locator.setter
439446
def locator(self, loc):
440-
self._long_axis().set_major_locator(loc)
447+
self.long_axis.set_major_locator(loc)
441448
self._locator = loc
442449

443450
@property
444451
def minorlocator(self):
445452
"""Minor tick `.Locator` for the colorbar."""
446-
return self._long_axis().get_minor_locator()
453+
return self.long_axis.get_minor_locator()
447454

448455
@minorlocator.setter
449456
def minorlocator(self, loc):
450-
self._long_axis().set_minor_locator(loc)
457+
self.long_axis.set_minor_locator(loc)
451458
self._minorlocator = loc
452459

453460
@property
454461
def formatter(self):
455462
"""Major tick label `.Formatter` for the colorbar."""
456-
return self._long_axis().get_major_formatter()
463+
return self.long_axis.get_major_formatter()
457464

458465
@formatter.setter
459466
def formatter(self, fmt):
460-
self._long_axis().set_major_formatter(fmt)
467+
self.long_axis.set_major_formatter(fmt)
461468
self._formatter = fmt
462469

463470
@property
464471
def minorformatter(self):
465472
"""Minor tick `.Formatter` for the colorbar."""
466-
return self._long_axis().get_minor_formatter()
473+
return self.long_axis.get_minor_formatter()
467474

468475
@minorformatter.setter
469476
def minorformatter(self, fmt):
470-
self._long_axis().set_minor_formatter(fmt)
477+
self.long_axis.set_minor_formatter(fmt)
471478
self._minorformatter = fmt
472479

473480
def _cbar_cla(self):
@@ -526,7 +533,7 @@ def _draw_all(self):
526533
else:
527534
if mpl.rcParams['xtick.minor.visible']:
528535
self.minorticks_on()
529-
self._long_axis().set(label_position=self.ticklocation,
536+
self.long_axis.set(label_position=self.ticklocation,
530537
ticks_position=self.ticklocation)
531538
self._short_axis().set_ticks([])
532539
self._short_axis().set_ticks([], minor=True)
@@ -545,7 +552,7 @@ def _draw_all(self):
545552
# also adds the outline path to self.outline spine:
546553
self._do_extends()
547554
lower, upper = self.vmin, self.vmax
548-
if self._long_axis().get_inverted():
555+
if self.long_axis.get_inverted():
549556
# If the axis is inverted, we need to swap the vmin/vmax
550557
lower, upper = upper, lower
551558
if self.orientation == 'vertical':
@@ -686,7 +693,7 @@ def _do_extends(self, ax=None):
686693
if self.orientation == 'horizontal':
687694
xy = xy[:, ::-1]
688695
# add the patch
689-
val = -1 if self._long_axis().get_inverted() else 0
696+
val = -1 if self.long_axis.get_inverted() else 0
690697
color = self.cmap(self.norm(self._values[val]))
691698
patch = mpatches.PathPatch(
692699
mpath.Path(xy), facecolor=color, alpha=self.alpha,
@@ -710,7 +717,7 @@ def _do_extends(self, ax=None):
710717
if self.orientation == 'horizontal':
711718
xy = xy[:, ::-1]
712719
# add the patch
713-
val = 0 if self._long_axis().get_inverted() else -1
720+
val = 0 if self.long_axis.get_inverted() else -1
714721
color = self.cmap(self.norm(self._values[val]))
715722
hatch_idx = len(self._y) - 1
716723
patch = mpatches.PathPatch(
@@ -812,9 +819,9 @@ def update_ticks(self):
812819
"""
813820
# Get the locator and formatter; defaults to self._locator if not None.
814821
self._get_ticker_locator_formatter()
815-
self._long_axis().set_major_locator(self._locator)
816-
self._long_axis().set_minor_locator(self._minorlocator)
817-
self._long_axis().set_major_formatter(self._formatter)
822+
self.long_axis.set_major_locator(self._locator)
823+
self.long_axis.set_minor_locator(self._minorlocator)
824+
self.long_axis.set_major_formatter(self._formatter)
818825

819826
def _get_ticker_locator_formatter(self):
820827
"""
@@ -849,15 +856,15 @@ def _get_ticker_locator_formatter(self):
849856
if locator is None:
850857
# we haven't set the locator explicitly, so use the default
851858
# for this axis:
852-
locator = self._long_axis().get_major_locator()
859+
locator = self.long_axis.get_major_locator()
853860
if minorlocator is None:
854-
minorlocator = self._long_axis().get_minor_locator()
861+
minorlocator = self.long_axis.get_minor_locator()
855862

856863
if minorlocator is None:
857864
minorlocator = ticker.NullLocator()
858865

859866
if formatter is None:
860-
formatter = self._long_axis().get_major_formatter()
867+
formatter = self.long_axis.get_major_formatter()
861868

862869
self._locator = locator
863870
self._formatter = formatter
@@ -881,12 +888,12 @@ def set_ticks(self, ticks, *, labels=None, minor=False, **kwargs):
881888
pass *labels*. In other cases, please use `~.Axes.tick_params`.
882889
"""
883890
if np.iterable(ticks):
884-
self._long_axis().set_ticks(ticks, labels=labels, minor=minor,
891+
self.long_axis.set_ticks(ticks, labels=labels, minor=minor,
885892
**kwargs)
886-
self._locator = self._long_axis().get_major_locator()
893+
self._locator = self.long_axis.get_major_locator()
887894
else:
888895
self._locator = ticks
889-
self._long_axis().set_major_locator(self._locator)
896+
self.long_axis.set_major_locator(self._locator)
890897
self.stale = True
891898

892899
def get_ticks(self, minor=False):
@@ -899,9 +906,9 @@ def get_ticks(self, minor=False):
899906
if True return the minor ticks.
900907
"""
901908
if minor:
902-
return self._long_axis().get_minorticklocs()
909+
return self.long_axis.get_minorticklocs()
903910
else:
904-
return self._long_axis().get_majorticklocs()
911+
return self.long_axis.get_majorticklocs()
905912

906913
def set_ticklabels(self, ticklabels, *, minor=False, **kwargs):
907914
"""
@@ -936,7 +943,7 @@ def set_ticklabels(self, ticklabels, *, minor=False, **kwargs):
936943
**kwargs
937944
`.Text` properties for the labels.
938945
"""
939-
self._long_axis().set_ticklabels(ticklabels, minor=minor, **kwargs)
946+
self.long_axis.set_ticklabels(ticklabels, minor=minor, **kwargs)
940947

941948
def minorticks_on(self):
942949
"""
@@ -948,7 +955,7 @@ def minorticks_on(self):
948955
def minorticks_off(self):
949956
"""Turn the minor ticks of the colorbar off."""
950957
self._minorlocator = ticker.NullLocator()
951-
self._long_axis().set_minor_locator(self._minorlocator)
958+
self.long_axis.set_minor_locator(self._minorlocator)
952959

953960
def set_label(self, label, *, loc=None, **kwargs):
954961
"""
@@ -1013,7 +1020,7 @@ def _set_scale(self, scale, **kwargs):
10131020
`matplotlib.scale.register_scale`. These scales can then also
10141021
be used here.
10151022
"""
1016-
self._long_axis()._set_axes_scale(scale, **kwargs)
1023+
self.long_axis._set_axes_scale(scale, **kwargs)
10171024

10181025
def remove(self):
10191026
"""
@@ -1285,20 +1292,14 @@ def _get_extension_lengths(self, frac, automin, automax, default=0.05):
12851292

12861293
def _extend_lower(self):
12871294
"""Return whether the lower limit is open ended."""
1288-
minmax = "max" if self._long_axis().get_inverted() else "min"
1295+
minmax = "max" if self.long_axis.get_inverted() else "min"
12891296
return self.extend in ('both', minmax)
12901297

12911298
def _extend_upper(self):
12921299
"""Return whether the upper limit is open ended."""
1293-
minmax = "min" if self._long_axis().get_inverted() else "max"
1300+
minmax = "min" if self.long_axis.get_inverted() else "max"
12941301
return self.extend in ('both', minmax)
12951302

1296-
def _long_axis(self):
1297-
"""Return the long axis"""
1298-
if self.orientation == 'vertical':
1299-
return self.ax.yaxis
1300-
return self.ax.xaxis
1301-
13021303
def _short_axis(self):
13031304
"""Return the short axis"""
13041305
if self.orientation == 'vertical':

‎lib/matplotlib/colorbar.pyi

Copy file name to clipboardExpand all lines: lib/matplotlib/colorbar.pyi
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import matplotlib.spines as mspines
22
from matplotlib import cm, collections, colors, contour, colorizer
33
from matplotlib.axes import Axes
4+
from matplotlib.axis import Axis
45
from matplotlib.backend_bases import RendererBase
56
from matplotlib.patches import Patch
67
from matplotlib.ticker import Locator, Formatter
@@ -63,6 +64,8 @@ class Colorbar:
6364
location: Literal["left", "right", "top", "bottom"] | None = ...
6465
) -> None: ...
6566
@property
67+
def long_axis(self) -> Axis: ...
68+
@property
6669
def locator(self) -> Locator: ...
6770
@locator.setter
6871
def locator(self, loc: Locator) -> None: ...

‎lib/matplotlib/tests/test_colorbar.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_colorbar.py
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,6 +1139,7 @@ def test_colorbar_set_formatter_locator():
11391139
fmt = LogFormatter()
11401140
cb.minorformatter = fmt
11411141
assert cb.ax.yaxis.get_minor_formatter() is fmt
1142+
assert cb.long_axis is cb.ax.yaxis
11421143

11431144

11441145
@image_comparison(['colorbar_extend_alpha.png'], remove_text=True,

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.