Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

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

Sign up
Appearance settings

Commit36daea4

Browse files
authored
Merge pull request#27167 from jklymak/enh-expose-long-axis
ENH: add long_axis property to colorbar
2 parents61995ee +fecdb54 commit36daea4

File tree

3 files changed

+39
-34
lines changed

3 files changed

+39
-34
lines changed

‎lib/matplotlib/colorbar.py‎

Lines 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+
deflong_axis(self):
435+
"""Axis that has decorations (ticks, etc) on it."""
436+
ifself.orientation=='vertical':
437+
returnself.ax.yaxis
438+
returnself.ax.xaxis
439+
433440
@property
434441
deflocator(self):
435442
"""Major tick `.Locator` for the colorbar."""
436-
returnself._long_axis().get_major_locator()
443+
returnself.long_axis.get_major_locator()
437444

438445
@locator.setter
439446
deflocator(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
defminorlocator(self):
445452
"""Minor tick `.Locator` for the colorbar."""
446-
returnself._long_axis().get_minor_locator()
453+
returnself.long_axis.get_minor_locator()
447454

448455
@minorlocator.setter
449456
defminorlocator(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
defformatter(self):
455462
"""Major tick label `.Formatter` for the colorbar."""
456-
returnself._long_axis().get_major_formatter()
463+
returnself.long_axis.get_major_formatter()
457464

458465
@formatter.setter
459466
defformatter(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
defminorformatter(self):
465472
"""Minor tick `.Formatter` for the colorbar."""
466-
returnself._long_axis().get_minor_formatter()
473+
returnself.long_axis.get_minor_formatter()
467474

468475
@minorformatter.setter
469476
defminorformatter(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
ifmpl.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-
ifself._long_axis().get_inverted():
555+
ifself.long_axis.get_inverted():
549556
# If the axis is inverted, we need to swap the vmin/vmax
550557
lower,upper=upper,lower
551558
ifself.orientation=='vertical':
@@ -686,7 +693,7 @@ def _do_extends(self, ax=None):
686693
ifself.orientation=='horizontal':
687694
xy=xy[:, ::-1]
688695
# add the patch
689-
val=-1ifself._long_axis().get_inverted()else0
696+
val=-1ifself.long_axis.get_inverted()else0
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
ifself.orientation=='horizontal':
711718
xy=xy[:, ::-1]
712719
# add the patch
713-
val=0ifself._long_axis().get_inverted()else-1
720+
val=0ifself.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
iflocatorisNone:
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
ifminorlocatorisNone:
854-
minorlocator=self._long_axis().get_minor_locator()
861+
minorlocator=self.long_axis.get_minor_locator()
855862

856863
ifminorlocatorisNone:
857864
minorlocator=ticker.NullLocator()
858865

859866
ifformatterisNone:
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
ifnp.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
defget_ticks(self,minor=False):
@@ -899,9 +906,9 @@ def get_ticks(self, minor=False):
899906
if True return the minor ticks.
900907
"""
901908
ifminor:
902-
returnself._long_axis().get_minorticklocs()
909+
returnself.long_axis.get_minorticklocs()
903910
else:
904-
returnself._long_axis().get_majorticklocs()
911+
returnself.long_axis.get_majorticklocs()
905912

906913
defset_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
defminorticks_on(self):
942949
"""
@@ -948,7 +955,7 @@ def minorticks_on(self):
948955
defminorticks_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
defset_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
defremove(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"ifself._long_axis().get_inverted()else"min"
1295+
minmax="max"ifself.long_axis.get_inverted()else"min"
12891296
returnself.extendin ('both',minmax)
12901297

12911298
def_extend_upper(self):
12921299
"""Return whether the upper limit is open ended."""
1293-
minmax="min"ifself._long_axis().get_inverted()else"max"
1300+
minmax="min"ifself.long_axis.get_inverted()else"max"
12941301
returnself.extendin ('both',minmax)
12951302

1296-
def_long_axis(self):
1297-
"""Return the long axis"""
1298-
ifself.orientation=='vertical':
1299-
returnself.ax.yaxis
1300-
returnself.ax.xaxis
1301-
13021303
def_short_axis(self):
13031304
"""Return the short axis"""
13041305
ifself.orientation=='vertical':

‎lib/matplotlib/colorbar.pyi‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
importmatplotlib.spinesasmspines
22
frommatplotlibimportcm,collections,colors,contour,colorizer
33
frommatplotlib.axesimportAxes
4+
frommatplotlib.axisimportAxis
45
frommatplotlib.backend_basesimportRendererBase
56
frommatplotlib.patchesimportPatch
67
frommatplotlib.tickerimportLocator,Formatter
@@ -63,6 +64,8 @@ class Colorbar:
6364
location:Literal["left","right","top","bottom"]|None= ...
6465
)->None: ...
6566
@property
67+
deflong_axis(self)->Axis: ...
68+
@property
6669
deflocator(self)->Locator: ...
6770
@locator.setter
6871
deflocator(self,loc:Locator)->None: ...

‎lib/matplotlib/tests/test_colorbar.py‎

Lines 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
assertcb.ax.yaxis.get_minor_formatter()isfmt
1142+
assertcb.long_axisiscb.ax.yaxis
11421143

11431144

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp