Skip to content

Commit 6010bb4

Browse files
committed
MNT: make colorbars locators and formatters properties
1 parent 4b3d513 commit 6010bb4

File tree

1 file changed

+91
-28
lines changed

1 file changed

+91
-28
lines changed

lib/matplotlib/colorbar.py

Lines changed: 91 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -440,9 +440,10 @@ def __init__(self, ax, mappable=None, *, cmap=None,
440440
linewidths=[0.5 * mpl.rcParams['axes.linewidth']])
441441
self.ax.add_collection(self.dividers)
442442

443-
self.locator = None
444-
self.minorlocator = None
445-
self.formatter = None
443+
self._locator = None
444+
self._minorlocator = None
445+
self._formatter = None
446+
self._minorformatter = None
446447
self.__scale = None # linear, log10 for now. Hopefully more?
447448

448449
if ticklocation == 'auto':
@@ -453,19 +454,19 @@ def __init__(self, ax, mappable=None, *, cmap=None,
453454
self._reset_locator_formatter_scale()
454455

455456
if np.iterable(ticks):
456-
self.locator = ticker.FixedLocator(ticks, nbins=len(ticks))
457+
self._locator = ticker.FixedLocator(ticks, nbins=len(ticks))
457458
else:
458-
self.locator = ticks # Handle default in _ticker()
459+
self._locator = ticks # Handle default in _ticker()
459460

460461
if isinstance(format, str):
461462
# Check format between FormatStrFormatter and StrMethodFormatter
462463
try:
463-
self.formatter = ticker.FormatStrFormatter(format)
464-
_ = self.formatter(0)
464+
self._formatter = ticker.FormatStrFormatter(format)
465+
_ = self._formatter(0)
465466
except TypeError:
466-
self.formatter = ticker.StrMethodFormatter(format)
467+
self._formatter = ticker.StrMethodFormatter(format)
467468
else:
468-
self.formatter = format # Assume it is a Formatter or None
469+
self._formatter = format # Assume it is a Formatter or None
469470
self._draw_all()
470471

471472
if isinstance(mappable, contour.ContourSet) and not mappable.filled:
@@ -486,6 +487,66 @@ def __init__(self, ax, mappable=None, *, cmap=None,
486487
# Set the cla function to the cbar's method to override it
487488
self.ax.cla = self._cbar_cla
488489

490+
@property
491+
def locator(self):
492+
"""
493+
Major locator being used for colorbar
494+
"""
495+
return self._long_axis().get_major_locator()
496+
497+
@locator.setter
498+
def locator(self, loc):
499+
"""
500+
Set the major locator being used for colorbar
501+
"""
502+
self._long_axis().set_major_locator(loc)
503+
self._locator = loc
504+
505+
@property
506+
def minorlocator(self):
507+
"""
508+
Minor locator being used for colorbar
509+
"""
510+
return self._long_axis().get_minor_locator()
511+
512+
@minorlocator.setter
513+
def minorlocator(self, loc):
514+
"""
515+
Set minor locator being used for colorbar
516+
"""
517+
self._long_axis().set_minor_locator(loc)
518+
self._minorlocator = loc
519+
520+
@property
521+
def formatter(self):
522+
"""
523+
Major formatter being used for colorbar
524+
"""
525+
return self._long_axis().get_major_formatter()
526+
527+
@formatter.setter
528+
def formatter(self, fmt):
529+
"""
530+
Set major formatter being used for colorbar
531+
"""
532+
self._long_axis().set_major_formatter(fmt)
533+
self._locator = fmt
534+
535+
@property
536+
def minorformatter(self):
537+
"""
538+
Minor formatter being used for colorbar
539+
"""
540+
return self._long_axis().get_minor_formatter()
541+
542+
@minorformatter.setter
543+
def minorformatter(self, fmt):
544+
"""
545+
Set minor formatter being used for colorbar
546+
"""
547+
self._long_axis().set_minor_locator(fmt)
548+
self._minorformatter = fmt
549+
489550
def _cbar_cla(self):
490551
"""Function to clear the interactive colorbar state."""
491552
for x in self._interactive_funcs:
@@ -778,11 +839,11 @@ def update_ticks(self):
778839
"""
779840
Setup the ticks and ticklabels. This should not be needed by users.
780841
"""
781-
# Get the locator and formatter; defaults to self.locator if not None.
842+
# Get the locator and formatter; defaults to self._locator if not None.
782843
self._get_ticker_locator_formatter()
783-
self._long_axis().set_major_locator(self.locator)
784-
self._long_axis().set_minor_locator(self.minorlocator)
785-
self._long_axis().set_major_formatter(self.formatter)
844+
self._long_axis().set_major_locator(self._locator)
845+
self._long_axis().set_minor_locator(self._minorlocator)
846+
self._long_axis().set_major_formatter(self._formatter)
786847

787848
def _get_ticker_locator_formatter(self):
788849
"""
@@ -794,13 +855,15 @@ def _get_ticker_locator_formatter(self):
794855
795856
Called by update_ticks...
796857
"""
797-
locator = self.locator
798-
formatter = self.formatter
799-
minorlocator = self.minorlocator
858+
locator = self._locator
859+
formatter = self._formatter
860+
minorlocator = self._minorlocator
800861
if isinstance(self.norm, colors.BoundaryNorm):
801862
b = self.norm.boundaries
802863
if locator is None:
803864
locator = ticker.FixedLocator(b, nbins=10)
865+
if minorlocator is None:
866+
minorlocator = ticker.FixedLocator(b)
804867
elif isinstance(self.norm, colors.NoNorm):
805868
if locator is None:
806869
# put ticks on integers between the boundaries of NoNorm
@@ -825,9 +888,9 @@ def _get_ticker_locator_formatter(self):
825888
if formatter is None:
826889
formatter = self._long_axis().get_major_formatter()
827890

828-
self.locator = locator
829-
self.formatter = formatter
830-
self.minorlocator = minorlocator
891+
self._locator = locator
892+
self._formatter = formatter
893+
self._minorlocator = minorlocator
831894
_log.debug('locator: %r', locator)
832895

833896
@_api.delete_parameter("3.5", "update_ticks")
@@ -851,10 +914,10 @@ def set_ticks(self, ticks, update_ticks=True, labels=None, *,
851914
if np.iterable(ticks):
852915
self._long_axis().set_ticks(ticks, labels=labels, minor=minor,
853916
**kwargs)
854-
self.locator = self._long_axis().get_major_locator()
917+
self._locator = self._long_axis().get_major_locator()
855918
else:
856-
self.locator = ticks
857-
self._long_axis().set_major_locator(self.locator)
919+
self._locator = ticks
920+
self._long_axis().set_major_locator(self._locator)
858921
self.stale = True
859922

860923
def get_ticks(self, minor=False):
@@ -912,14 +975,14 @@ def minorticks_on(self):
912975
"""
913976
Turn on colorbar minor ticks.
914977
"""
978+
print(self._minorlocator)
915979
self.ax.minorticks_on()
916-
self.minorlocator = self._long_axis().get_minor_locator()
917980
self._short_axis().set_minor_locator(ticker.NullLocator())
918981

919982
def minorticks_off(self):
920983
"""Turn the minor ticks of the colorbar off."""
921-
self.minorlocator = ticker.NullLocator()
922-
self._long_axis().set_minor_locator(self.minorlocator)
984+
self._minorlocator = ticker.NullLocator()
985+
self._long_axis().set_minor_locator(self._minorlocator)
923986

924987
def set_label(self, label, *, loc=None, **kwargs):
925988
"""
@@ -1157,9 +1220,9 @@ def _reset_locator_formatter_scale(self):
11571220
the mappable normal gets changed: Colorbar.update_normal)
11581221
"""
11591222
self._process_values()
1160-
self.locator = None
1161-
self.minorlocator = None
1162-
self.formatter = None
1223+
self._locator = None
1224+
self._minorlocator = None
1225+
self._formatter = None
11631226
if (self.boundaries is not None or
11641227
isinstance(self.norm, colors.BoundaryNorm)):
11651228
if self.spacing == 'uniform':

0 commit comments

Comments
 (0)