8000 MNT: make colorbars locators and formatters properties · matplotlib/matplotlib@01d7288 · GitHub
[go: up one dir, main page]

Skip to content

Commit 01d7288

Browse files
committed
MNT: make colorbars locators and formatters properties
1 parent 7839b67 commit 01d7288

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
@@ -463,9 +463,10 @@ def __init__(self, ax, mappable=None, *, cmap=None,
463463
linewidths=[0.5 * mpl.rcParams['axes.linewidth']])
464464
self.ax.add_collection(self.dividers)
465465

466-
self.locator = None
467-
self.minorlocator = None
468-
self.formatter = None
466+
self._locator = None
467+
self._minorlocator = None
468+
self._formatter = None
469+
self._minorformatter = None
469470
self.__scale = None # linear, log10 for now. Hopefully more?
470471

471472
if ticklocation == 'auto':
@@ -476,19 +477,19 @@ def __init__(self, ax, mappable=None, *, cmap=None,
476477
self._reset_locator_formatter_scale()
477478

478479
if np.iterable(ticks):
479-
self.locator = ticker.FixedLocator(ticks, nbins=len(ticks))
480+
self._locator = ticker.FixedLocator(ticks, nbins=len(ticks))
480481
else:
481-
self.locator = ticks # Handle default in _ticker()
482+
self._locator = ticks # Handle default in _ticker()
482483

483484
if isinstance(format, str):
484485
# Check format between FormatStrFormatter and StrMethodFormatter
485486
try:
486-
self.formatter = ticker.FormatStrFormatter(format)
487-
_ = self.formatter(0)
487+
self._formatter = ticker.FormatStrFormatter(format)
488+
_ = self._formatter(0)
488489
except TypeError:
489-
self.formatter = ticker.StrMethodFormatter(format)
490+
self._formatter = ticker.StrMethodFormatter(format)
490491
else:
491-
self.formatter = format # Assume it is a Formatter or None
492+
self._formatter = format # Assume it is a Formatter or None
492493
self.draw_all()
493494

494495
if isinstance(mappable, contour.ContourSet) and not mappable.filled:
@@ -509,6 +510,66 @@ def __init__(self, ax, mappable=None, *, cmap=None,
509510
# Set the cla function to the cbar's method to override it
510511
self.ax.cla = self._cbar_cla
511512

5 8000 13+
@property
514+
def locator(self):
515+
"""
516+
Major locator being used for colorbar
517+
"""
518+
return self._long_axis().get_major_locator()
519+
520+
@locator.setter
521+
def locator(self, loc):
522+
"""
523+
Set the major locator being used for colorbar
524+
"""
525+
self._long_axis().set_major_locator(loc)
526+
self._locator = loc
527+
528+
@property
529+
def minorlocator(self):
530+
"""
531+
Minor locator being used for colorbar
532+
"""
533+
return self._long_axis().get_minor_locator()
534+
535+
@minorlocator.setter
536+
def minorlocator(self, loc):
537+
"""
538+
Set minor locator being used for colorbar
539+
"""
540+
self._long_axis().set_minor_locator(loc)
541+
self._minorlocator = loc
542+
543+
@property
544+
def formatter(self):
545+
"""
546+
Major formatter being used for colorbar
547+
"""
548+
return self._long_axis().get_major_formatter()
549+
550+
@formatter.setter
551+
def formatter(self, fmt):
552+
"""
553+
Set major formatter being used for colorbar
554+
"""
555+
self._long_axis().set_major_formatter(fmt)
556+
self._locator = fmt
557+
558+
@property
559+
def minorformatter(self):
560+
"""
561+
Minor formatter being used for colorbar
562+
"""
563+
return self._long_axis().get_minor_formatter()
564+
565+
@minorformatter.setter
566+
def minorformatter(self, fmt):
567+
"""
568+
Set minor formatter being used for colorbar
569+
"""
570+
self._long_axis().set_minor_locator(fmt)
571+
self._minorformatter = fmt
572+
512573
def _cbar_cla(self):
513574
"""Function to clear the interactive colorbar state."""
514575
for x in self._interactive_funcs:
@@ -791,11 +852,11 @@ def update_ticks(self):
791852
"""
792853
Setup the ticks and ticklabels. This should not be needed by users.
793854
"""
794-
# Get the locator and formatter; defaults to self.locator if not None.
855+
# Get the locator and formatter; defaults to self._locator if not None.
79 F438 5856
self._get_ticker_locator_formatter()
796-
self._long_axis().set_major_locator(self.locator)
797-
self._long_axis().set_minor_locator(self.minorlocator)
798-
self._long_axis().set_major_formatter(self.formatter)
857+
self._long_axis().set_major_locator(self._locator)
858+
self._long_axis().set_minor_locator(self._minorlocator)
859+
self._long_axis().set_major_formatter(self._formatter)
799860

800861
def _get_ticker_locator_formatter(self):
801862
"""
@@ -807,13 +868,15 @@ def _get_ticker_locator_formatter(self):
807868
808869
Called by update_ticks...
809870
"""
810-
locator = self.locator
811-
formatter = self.formatter
812-
minorlocator = self.minorlocator
871+
locator = self._locator
872+
formatter = self._formatter
873+
minorlocator = self._minorlocator
813874
if isinstance(self.norm, colors.BoundaryNorm):
814875
b = self.norm.boundaries
815876
if locator is None:
816877
locator = ticker.FixedLocator(b, nbins=10)
878+
if minorlocator is None:
879+
minorlocator = ticker.FixedLocator(b)
817880
elif isinstance(self.norm, colors.NoNorm):
818881
if locator is None:
819882
# put ticks on integers between the boundaries of NoNorm
@@ -838,9 +901,9 @@ def _get_ticker_locator_formatter(self):
838901
if formatter is None:
839902
formatter = self._long_axis().get_major_formatter()
840903

841-
self.locator = locator
842-
self.formatter = formatter
843-
self.minorlocator = minorlocator
904+
self._locator = locator
905+
self._formatter = formatter
906+
self._minorlocator = minorlocator
844907
_log.debug('locator: %r', locator)
845908

846909
@_api.delete_parameter("3.5", "update_ticks")
@@ -864,10 +927,10 @@ def set_ticks(self, ticks, update_ticks=True, labels=None, *,
864927
if np.iterable(ticks):
865928
self._long_axis().set_ticks(ticks, labels=labels, minor=minor,
866929
**kwargs)
867-
self.locator = self._long_axis().get_major_locator()
930+
self._locator = self._long_axis().get_major_locator()
868931
else:
869-
self.locator = ticks
870-
self._long_axis().set_major_locator(self.locator)
932+
self._locator = ticks
933+
self._long_axis().set_major_locator(self._locator)
871934
self.stale = True
872935

873936
def get_ticks(self, minor=False):
@@ -925,14 +988,14 @@ def minorticks_on(self):
925988
"""
926989
Turn on colorbar minor ticks.
927990
"""
991+
print(self._minorlocator)
928992
self.ax.minorticks_on()
929-
self.minorlocator = self._long_axis().get_minor_locator()
930993
self._short_axis().set_minor_locator(ticker.NullLocator())
931994

932995
def minorticks_off(self):
933996
"""Turn the minor ticks of the colorbar off."""
934-
self.minorlocator = ticker.NullLocator()
935-
self._long_axis().set_minor_locator(self.minorlocator)
997+
self._minorlocator = ticker.NullLocator()
998+
self._long_axis().set_minor_locator(self._minorlocator)
936999

9371000
def set_label(self, label, *, loc=None, **kwargs):
9381001
"""
@@ -1170,9 +1233,9 @@ def _reset_locator_formatter_scale(self):
11701233
the mappable normal gets changed: Colorbar.update_normal)
11711234
"""
11721235
self._process_values()
1173-
self.locator = None
1174-
self.minorlocator = None
1175-
self.formatter = None
1236+
self._locator = None
1237+
self._minorlocator = None
1238+
self._formatter = None
11761239
if (self.boundaries is not None or
11771240
isinstance(self.norm, colors.BoundaryNorm)):
11781241
if self.spacing == 'uniform':

0 commit comments

Comments
 (0)
0